[英]Getting Context from Background thread in Xamarin Android
In my application, I need to invoke an alert method from the background thread. 在我的应用程序中,我需要从后台线程调用警报方法。 I need to get the Context from the background thread.
我需要从后台线程获取上下文。 While getting the context I am getting the token null error.
在获取上下文时,我得到了令牌空错误。 Here is my code
这是我的代码
Handler h = new Handler();
BackgroService.Event += () =>
{
Action myAction = () =>
{
Dialog dialog = new Dialog(Application.Context);
Android.App.AlertDialog.Builder alert = new Android.App.AlertDialog.Builder(Application.Context, Resource.Style.AlertDialog);
alert.SetTitle("");
alert.SetMessage("MSG");
alert.SetPositiveButton(GetString(Resource.String.ok), (senderAlert, args) =>
{
//MyAction
dialog.Dismiss();
});
dialog = alert.Create();
dialog.Window.SetType(Android.Views.WindowManagerTypes.ApplicationPanel);
dialog.Show();
};
h.PostDelayed(myAction, 1000);
};
I am using Application.Context
and also this
are not working. 我正在使用
Application.Context
, this
也不起作用。 Does any one have any idea of doing this properly. 是否有人有适当执行此操作的想法。
我找到一种解决方案,通过使用WindowManagerTypes作为Toast从后台线程显示警报
dialog.Window.SetType(Android.Views.WindowManagerTypes.Toast);
From the background thread call 从后台线程调用
RunOnUiThread(() =>
{
//show the alert here (you can use the keyword 'this')
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.