C#-Xaml CloseButtonText给出InvalidCastException

我有一个可在Windows 10移动企业版设备上运行的基本UAP应用程序.但是,当我尝试打开最基本的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();
    }

Unable to cast object of type ‘Windows.UI.Xaml.Controls.ContentDialog’
to type ‘Windows.UI.Xaml.Controls.IContentDialog2’.

System.InvalidCastException: Specified cast is not valid. at
System.StubHelpers.StubHelpers.GetCOMIPFromRCW_WinRT(Object objSrc,
IntPtr pCPCMD, IntPtr& ppTarget) at
Windows.UI.Xaml.Controls.ContentDialog.put_CloseButtonText(String
value) at
MyApplication.MainPage.d__4.MoveNext()
— End of stack trace from previous location where exception was thrown — at
System.Runtime.CompilerServices.AsyncMethodBuilderCore.<>c.b__6_0(Object
state) at
System.Threading.WinRTSynchronizationContext.Invoker.InvokeCore()

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

解决方法:

尝试这个

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();
上一篇:spring boot 配置多个数据源


下一篇:【MySQL 数据库 进阶】 面试官常问 索引+事务+JDBC