-
Notifications
You must be signed in to change notification settings - Fork 1.4k
RelayCommand<T> throws NullReferenceException when T is a ValueType and CanExecute is used #3619
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hello sonnemaf, thank you for opening an issue with us! I have automatically added a "needs triage" label to help get things started. Our team will analyze and investigate the issue, and escalate it to the relevant team if possible. Other community members may also look into the issue and provide feedback 🙌 |
Thanks for the report Fons! This is something I was looking into and that had been reported by some devs in the Discord server as well. The issue is that the behavior is not really well defined in general when you're binding to a value type and the value is null. There are two possible solutions to this to avoid the null reference exception:
cc. @FrayxRulez since you mentioned this on Discord as well. I guess we should just decide on which behavior would be preferred in these cases 🙂 |
I would just return |
I'm a bit confused, what's In the example I'm assuming if I'd say returning So, I agree with @sonnemaf that returning |
@michael-hawker I can understand your confusion. This is just something which is badly designed in WPF and UWP. The CanExecute is called twice from the Page/Window constructor. The first time with The first one will cause the app to crash. I don't think we can influence how WPF or UWP works so we have to fix it ourselves. It doesn't really matter which solution you pick it will be |
Yeah that is indeed pretty annoying, my guess is that Fixed in 3f965b2. Side note, wonder if we should open an issue for WinUI 3 to fix this quirk? 🤔 |
@Sergio0694 great solution. This also works correct for a |
@Sergio0694 probably not a bad idea for posterity at least, though I'd tell you to open an identical one on the WPF repo as well. 🙂 |
Describe the bug
If you open a View (Page or Window) and you have DataBound the Command of a Button to a
RelayCommand<T>
whereT
is a ValueType and the command has a CanExecute action you get a NullReferenceException. Tested it in a UWP and in a WPF app. Both have this problem.Without the CanExecute it works ok. If
T
is a reference type it also works ok.Steps to Reproduce
Take this xaml
And this codebehind
Start the app using F5. You get the Exception. You don't get the Page/Window it happens from the constructor of the page/window.
This also happens in WPF apps, not only UWP.
Expected behavior
No Exception, just a working app.
Environment
Additional context
Add any other context about the problem here.
The text was updated successfully, but these errors were encountered: