我想從Android通知中加載Xamarin Forms頁面。我真的迷失在這個android的一面,我的大部分代碼都是從教程和博客文章拼湊而成的。然而,似乎沒有真正涵蓋這種情況。從android通知中加載Xamarin Forms頁面
我實現了一個非常簡單的接口與依賴服務來創建一個意向通知...
[assembly: Xamarin.Forms.Dependency(typeof(QuoteNotification))]
namespace QuoteApp.Droid
{
class QuoteNotification : IQuoteNotification
{
public void Notify(string title, string message)
{
//type needs to be derived from java, not xamarin.forms
Intent LoadPostPage = new Intent(Forms.Context, typeof(DroidToForms));
const int pendingIntentId = 0;
PendingIntent pendingIntent =
PendingIntent.GetActivity(Forms.Context, pendingIntentId, LoadPostPage, PendingIntentFlags.OneShot);
Notification.Builder builder = new Notification.Builder(Forms.Context)
.SetContentIntent(pendingIntent)
.SetAutoCancel(true)
.SetContentTitle(title)
.SetContentText(message)
.SetSmallIcon(Resource.Drawable.icon);
// Build the notification:
Notification notification = builder.Build();
// Get the notification manager:
NotificationManager notificationManager =
Forms.Context.GetSystemService(Context.NotificationService) as NotificationManager;
// Publish the notification:
const int notificationId = 0;
notificationManager.Notify(notificationId, notification);
}
}
}
這叫一個活動 -
namespace QuoteApp.Droid
{
[Activity(Label = "QuoteApp", MainLauncher = true)]
class DroidToForms : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
StartActivity(typeof(XFPostPage));
//Xamarin.Forms.Application.Current.MainPage.Navigation.PushAsync(new QuoteApp.PostPage());
}
}
}
進而調用此窗體頁,所有的代碼在這裏已經Xamarin.Android項目
namespace QuoteApp.Droid
{
/// <summary>
/// This is a Xamarin.Forms screen. It MUST:
/// * inherit from ANdroidActivity
/// * call Forms.Init()
/// * use LoadApplication()
/// </summary>
[Activity(Label = "XFPostPage", MainLauncher = true) ]
public class XFPostPage : Xamarin.Forms.Platform.Android.FormsApplicationActivity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
Xamarin.Forms.Forms.Init(this, bundle);
//LoadApplication(new App()); //how do i send to PostPage.xaml ??
Xamarin.Forms.Application.Current.MainPage.Navigation.PushAsync(new QuoteApp.PostPage());
}
}
}
這給在logcat的
07-04 17:51:44.494 7668 7668 E AndroidRuntime: Caused by: android.runtime.JavaProxyThrowable: System.NullReferenceException: Object reference not set to an instance of an object
07-04 17:51:44.494 7668 7668 E AndroidRuntime: at QuoteApp.Droid.XFPostPage.OnCreate (Android.OS.Bundle bundle) [0x00016] in <ab64801c0fb24acb8457c1d1202cc143>:0
07-04 17:51:44.494 7668 7668 E AndroidRuntime: at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_savedInstanceState) [0x0000f] in <d855bac285f44dda8a0d8510b679b1e2>:0
07-04 17:51:44.494 7668 7668 E AndroidRuntime: at (wrapper dynamic-method) System.Object:2780fa42-5931-40d3-8d14-a642f1a3025c (intptr,intptr,intptr)
07-04 17:51:44.494 7668 7668 E AndroidRuntime: at md543d03747be5a5b03a21a2a23b8ba191a.XFPostPage.n_onCreate(Native Method)
07-04 17:51:44.494 7668 7668 E AndroidRuntime: at md543d03747be5a5b03a21a2a23b8ba191a.XFPostPage.onCreate(XFPostPage.java:29)
07-04 17:51:44.494 7668 7668 E AndroidRuntime: at android.app.Activity.performCreate(Activity.java:6237)
07-04 17:51:44.494 7668 7668 E AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
07-04 17:51:44.494 7668 7668 E AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
07-04 17:51:44.494 7668 7668 E AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
07-04 17:51:44.494 7668 7668 E AndroidRuntime: at android.app.ActivityThread.-wrap11(ActivityThread.java)
07-04 17:51:44.494 7668 7668 E AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
07-04 17:51:44.494 7668 7668 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
07-04 17:51:44.494 7668 7668 E AndroidRuntime: at android.os.Looper.loop(Looper.java:148)
07-04 17:51:44.494 7668 7668 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5417)
07-04 17:51:44.494 7668 7668 E AndroidRuntime: ... 3 more
以下錯誤我已經嘗試了一堆不同的想法都具有不同的運行時錯誤,我的Xamarin.Android運作的瞭解是非常有限的。最終我將通過AlarmManager使用通知,並將進一步的信息發送到PostPage.Xaml。目前,我只是迷失在這個錯誤的地方?!
任何幫助將非常感激,在此先感謝。
嘗試使用頁面渲染器https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/custom-renderer/contentpage/ – Pratik
我不知道如何從Android加載Xamarin表單頁面,這似乎去另一種方式來加載在窗體中的Android特定頁面?除非我錯過了什麼? – Chrisco