NotifyCanExecuteChangedFor
generated code raises exception
#379
-
If I add a I start with a simple public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new MainWindowVM();
}
} and [INotifyPropertyChanged]
public partial class MainWindowVM
{
public MainWindowVM()
{
System.Timers.Timer timer = new System.Timers.Timer(1000);
timer.Elapsed += Timer_Elapsed;
timer.AutoReset = true;
timer.Start();
}
private void Timer_Elapsed(object? sender, System.Timers.ElapsedEventArgs e)
{
Counter = (Counter + 200)%3000;
}
[ObservableProperty]
[NotifyCanExecuteChangedFor(nameof(DoThingCommand))]
private int counter;
[RelayCommand(CanExecute = nameof(CanRunDoThing), IncludeCancelCommand = true)]
private async Task DoThingAsync(int? time, CancellationToken token = default)
{
try
{
await Task.Delay(time.GetValueOrDefault(), token);
}
catch (OperationCanceledException) { }
}
private bool CanRunDoThing(int? time) => true;
} The generated code looks like (the comments are mine): [global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "8.0.0.0")]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public int Counter
{
get => counter;
set
{
if (!global::System.Collections.Generic.EqualityComparer<int>.Default.Equals(counter, value))
{
OnCounterChanging(value);
counter = value;
OnCounterChanged(value);
// this call runs fine
OnPropertyChanged(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangedArgs.Counter);
// !! exception occurs here !!
DoThingCommand.NotifyCanExecuteChanged();
}
}
} I'm guessing this is occurring because the The Thank you very much for any thoughts/help. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
Raising the event itself doesn't care about the thread, what matters is if there's anything on the UI that's subscribed to that event and doing something. I'd assume that is crashing for you because you have a button or something bound to that command, but not the the other property. If you tried to also bind something to that property, you should see the same crash as well. Either way this is not an issue with the MVVM Toolkit itself 🙂 |
Beta Was this translation helpful? Give feedback.
-
Hi Sergio (or anyone) I am back to trying to use the ToolKit and would like to ask another question on this same topic. If i have a property on a ViewModel implementing However, If I have a command which updates its It really seems like the commands behave differently in this regard form property changes. Is this really the intended behavior? Why aren't the command state changes sent to the dispatcher behind the scenes like the property change notifications? Thank you for any feedback/guidance. |
Beta Was this translation helpful? Give feedback.
-
I'm seeing this too, but in my case I think it's an issue with .NET MAUI as rightly, the MVVM library shouldn't care about threading. The UI framework however, should deal with such bindings safely. |
Beta Was this translation helpful? Give feedback.
Raising the event itself doesn't care about the thread, what matters is if there's anything on the UI that's subscribed to that event and doing something. I'd assume that is crashing for you because you have a button or something bound to that command, but not the the other property. If you tried to also bind something to that property, you should see the same crash as well. Either way this is not an issue with the MVVM Toolkit itself 🙂