问题描述
我正在尝试从Android通知加载Xamarin表单页面.我真的迷上了android方面,我的大部分代码已从教程和博客文章中拼凑而成.但是,似乎没有什么东西能真正涵盖这种情况.
I am trying to load a Xamarin Forms page from an Android Notification. I am really lost on the android side of this, and most of my code has been pieced together from tutorials and blog posts. However nothing really seems to cover exactly this scenario.
我已经实现了一个非常简单的带有依赖项服务的界面,以创建具有意图的通知...
I have implemented a very simple interface with dependency service to create the notification with an intent...
[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项目中
which in turn calls this forms page, all the code here has been in Xamarin.Android project
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中给出以下错误
this gives the following error in 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.目前,我只是失去了确切位置,这是怎么了?!虽然
I have tried a bunch of different ideas all with different runtime errors, my understanding of the Xamarin.Android workings are very limited. eventually i will be using the notification with AlarmManager and further information will be sent to PostPage.Xaml. At the moment i'm just lost at exactly where this is going wrong though?!
任何帮助将不胜感激,在此先感谢您.
Any Help would be hugely appreciated, thanks in advance.
推荐答案
问题出在您的代码Intent LoadPostPage = new Intent(Forms.Context, typeof(DroidToForms));
上.
对于XF项目的Android平台,它只有一个Activity
,即MainActivity
,或者基于此MainActivity
呈现XF页面.
For Android platform of XF project, it only has one Activity
, which is MainActivity
, or pages of XF are rendered based on this MainActivity
.
因此,您可以这样更改代码:
So you may change you code like this:
Intent intent = new Intent(context, typeof(MainActivity));
intent.PutExtras(valuesForActivity);
PendingIntent resultPendingIntent = PendingIntent.GetActivity(Xamarin.Forms.Forms.Context, 0, intent, PendingIntentFlags.OneShot);
然后在MainActivity
的OnCreate
方法中:
//navigate to the page of PCL
Xamarin.Forms.Application.Current.MainPage.Navigation.PushAsync(new PostPage());
最后,按照@Pratik的建议,如果要为Android平台创建本机页面/视图,请使用PageRenderer
或 ViewRenderer 即可.
At last, as @Pratik suggested, if you want to create a native page/ view for Android platform, use PageRenderer
or ViewRenderer to do so.
这篇关于从android通知加载Xamarin Forms页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!