maui icon indicating copy to clipboard operation
maui copied to clipboard

ToolbarItem property not updated when PropertyChanged is triggered in the ViewModel

Open inimirpaz opened this issue 4 months ago • 5 comments

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

inimirpaz avatar Apr 22 '24 14:04 inimirpaz

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 Binding

XamlTest avatar Apr 23 '24 02:04 XamlTest

@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

kubaflo avatar May 05 '24 13:05 kubaflo

@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 avatar May 06 '24 13:05 inimirpaz

@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

kubaflo avatar May 06 '24 14:05 kubaflo

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

inimirpaz avatar May 07 '24 09:05 inimirpaz