MAUI AppShell中FlyoutItemIsVisible绑定失败问题求助
解决MAUI Shell中FlyoutItemIsVisible绑定错误XFC0009
问题描述
需要动态显示/隐藏Shell菜单内容,FlyoutItemIsVisible是继承自BaseShellItem的可绑定属性,但绑定到ShellContent时出现错误:Error XFC0009 No property, BindableProperty, or event found for "FlyoutItemIsVisible", or mismatching type between value and property。BindingContext设置正常,ShellContent的Title能从同一ViewModel正常绑定,该问题在.NET7和.NET8 RC环境下均存在。
视图代码
<Shell x:Class="AppShell.AppShell" xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:AppShell" Shell.FlyoutBehavior="Flyout" Title="AppShell"> <ShellContent Title="{Binding HomeText}" FlyoutItemIsVisible="{Binding IsVisible}" ContentTemplate="{DataTemplate local:MainPage}" Route="MainPage" /> </Shell>
代码后置
public AppShell(AppShellViewModel vm) { BindingContext = vm; InitializeComponent(); }
ViewModel代码
namespace AppShell { public partial class AppShellViewModel : ObservableObject { public AppShellViewModel() { HomeText = "Home"; IsVisible = true; } [ObservableProperty] bool isVisible; [ObservableProperty] string homeText; } }
问题原因
FlyoutItemIsVisible是Shell类的附加属性,并非ShellContent的自有属性。直接写FlyoutItemIsVisible="{Binding IsVisible}"会让XAML解析器误认为这是ShellContent的属性,从而触发找不到属性的错误。
解决方案
修改ShellContent的属性绑定,为FlyoutItemIsVisible添加附加属性的所有者前缀Shell.:
<ShellContent Title="{Binding HomeText}" Shell.FlyoutItemIsVisible="{Binding IsVisible}" ContentTemplate="{DataTemplate local:MainPage}" Route="MainPage" />
修改后,XAML解析器会正确识别这是Shell的附加属性,绑定即可正常工作,实现根据ViewModel中IsVisible的值动态显示/隐藏Flyout中的ShellContent项。
内容的提问来源于stack exchange,提问作者greatgrayowl1
相关产品推荐
相关产品推荐

