Xamarin.android 从android通知加载Xamarin表单页面
我正在尝试从Android通知加载Xamarin表单页面。我真的很迷恋android,我的大部分代码都是从教程和博客文章中拼凑而成的。然而,似乎没有任何东西能够完全涵盖这种情况 我已经用依赖服务实现了一个非常简单的接口来创建通知,目的是Xamarin.android 从android通知加载Xamarin表单页面,xamarin.android,xamarin.forms,Xamarin.android,Xamarin.forms,我正在尝试从Android通知加载Xamarin表单页面。我真的很迷恋android,我的大部分代码都是从教程和博客文章中拼凑而成的。然而,似乎没有任何东西能够完全涵盖这种情况 我已经用依赖服务实现了一个非常简单的接口来创建通知,目的是 [assembly: Xamarin.Forms.Dependency(typeof(QuoteNotification))] namespace QuoteApp.Droid { class QuoteNotification : IQuoteNo
[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());
}
}
}
名称空间QuoteApp.Droid
{
///
///这是Xamarin.Forms屏幕。它必须:
///*继承自雄激素活性
///*call Forms.Init()
///*使用LoadApplication()
///
[活动(Label=“XFPostPage”,MainLauncher=true)]
公共类XFPostPage:Xamarin.Forms.Platform.Android.FormsApplicationActivity
{
创建时受保护的覆盖无效(捆绑包)
{
base.OnCreate(bundle);
Xamarin.Forms.Forms.Init(这个,bundle);
//LoadApplication(new App());//如何发送到PostPage.xaml??
Xamarin.Forms.Application.Current.MainPage.Navigation.PushAsync(新的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
07-04 17:51:44.494 7668 7668 E AndroidRuntime:原因:android.runtime.JavaProxyThrowable:System.NullReferenceException:对象引用未设置为对象的实例
07-04 17:51:44.494 7668 7668 E AndroidRuntime:at QuoteApp.Droid.xfppostpage.OnCreate(Android.OS.Bundle)[0x00016]in:0
07-04 17:51:44.494 7668 7668 E AndroidRuntime:在Android.App.Activity.n_OnCreate_Landroid_os_Bundle(System.IntPtr jnienv,System.IntPtr native_this,System.IntPtr native_savedInstanceState)[0x0000f]in:0
07-04 17:51:44.494 7668 7668 E AndroidRuntime:at(包装器动态方法)系统。对象:2780fa42-5931-40d3-8d14-a642f1a3025c(intptr、intptr、intptr)
07-04 17:51:44.494 7668 7668 E AndroidRuntime:at MD543D03747BE5A5B03A2A2A23B8BA191A.xPostPage.n_onCreate(本机方法)
07-04 17:51:44.494 7668 7668 E AndroidRuntime:at MD543D03747BE5A5B03A2A2A23B8BA191A.xfpostage.onCreate(xfpostage.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:在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个以上
我尝试了很多不同的想法,但都有不同的运行时错误,我对Xamarin的理解非常有限。最终,我将与AlarmManager一起使用通知,进一步的信息将发送到PostPage.Xaml。现在我完全不知道这到底是怎么回事
任何帮助都将不胜感激,提前谢谢 问题在于您的代码
Intent LoadPostPage=newintent(Forms.Context,typeof(DroidToForms))代码>
对于XF项目的Android平台,它只有一个Activity
,即MainActivity
,或者XF的页面是基于该MainActivity
呈现的
因此,您可以这样更改代码:
Intent intent = new Intent(context, typeof(MainActivity));
intent.PutExtras(valuesForActivity);
PendingIntent resultPendingIntent = PendingIntent.GetActivity(Xamarin.Forms.Forms.Context, 0, intent, PendingIntentFlags.OneShot);
然后在main活动的OnCreate
方法中:
//navigate to the page of PCL
Xamarin.Forms.Application.Current.MainPage.Navigation.PushAsync(new PostPage());
最后,正如@Pratik所建议的,如果您想为Android平台创建一个本机页面/视图,请使用PageRenderer
或这样做 对于任何无意中发现这一点的人,我最终在MainActivity
中使用了OnNewIntent
这里有一些极好的信息
我的代码最后看起来像这样
调用依赖项服务以获取android实现
DependencyService.Get<IQuoteNotification>().Notify("words",SelectedQuote.FixedTitle , SelectedQuote.Id);
然后覆盖main活动中的OnNewIntent
。如果意图不是由我的QuoteNotification
类生成的,则将-1作为PostID发送
protected override void OnNewIntent(Intent intent)
{
if (intent.HasExtra("NotificationTrue")){
LoadApplication(new App(intent.GetIntExtra("PostID", -1)));
//Xamarin.Forms.Application.Current.MainPage.Navigation.PushAsync(new QuoteApp.PostPage());
}
base.OnNewIntent(intent);
}
然后在App.Xaml.cs
中添加一些逻辑以导航到正确的页面
public partial class App : Application
{
public App(int PostId = -1)
{
InitializeComponent();
if (PostId >= 0)
{
MainPage = new NavigationPage(new PostPage(PostId));
}
else
MainPage = new NavigationPage(new MainPage());
谢谢,希望它能帮助其他人。尝试使用页面渲染器我不确定如何从android加载Xamarin表单页面,这似乎与在表单中加载android特定页面的方式相反?除非我错过了什么?谢谢你,这似乎是我在别处读到的建议,我也尝试过。它似乎每次启动都会推动延迟,这是不可取的。我在主活动中使用OnNewIntent几乎可以正常工作。@Chrisco,“它似乎每次启动时都会推迟,这是不可取的。”关键是,对于XF,或者页面基于MainActivity
,您可以根据从通知中获得的额外信息导航到其他页面,但是,不建议从MainActivity
尝试新的活动。谢谢,我明白了这一点,我的最新代码现在基于“MainActivity”。您好,我已经尝试了上面的代码,它工作得很好,但当用户收到多种类型的通知时,我遇到了问题。聚氨基甲酸酯
public partial class App : Application
{
public App(int PostId = -1)
{
InitializeComponent();
if (PostId >= 0)
{
MainPage = new NavigationPage(new PostPage(PostId));
}
else
MainPage = new NavigationPage(new MainPage());