2017-08-06 55 views
0

在我的應用程序中,我需要從後臺線程中調用一個alert方法。我需要從後臺線程獲取上下文。獲取上下文時,我得到了令牌空錯誤。這裏是我的代碼在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不工作。有沒有人有任何想法做到這一點。

回答

0

我找到一個解決方案通過使用WindowManagerTypes敬酒

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

從後臺線程調用顯示從後臺線程警報

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