3

Am building a new App in .Net Maui using VS 2022 Preview. I am using CommunityToolkit.Mvvm

I have an entry field. When this field loses focus, I want to trigger a command.

My xaml is :

  <Entry Margin="10,0,10,0" 
                  
                   MaxLength="20"
                  
                   IsEnabled="{Binding IsNotBusy}"
                   PlaceholderColor="Red"
                   >
            <Entry.Behaviors>
                <toolkit:EventToCommandBehavior EventName="Unfocused" Command="{Binding GetDetailsCommand}"></toolkit:EventToCommandBehavior>
            </Entry.Behaviors>

Everything compiles and runs. But when the entry field loses focus I get a System.InvalidCastException.

What am I doing wrong?

Could this be because I am using the sourcegenerators in the mvvm toolkit?

[ICommand]
public async void GetDetails() 

Kindly help.

Edit : I noticed that source generator generates IAsyncRelayCommand and not an ICommand object. That is what prompted me to ask the above question.

MethodToChaos
  • 335
  • 2
  • 17

1 Answers1

3

Actually, THAT is exactly the case. I changed to implementing a normal Command instead of IAsyncRelayCommand generated by source generators and everything works.

I went through various blogs and YouTube videos and realised that there is not much stuff available on "Unfocused" event and its EventToCommand behaviour. That got me thinking that it is ME doing something wrong because the problem must be trivial. Wonder why it was not pointed out earlier.

My recommendation? Please follow what is already there and established, when working with .Net MAUI GA instead of jumping ahead and trying to implement the latest stuff after watching videos about new features. Spent one entire day hunting for a solution. Actually, was so frustrated at one point that started thinking about shifting to Flutter, believing that .Net MAUI is unstable.

MethodToChaos
  • 335
  • 2
  • 17
  • What's the platform that gives you the problem? I tried creating a Windows app and no errors occurred. I'm using CommunityToolit.Mvvm 8.0.0 preview 4 – Riccardo Minato Jul 01 '22 at 13:30
  • .Net MAUI. VS2022 Preview. I first used the same preview version of MVVM Toolkit as you. It had [RelayCommand] as source generator attribute. So I downgraded to MVVM Toolkit stable version which has [ICommand]. (Not documented, of course]. Only when I removed the attribute and declared the command as a variable in my view model, it started working. – MethodToChaos Jul 02 '22 at 05:28
  • By the way, in the stable version of mvvm Toolkit, how do I use canexecute when i use the source generator through ICommand attribute? Would be a great help if you guided me. – MethodToChaos Jul 02 '22 at 05:35
  • Sorry for the delay. In my comment I meant the target platform (Windows, Android, iOS...). If it's not documented I wouldn't rely on it. In preview 4 `RelayCommand` attribute seems to work. How would you like to use `canExecute`? If you simply need to call it, well, call it :) You can refer to the autogenerated command and call the method. – Riccardo Minato Jul 04 '22 at 09:17
  • Android. Thanks very much, Ricardo. – MethodToChaos Jul 05 '22 at 11:13
  • Could you please mark it as an accepted answer? This will help more people which meet the same problem. – Liyun Zhang - MSFT Jul 22 '22 at 08:37