从 Application.Context 获取当前活动 - MonoAndroid

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25613225/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:34:37  来源:igfitidea点击:

Get current activity from Application.Context - MonoAndroid

androiddependency-injectionxamarinxamarin.androidxamarin.forms

提问by Michael Kniskern

I am currently developing an application using Xamarin.Formsthat will be available on the Android and iOS platforms. When the application is first loaded on device, I check to see if there is an internet connection available on the device. I want to display a dialog box if an internet connection is not available.

我目前正在开发一个Xamarin.Forms可在 Android 和 iOS 平台上使用的应用程序。当应用程序首次加载到设备上时,我会检查设备上是否有可用的互联网连接。如果 Internet 连接不可用,我想显示一个对话框。

Here is the following snippet of code I am using to check the internet on the Xamarin.Forms.ContentPage

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

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

I am using dependency injection to build the appropriate module for handling dialog boxes for each appropriate environment. The Android is throwing the following exception

我正在使用依赖注入来构建适当的模块来处理每个适当环境的对话框。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:

Android.Views.WindowManagerBadTokenException: Unable to add window -- token null is not for an application 下面是 Android 上 DisplayLocalNotification 方法的代码:

public void DisplayLocalNotification(string title, string content)
{        
     AlertDialog.Builder builder = new AlertDialog.Builder(Application.Context)
          .SetTitle(title)
          .SetMessage(content)
          .SetCancelable(true)
          .SetPositiveButton("OK", (EventHandler<DialogClickEventArgs>) null);

      AlertDialog alert = builder.Create();
      alert.Show();

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

      okBtn.Click += (sender, args) =>
      {
           alert.Dismiss();
      };
}

After doing some research, I need to get pass the current activity to the AlertDialog.Builderconstructor instead of the Application.Context. How do I get the current activity object from the application context when you need to the activity outside of the activity context?

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

回答by SKall

Xamarin.Forms Android platform code should assign the current Activity into Forms.Context property. This is the static Forms class and if you debug it you will see that the Forms.Context is an Activity.

Xamarin.Forms Android 平台代码应将当前 Activity 分配给 Forms.Context 属性。这是静态 Forms 类,如果您调试它,您将看到 Forms.Context 是一个 Activity。

public static class Forms
{
    public static Context Context { get; }
    public static bool IsInitialized { get; }

    public static event EventHandler<ViewInitializedEventArgs> ViewInitialized;

    public static void Init(Activity activity, Bundle bundle);
}