android获取当前活动,从Application.Context – MonoAndroid获取当前活动

我目前正在使用XAMarin.Forms开发一个可在

Android和iOS平台上使用的应用程序.当应用程序首次加载到设备上时,我检查设备上是否有可用的Internet连接.如果Internet连接不可用,我想显示一个对话框.

以下是我用来检查Xamarin.Forms.ContentPage上的互联网的以下代码片段

if(App.Connectivity.IsNetworkConnectivityAvailable())

{

App.Notification.DisplayLocalNotifications("No Internet", "You need an internet connection to access certain application content");

}

我正在使用依赖注入来构建适合每个适当环境的对话框的模块. Android正在抛出以下异常

Android.Views.WindowManagerBadTokenException: Unable to add window —

token null is not for an application Here is the code for the

DisplayLocalNotification method on the Android:

public void DisplayLocalNotification(string title, string content)

{

AlertDialog.Builder builder = new AlertDialog.Builder(Application.Context)

.SetTitle(title)

.SetMessage(content)

.SetCancelable(true)

.SetPositiveButton("OK", (EventHandler) null);

AlertDialog alert = builder.Create();

alert.Show();

var okBtn = alert.GetButton((int)DialogButtonType.Positive);

okBtn.Click += (sender, args) =>

{

alert.Dismiss();

};

}

在进行一些研究之后,我需要将当前活动传递给AlertDialog.Builder构造函数而不是Application.Context.当您需要在活动上下文之外的活动时,如何从应用程序上下文获取当前活动对象?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值