CommunityToolkit / MVVM-Samples

Sample repo for MVVM package
Other
1.14k stars 221 forks source link

Handling an IAsyncRelayCommand in WinUI 3.0 #57

Open natecheadle opened 2 years ago

natecheadle commented 2 years ago

in issue #5 it mentions that the UWP example can be used to also do WinUI 3.0 by using CommunityToolkit.MVVM.

In the example for handling an IAsyncRelayCommand looks to be using Microsft.Toolkit.Uwp.UI.Converters in AsyncRelayCommandPage.xaml to get the task result.

My question is how should one get a task result in WinUI 3.0? I would guess there is a non-UWP specific converter that should be used, but I am not sure what it is and haven't been able to find it.

natecheadle commented 2 years ago

If I change the converter in the page to xmlns:converters="using:CommunityToolkit.WinUI.UI.Converters"

and install nuget packages for CommunityToolkit.WinUI.UI and CommunityToolkit.WinUI the application crashes if I try to use the converter. The first exception thrown is a System.InvalidCastException.

If I remove references to the converter ex. xmlns:converters="using:CommunityToolkit.WinUI.UI.Converters" and <Run Text="{x:Bind ViewModel.DownloadTextCommand.ExecutionTask, Converter={StaticResource TaskResultConverter}, Mode=OneWay}"/>

I can run the application without issue with WinUI 3.

runceel commented 1 year ago

@natecheadle As I researched, CommunityToolkit.WinUI package and WinUI.UI package looks like don't have TaskResultConverter class. So you want to use it, you have to implement it yourself.

Fortunately, TaskResultConverter is a really simple class. I have just implemented it. And it worked fine.

Please see below repository:

https://github.com/runceel/MvvmToolkitSample.WinUI.AsyncRelayCommand