RelayCommand<T> does not work with Enumerations

オープン
#1,048 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
42/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
停滞
技術スタック
csharp
領域
desktop

調査の方向性

提供された UWP の再現コードをもとに、まず RelayCommand と生成された SetSortOrderCommand を追跡します。SortOrder enum と uint ベースの CommandParameter で失敗を再現し、その後、enum パラメーターが CanExecute と実行に正しく渡ることを確認します。報告された binding シナリオが uint と SortOrder の不一致なしに動作すれば、Issue は完了です。

索引モデルが issue の本文から書いたものです。

説明

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

主要言語
C#
スター
3.8k
フォーク
402
PR マージ指標
30日以内にマージされた PR はありません

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

CommunityToolkit/dotnet のほかの issue

CommunityToolkit/dotnet の issue をすべて見る

似ている issue

C# の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。