I have a WinUI 3 application. There is a simple custom dialog that extends ContentDialog.
XAML for the dialog
<StackPanel
Grid.Column="0"
Orientation="Vertical">
<RadioButton
x:Name="GeneralTabButton"
Content="{common:LocalizedUIString Uid=SettingsTab.General}"
GroupName="GROUP_Setting"
Command="{x:Bind VM.SelectGeneralSettingsCommand}"
Style="{StaticResource SettingsRadioButton}">
</RadioButton>
<RadioButton
x:Name="SettingsTabButton"
Content="{common:LocalizedUIString Uid=SettingsTab.Database}"
GroupName="GROUP_Setting"
Command="{x:Bind VM.SelectDatabaseSettingsCommand}"
Style="{StaticResource SettingsRadioButton}">
</RadioButton>
</StackPanel>
There is problem when I want to set IsChecked state of some radiobutton when showing the dialog. Sometimes it ends up in System.Runtime.InteropServices.COMException without any additional info. It seems very random. Sometimes it crashes on second showing, sometimes on tenth. It never crashes on the first showing.
I tried to set the state directly from the XAML, I tried to set the state in Loaded method (even through DispatcherQueue). Unloaded event of the dialog gets called everytime. I am using WindowsAppSDK 1.2.221209.1. Style of the radiobutton is pretty much just the microsoft style with different colors for radiobutton states.
Is there at least some way how can I pinpoint root of this problem?
EDIT:
I have x64 packaged WinUI3 application, latest version WindowsAppSDK has the same problem. I found out that I can fix this problem by removing the GroupName. These are minimal steps that can replicate this problem:
- Create new blank packaged WinUI3 app with Windows app packaging project
- Create new user control, change base class to ContentDialog and add new radio buttons
Xaml for this component
<ContentDialog
x:Class="RadioButtonGroupCrash.Dialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
IsPrimaryButtonEnabled="True"
IsSecondaryButtonEnabled="True"
PrimaryButtonText="Ok"
SecondaryButtonText="Cancel">
<StackPanel>
<RadioButton x:Name="rb1" GroupName="group">
</RadioButton>
<RadioButton x:Name="rb2" GroupName="group">
</RadioButton>
</StackPanel>
</ContentDialog>
Code behind
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
namespace RadioButtonGroupCrash
{
public sealed partial class Dialog : ContentDialog
{
public Dialog()
{
this.InitializeComponent();
Loaded += Dialog_Loaded;
}
private void Dialog_Loaded(object sender, RoutedEventArgs e)
{
rb1.IsChecked = true;
}
}
}
Show dialog in MainWindow code behind
private async void myButton_Click(object sender, RoutedEventArgs e) { await new Dialog() { XamlRoot = App.MainWindow.Content.XamlRoot }.ShowAsync(); }
Seems like a bug in the WinUI3 framework.