Xaml CloseButtonText providing InvalidCastException

I have a basic UAP app to run on a Windows 10 Mobile Enterprise device. However, when I try to open even the simplest of ContentDialogs, I get InvalidCastException when I try to set it CloseButtonText

 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 create object of type 'Windows.UI.Xaml.Controls.ContentDialog' for input 'Windows.UI.Xaml.Controls.IContentDialog2'.

System.InvalidCastException: The specified listing is not valid. at System.StubHelpers.StubHelpers.GetCOMIPFromRCW_WinRT (Object objSrc, IntPtr pCPCMD, IntPtr & ppTarget) at Windows.UI.Xaml.Controls.ContentDialog.put_CloseoveButtonText (StringMain.disc.) stack from the previous location where the exception was thrown - at System.Runtime.CompilerServices.AsyncMethodBuilderCore <. > C.b__6_0 (State Object) in System.Threading.WinRTSynchronizationContext.Invoker.InvokeCore ()

Win 10 minimum and target platforms are set to 10240 and 15063 respectively. CloseButtonText was introduced in 1703.

+3


source to share


3 answers


try it

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();

      



Or using 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


source


Just use SecondaryButtonText

instead CloseButtonText

.

Or go to Project Properties

and set the Min version in the Application tab to Windows 10 Creators Update (10.0; Build 15063)

. But this will make the app unavailable for older versions of Windows 10.



PS I would appreciate it if Microsoft starts thinking about adding a compile-time check for such cases instead of throwing a completely obscure exception at runtime.

+1


source


Works on Windows 10

I got this SO question after successfully running the following code on a Win10 window:

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();
}

      

Server 2016 crash

Then I built and ran the code on Server 2016 running Visual Studio 2017 v15.5.0 Preview 4.0 and when ContentDialog gets an instance I get the following error:

cannot cast type in icontentdialog2

I changed the code to use PrimaryButtonText instead of CloseButtonText (which seems to be no longer defined under UWP) and it worked.

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();
}

      

0


source







All Articles