2017-08-06 60 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') 
}); 
相关问题