XAML CommandParameter
and enum
values
#407
-
Feel like this may be a bug (or oversight), but I'm also not sure how to work around it... Background: Came about as I was taking the WTS template for Settings Page which uses the public enum ElementTheme
{
Default = 0,
Light = 1,
Dark = 2
} This is exposed in a command in the view model like so: [RelayCommand]
public async Task SwitchTheme(MyLib.ElementTheme newTheme)
{
ElementTheme = newTheme;
await ThemeSelectorService.SetThemeAsync(newTheme);
} And then used directly in XAML here in the application layer: <RadioButton
x:Uid="Settings_Theme_Light"
Command="{x:Bind ViewModel.SwitchThemeCommand}"
IsChecked="{x:Bind ViewModel.ElementTheme, Converter={StaticResource EnumToBooleanConverter}, ConverterParameter=Light, Mode=OneWay}"
FontSize="15"
GroupName="AppTheme">
<RadioButton.CommandParameter>
<models:ElementTheme>Light</models:ElementTheme> <!-- This used to be xaml: in the original WTS version as it directly used the XAML based enum version -->
</RadioButton.CommandParameter>
</RadioButton> However, it seems like the enum is being converted by XAML or WinRT to an Int32 between the layers, and now I'm getting an exception from the
dotnet/CommunityToolkit.Mvvm/Input/RelayCommand{T}.cs Lines 167 to 170 in 9d1a525 Being called from here L271: dotnet/CommunityToolkit.Mvvm/Input/AsyncRelayCommand{T}.cs Lines 263 to 271 in 9d1a525 It's happening when the command is getting set by the generated xaml from the binding public static void Set_Windows_UI_Xaml_Controls_Primitives_ButtonBase_Command(global::Windows.UI.Xaml.Controls.Primitives.ButtonBase obj, global::System.Windows.Input.ICommand value, string targetNullValue)
{
if (value == null && targetNullValue != null)
{
value = (global::System.Windows.Input.ICommand) global::Windows.UI.Xaml.Markup.XamlBindingHelper.ConvertValue(typeof(global::System.Windows.Input.ICommand), targetNullValue);
}
obj.Command = value;
} Which somehow triggers a private bool CanExecute(object parameter)
{
ICommand _this = Unsafe.As<ICommand>(this);
return _this.CanExecute(parameter);
} Call stack from there:
🤷♂️ |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Quickly tried using [RelayCommand]
public async Task SwitchTheme(int newTheme)
{
ElementTheme = (MyLib.ElementTheme)newTheme;
await ThemeSelectorService.SetThemeAsync(ElementTheme);
} This feels dirty, but works for now. Would be nice to keep strong typing through-out though. |
Beta Was this translation helpful? Give feedback.
-
This seems related to this platform issue filed here: microsoft/microsoft-ui-xaml#7633 |
Beta Was this translation helpful? Give feedback.
I have confirmed this is indeed a platform bug with UWP/XAML. I wrote a simple MarkupExtension and it provided the value and passed it correctly. I've posted that solution in that thread here: microsoft/microsoft-ui-xaml#7633 (comment)