maui
maui copied to clipboard
ToolbarItem property not updated when PropertyChanged is triggered in the ViewModel
Description
(see repro) I have some legacy Xamarin code handling the toolbar item visibility. It used to work in Xamarin, it does not in .NET MAUI. It seems that a property of a ToolbarItem when is bound to a property in the ViewModel, the notification of its change is not propagated properly. What I found is that if you bind the property to, for instance, the code behind of the page itself (via x:Reference and referencing the x:Name of the page), it DOES work.
Steps to Reproduce
No response
Link to public reproduction project repository
https://github.com/inimirpaz/maui_issues
Version with bug
8.0.20 SR4
Is this a regression from previous behavior?
Yes, this used to work in Xamarin.Forms
Last version that worked well
Unknown/Other
Affected platforms
iOS, Android
Affected platform versions
No response
Did you find any workaround?
No response
Relevant log output
No response
Verified on VS 17.10.0 Preview 4.0(8.0.20). Repro on Android 14.0-API34 and iOS 17.2 with below Project:
ToolbarItemBindingIssue.zip
@inimirpaz if you remove this line https://github.com/inimirpaz/maui_issues/blob/698f750c1559579f2797d75fe2bc84762873fc16/ToolbarItemBindingIssue/ToolbarItemExtended.cs#L9C13-L12C16
and add this to onIsVisibleChanged
else if (!(bool)newvalue && items.Contains(item))
{
var parent = item.Parent;
items.Remove(item);
item.Parent = parent;
}
then it should work
@inimirpaz if you remove this line https://github.com/inimirpaz/maui_issues/blob/698f750c1559579f2797d75fe2bc84762873fc16/ToolbarItemBindingIssue/ToolbarItemExtended.cs#L9C13-L12C16
and add this to onIsVisibleChanged
else if (!(bool)newvalue && items.Contains(item)) { var parent = item.Parent; items.Remove(item); item.Parent = parent; }
then it should work
You are correct, it works. It works even when the task is not removed (it was some sort of workaround I guess I could try to get rid of it now).
Why in .NET MAUI is it necessary to re-assign the parent though?
@inimirpaz because when an item is removed its parent is set to null - probably to fix possible memory leaks. A parent wasn't set to null in Xamarin Forms
Hello @kubaflo ,
I have a related issue now... Both by using the standard Microsoft.Maui.Controls.Command
and the AsyncCommand
(by James Montemagno), whenever I specify the canExecute condition, after the ToolbarItem is removed-then-readded it is disabled regardless of the canExecute being set to true. (it was not the case with Xamarin.Forms)
Repro branch: https://github.com/inimirpaz/maui_issues/tree/issue_with_enabled