Comments (9)
@drasticactions Thanks! In my sample I did forget to set BindingContext
, I didn't actually forget this in my actual project, the main problem was that I forgot to add [NotifyCanExecuteChangedFor(nameof(DoWorkCommand))]
.
from maui.
I'm glad you solved it! I'm also pleased that NotifyCanExecuteChangedFor
exists; since my solution was kind of janky, that one makes more sense.
from maui.
Hi I'm an AI powered bot that finds similar issues based off the issue title.
Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it. Thank you!
Closed similar issues:
- Button Command CanExecute does not update the button visual on Windows (#6297), similarity score: 0.79
- Windows: Command ChangeCanExecute does not re-enable a disabled button (#7690), similarity score: 0.78
- Android-.NET6-Style issue when changing the button status (disabled->enabled) (#11336), similarity score: 0.76
- Setting 'CanExecute' doesn't change the status of Button controls when they are bound to MVVM Commands (#9753), similarity score: 0.76
- IsEnabled is unable to update visual appearance (#13971), similarity score: 0.75
Note: You can give me feedback by thumbs upping or thumbs downing this comment.
from maui.
Can repro this issue at Android platform on the latest 17.11.0 Preview 1.0(8.0.7/8.0.10/8.0.21).
BindingBreaksStyle.zip
from maui.
This looks related: #9753
from maui.
This looks related: #9753
Not quite the same, #9753 case is that the visual of the button has not changed, but in my case the button cannot be enabled/disabled based on the CanExecute change.
from maui.
FWIW I was able to get it working just fine
https://github.com/drasticactions/MauiRepros/tree/main/MvvmTestCanExecute
It may be that you didn't set up your model correctly? Maybe the library you are using is at fault? If you make a sample showing what you're doing that could help, or maybe run what I wrote above and see if it doesn't work for you?
from maui.
@drasticactions Hi, I made a sample, I use Mvvm Toolkit, and the [RelayCommand]
feature.
public partial class MainViewModel : ObservableObject
{
[ObservableProperty]
private bool _canDoWork; // binding to Switch button
[RelayCommand(CanExecute = nameof(CanDoWork))]
private static void DoWork() // binding to 'Click me' button
{
Debug.WriteLine("DoWork");
}
}
from maui.
Thank you for the sample! I believe you set up your view model incorrectly.
First, in your app page, your View Model isn't set as the BindingContext, it's null, so it will never work.
Then, in your ViewModel,
public partial class MainViewModel : ObservableObject
{
[ObservableProperty]
private bool _canDoWork;
[RelayCommand(CanExecute = nameof(CanDoWork))]
private static void DoWork()
{
Debug.WriteLine("DoWork");
}
}
I am not well versed in the CommunityToolkit MVVM, but I believe you didn't set it up correctly. CanExecute gets called whenever someone calls for it to be raised, it's not done automatically. So when CanDoWork gets updated, you then need to raise the event on DoWork to be able to call CanExecute.
Changing it to this:
public partial class MainViewModel : ObservableObject
{
[ObservableProperty]
private bool _canDoWork;
partial void OnCanDoWorkChanged(bool value)
{
this.DoWorkCommand.NotifyCanExecuteChanged();
}
[RelayCommand(CanExecute = nameof(CanDoWork))]
private async void DoWork()
{
Debug.WriteLine("DoWork");
}
}
Allows it to work. Likewise, DoWork can't be static, or else you can't call on the command for NotifyCanExecuteChanged. I would encourage you to read their documentation for more about it, but I do not believe this is a MAUI bug.
from maui.
Related Issues (20)
- Error when trying to run myfirstapp on pixel device emulation HOT 4
- Error: Activity class {../MainActivity} does not exist. HOT 4
- NavigationBarColors from NavigationPage not changing on AppTheme changing HOT 3
- Maui MediaPicker.Default.CapturePhotoAsync fails under iOS with no error message HOT 1
- Memory leak in RadioButton ControlTemplate HOT 2
- RadioButton with ControlTemplate crashes on Android in the release mode HOT 3
- ToolbarItem font icon color not updating with AppThemeBinding HOT 2
- I wanna create my self predictor aviator HOT 2
- [NET9] JavaCast not currently working on NET9
- When in the Shell, HideSoftInputOnTapped does not work at Navigation.PushAsync(animated: false) in TapGestureRecognizer HOT 2
- Looking for AndroidManifest.xml in /Android folder instead of /Platforms/Android HOT 3
- App refused by Apple with error ITMS-90338: Non-public API usage HOT 11
- [BUG] Margin and Padding don't work in CollectionView HOT 3
- [Windows] [.NET 9.0 Preview 5] App icon in the taskbar is pixelated HOT 3
- Flyout text dissappears after locking the screen HOT 3
- FontImage not support woff2 format HOT 2
- Installing MAUI workloads on Linux fails on read-only filesystems such as Flatpak HOT 3
- Application Crash Due to Incorrect Use of <ReorderableItemsView> Instead of <CollectionView> for Reordering Items HOT 1
- Duplicate Default Style Behaves Strangely HOT 2
- Unable to trigger focus on Picker programatically HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from maui.