给出InvalidCastException的Xaml CloseButtonText

问题描述 投票:3回答:3

我有一个基本的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:指定的强制转换无效。 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()--- stack of end从抛出异常的先前位置追踪---在System.Threading.WinRTSynchronizationContext.Invoker.InvokeCore()上的System.Runtime.CompilerServices.AsyncMethodBuilderCore。<> c.b__6_0(对象状态)

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

c# xaml uwp
3个回答
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();

2
投票

只需使用SecondaryButtonText而不是CloseButtonText

或者转到Project Properties并在应用程序选项卡上将最小版本设置为Windows 10 Creators Update (10.0; Build 15063)。但这将使旧版Windows 10无法使用该应用。

附:如果微软开始考虑为这种情况添加编译时检查而不是在运行时抛出完全不清楚的异常,我将不胜感激。


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();
}
© www.soinside.com 2019 - 2024. All rights reserved.