C# Xamarin toast消息仅在emulator中工作

C# Xamarin toast消息仅在emulator中工作,c#,android,xamarin.android,android-toast,C#,Android,Xamarin.android,Android Toast,我试图用以下C#代码在针对Android的Xamarin应用程序中显示toast消息: [活动(Label=“Storyvoque”,MainLauncher=true)] 公共类MainActivity:Activity、ICommandExecutionContext、HistoryHostContext、IProgress ... 公共覆盖bool OnOptionsItemSelected(IMenuItem) ... 尝试 { RunOnUiThread(()=> { 尝试 { 使用(

我试图用以下C#代码在针对Android的Xamarin应用程序中显示toast消息:

[活动(Label=“Storyvoque”,MainLauncher=true)]
公共类MainActivity:Activity、ICommandExecutionContext、HistoryHostContext、IProgress
...
公共覆盖bool OnOptionsItemSelected(IMenuItem)
...
尝试
{
RunOnUiThread(()=>
{
尝试
{
使用(var toast=toast.MakeText(this,Resource.String.msg\u story\u saved,ToastLength.Short))
toast.Show();
ShowMessage(“OK”);
}
捕获(例外情况除外)
{
ShowMessage(例如Message);
}
});
}
捕获(例外情况除外)
{
ShowMessage(例如Message);
}
...
私有void ShowMessage(字符串消息)
{
RunOnUiThread(()=>
{
AlertDialog dlgAlert=新建AlertDialog.Builder(this.Create();
dlgAlert.SetMessage(消息);
dlgAlert.SetButton(“关闭”,(s,args)=>
{
dlgAlert.disclose();
dlgAlert.Dispose();
});
dlgAlert.Show();
});
}

在我的AVD_for_Nexus_6_by_Google模拟设备上运行Android 6.0 API Level 23,一切正常。我收到一条祝酒词和一个写着“OK”的留言框。但当我在运行安卓8.1.0的Nexus6p上运行同一个应用程序时,会显示“OK”消息,但不会显示toast消息。即使没有OK消息,toast消息也不会出现。我检查了应用程序的通知设置;我没有为我的任何应用禁用通知。如何找到此问题的根源?

您确定资源构建正确吗?特别是在更改设备时,它可以帮助清理解决方案并重建。您是否尝试过“Application.Context”?为什么要在ui线程中调用toast.Show()?打电话到外面,如果有问题就告诉我works@G.hakim我尝试删除RunOnUiThread,重建应用程序并将其重新部署到我的设备上。现在它起作用了。但后来我尝试了另一个代码,它仍然使用
RunOnUiThread
,昨天也失败了,现在它也可以工作了。所以其他一些改变导致Toast消息开始在我的设备上工作,我不知道它是什么。
[Activity(Label = "Storyvoque", MainLauncher = true)]
public class MainActivity : Activity, ICommandExecutionContext, IStoryHostContext, IProgress<string>
...
    public override bool OnOptionsItemSelected(IMenuItem item)
    ...
        try
        {
           RunOnUiThread(() =>
           {
              try
              {
                 using (var toast = Toast.MakeText(this, Resource.String.msg_story_saved, ToastLength.Short))
                    toast.Show();
                 ShowMessage("OK");
              }
              catch (Exception ex)
              {
                 ShowMessage(ex.Message);
              }
           });
        }
        catch(Exception ex)
        {
           ShowMessage(ex.Message);
        }
   ...
   private void ShowMessage(string message)
   {
      RunOnUiThread(() =>
      {
         AlertDialog dlgAlert = new AlertDialog.Builder(this).Create();
         dlgAlert.SetMessage(message);
         dlgAlert.SetButton("Close", (s, args) =>
         {
            dlgAlert.Dismiss();
            dlgAlert.Dispose();
         });
         dlgAlert.Show();
      });
   }