【问题标题】:Xaml CloseButtonText giving InvalidCastExceptionXaml CloseButtonText 给出 InvalidCastException
【发布时间】:2017-11-21 03:43:32
【问题描述】:

我有一个在 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();
    }

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

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

最低和目标 Win 10 平台分别设置为 10240 和 15063。 CloseButtonText 是在 1703 年引入的。

【问题讨论】:

  • 我设置了最小和目标Win 10平台分别设置为10240和15063,没有例外。顺便说一句,我的 Windows 10 移动操作系统版本是 10.15063.297。如果您将其部署在 Windows 10 Mobile 上,您应该能够检查您手机的操作系统版本。
  • 我在运行 Windows 10 Mobile Enterprise 10.0.10586.218 的设备上进行测试
  • CloseButtonText 是在 Windows 10 Creators Update(引入 v10.0.15063.0)中添加的,我们无法在 Windows 10 Mobile Enterprise 10.0.10586.218 中使用它。它将抛出 InvalidCastException。
  • 我在我的 Xamarin.Forms UWP 自定义渲染器中遇到了一个非常相似的问题,其中显示无法将控件(类型 TextBlock)转换为 ITextBlock5。在这里发布问题:stackoverflow.com/questions/47697540/…

标签: c# xaml uwp


【解决方案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();

【讨论】:

  • 我不想使用 MessageDialog 因为那将被弃用;此外,仅使用 PrimaryButtonText 之类的作品(它会切断按钮,所以我看不到它),我也可以使用 SecondaryButtonText;但是为什么 CloseButtonText 会抛出异常呢?
  • @monty 因为根据文档,ContentDialog 没有属性“CloseButtonText”:docs.microsoft.com/en-us/uwp/api/…
  • @TheTanic 这不是继承的属性,但它在您提供的 api 链接中列为属性。我用来测试的代码直接取自该页面。
【解决方案2】:

只需使用SecondaryButtonText 而不是CloseButtonText

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

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

【讨论】:

    【解决方案3】:

    在 Windows 10 上工作

    在 Win10 机器上成功运行以下代码后,我遇到了这个 SO 问题:

    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 被实例化时,我收到以下错误:

    我更改了代码以使用 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();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-19
      • 2020-04-18
      • 1970-01-01
      • 1970-01-01
      • 2012-04-13
      相关资源
      最近更新 更多