繁体   English   中英

从Xamarin Android中的后台线程获取上下文

[英]Getting Context from Background thread in Xamarin Android

在我的应用程序中,我需要从后台线程调用警报方法。 我需要从后台线程获取上下文。 在获取上下文时,我得到了令牌空错误。 这是我的代码

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);
        };

我正在使用Application.Contextthis也不起作用。 是否有人有适当执行此操作的想法。

我找到一种解决方案,通过使用WindowManagerTypes作为Toa​​st从后台线程显示警报

dialog.Window.SetType(Android.Views.WindowManagerTypes.Toast);

从后台线程调用

RunOnUiThread(() =>
{
    //show the alert here (you can use the keyword 'this')
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM