簡體   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