Xaml CloseButtonText给出InvalidCastException

时间:2017-06-19 03:26:38

标签: c# xaml uwp

我有一个基本的UAP应用程序,可以在Windows 10 Mobile Enterprise设备上运行。但是,当我尝试打开最基本的ContentDialogs时,在尝试设置其CloseButtonText

时会出现InvalidCastException。
 using Windows.UI.Xaml.Controls;
 ...

 private async void DisplayNoWifiDialog()
    {
        ContentDialog noWifiDialog = new ContentDialog {
            Title = "No wifi connection",
            Content = "Check your connection and try again.",
            CloseButtonText = "Ok"
        };

        ContentDialogResult result = await noWifiDialog.ShowAsync();
    }
  

无法转换“Windows.UI.Xaml.Controls.ContentDialog”类型的对象   输入'Windows.UI.Xaml.Controls.IContentDialog2'。

     

System.InvalidCastException:指定的强制转换无效。在   System.StubHelpers.StubHelpers.GetCOMIPFromRCW_WinRT(Object objSrc,   IntPtr pCPCMD,IntPtr& ppTarget)at   Windows.UI.Xaml.Controls.ContentDialog.put_CloseButtonText(字符串   价值)   MyApplication.MainPage.d__4.MoveNext()   ---从抛出异常的先前位置开始的堆栈跟踪结束--- at   System.Runtime.CompilerServices.AsyncMethodBuilderCore<> c.b__6_0(对象   国家)   System.Threading.WinRTSynchronizationContext.Invoker.InvokeCore()

最小和目标Win 10平台分别设置为10240和15063。 CloseButtonText于1703年推出。

2 个答案:

答案 0 :(得分:1)

试试这个

var dialog = new ContentDialog() {
        Title = "No wifi connection",
        //RequestedTheme = ElementTheme.Dark,
        //FullSizeDesired = true,
        MaxWidth = this.ActualWidth // Required for Mobile!
    };
panel.Children.Add(new TextBlock {
    Text = "Check your connection and try again." ,
    TextWrapping = TextWrapping.Wrap,
});

// a check box for example
var cb = new CheckBox {
    Content = "Don't show this dialog again"
};

panel.Children.Add(cb);
dialog.Content = panel;

// Add Buttons
dialog.PrimaryButtonText = "OK";
dialog.PrimaryButtonClick += delegate {
   // do something
};
// Show Dialog
var result = await dialog.ShowAsync();

或使用MessageDialog

 var dialog = new Windows.UI.Popups.MessageDialog(
                "Check your connection and try again." ,
                "No wifi connection");

    dialog.Commands.Add(new Windows.UI.Popups.UICommand("Ok") { Id = 0 });
   // add another button if you want to
   //dialog.Commands.Add(new Windows.UI.Popups.UICommand("Cancel") { Id = 1 });

    // example: check mobile and add another button
    if (Windows.System.Profile.AnalyticsInfo.VersionInfo.DeviceFamily != "Windows.Mobile") 
    {
        // Adding a 3rd command will crash the app when running on Mobile !!!
        //dialog.Commands.Add(new Windows.UI.Popups.UICommand("Maybe later") { Id = 2 });
    }

    dialog.DefaultCommandIndex = 0;
    //dialog.CancelCommandIndex = 1;

    var result = await dialog.ShowAsync();

答案 1 :(得分:0)

在Windows 10上工作

在Win10盒子上成功运行以下代码后,我得到了这个问题:

private async void DisplayToggleDialog()
{
   ContentDialog toggleDialog = new ContentDialog
   {
      Title = "You've Toggled the Item",
              Content = "The Item is On",
              CloseButtonText = "Ok"
   };

   ContentDialogResult result = await toggleDialog.ShowAsync();
}

在服务器2016上失败

然后我在Server 2016上构建并运行代码,运行Visual Studio 2017 v15.5.0 Preview 4.0,当ContentDialog实例化时,我收到以下错误:

cannot cast type to icontentdialog2

我更改了代码以使用PrimaryButtonText代替CloseButtonText(似乎不再在UWP下定义)并且它有效。

private async void DisplayToggleDialog()
{
   ContentDialog toggleDialog = new ContentDialog
   {
      Title = "You've Toggled the Item",
              Content = "The Item is On",
              PrimaryButtonText = "Ok"
   };

   ContentDialogResult result = await toggleDialog.ShowAsync();
}