RelayCommand<T> does not work with Enumerations

Abierto
#1,048 1 comentario 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
42/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Estancado
Stack tecnológico
csharp
Área
desktop

Línea de trabajo

Comienza rastreando RelayCommand y el SetSortOrderCommand generado a partir de la reproducción UWP proporcionada. Reproduce el fallo con el enum SortOrder y su CommandParameter basado en uint, y verifica después que los parámetros enum lleguen correctamente a CanExecute y a la ejecución. El issue estará terminado cuando el escenario de binding notificado funcione sin la discrepancia entre uint y SortOrder.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

bug :bug:
Describe the bug

If you try to bind an enum to a CommandParameter in UWP, then UWP tends to pass them around the system as the raw type of the enum as opposed to the .net type.

This means that when you bind to it and it fires the CanExecute(object?), that it fails because the type does not match (uint != SortOrder)

Regression

No response

Steps to reproduce

I've provided a bare-bones reproduction here:
https://github.com/[AtomicBlom/EnumRelayCommandTest](https://github.com/AtomicBlom/EnumRelayCommandTest)

But if you create a blank app, and add a ViewModel based on ObservableObject, something like this:

public partial class MainPageViewModel : ObservableObject
	{
		[RelayCommand]
		private void OnSetSortOrder(SortOrder order)
		{
			Debug.WriteLine($"Sort order set to {order}");
		}
	}

	public enum SortOrder : uint
	{
		Ascending,
		Descending
	}

Then attempt to bind to it.

		<Button Command="{x:Bind ViewModel.SetSortOrderCommand, Mode=OneWay}" Content="Press me">
			<Button.CommandParameter>
				<local:SortOrder>Ascending</local:SortOrder>
			</Button.CommandParameter>
		</Button>

You'll find that it fails

Expected behavior

You should be able to pass an enum as a CommandParameter

Screenshots

No response

IDE and version

VS 2022

IDE version

17.12.4

Nuget packages
  • CommunityToolkit.Common
  • CommunityToolkit.Diagnostics
  • CommunityToolkit.HighPerformance
  • CommunityToolkit.Mvvm (aka MVVM Toolkit)
Nuget package version(s)

8.4.0

Additional context

This error was also observed while attempting to port a UWP application to .net9

Help us help you

Yes, I'd like to be assigned to work on this item

Lenguaje dominante
C#
Estrellas
3.8k
Forks
402
Métricas de merge de PR
Sin PR fusionados en 30 d

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de CommunityToolkit/dotnet

Todos los issues de CommunityToolkit/dotnet

Issues similares

Más issues de C#

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.