我目前正在使用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.当您需要在活动上下文之外的活动时,如何从应用程序上下文获取当前活动对象?