【问题标题】:Xamarin Forms FlyoutPage wtih Binding Problems具有绑定问题的 Xamarin 表单 FlyoutPage
【发布时间】:2021-11-13 17:31:34
【问题描述】:

我正在使用 FlyoutPage (https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/navigation/flyoutpage) 制作 Xamarin Forms 应用程序。

我制作了所有的页面和菜单按钮等

现在我想在菜单中使用绑定但找不到方法。

            <x:Array Type="{x:Type local:FlyoutPageItem}">
                <local:FlyoutPageItem Title="{Binding NewContacts}" IconSource="contacts.png" TargetType="{x:Type local:ContactsPage}" />
                <local:FlyoutPageItem Title="{Binding NewTodo}" IconSource="todo.png" TargetType="{x:Type local:TodoListPage}" />
                <local:FlyoutPageItem Title="{Binding NewReminder}" IconSource="reminders.png" TargetType="{x:Type local:ReminderPage}" />
            </x:Array>

但总是报错: XamlC 错误 XFC0009:找不到“标题”的属性、BindableProperty 或事件,或者值和属性之间的类型不匹配。

有什么帮助吗?提前致谢。

【问题讨论】:

  • 就像错误所说的那样,Title 不是可绑定的属性。 FlyoutPageItem 是示例中定义的本地类,因此您可以对其进行修改以适合您的目的
  • 正如json所说,你必须在模型中定义自定义属性,请参考类:github.com/xamarin/xamarin-forms-samples/blob/main/Navigation/…
  • 感谢您的帮助。我有类似示例的类,但无法在数组中绑定。
  • 你试过官方项目吗?
  • 不是整个项目。但是尝试了其中的很大一部分。我错过了什么吗?

标签: xamarin binding flyout


【解决方案1】:

出现问题是因为您试图将一个属性与另一个属性绑定,这是不正确的。

通常我们在 Targetsource 之间创建绑定。

目标是设置数据绑定的对象(和属性,定义为 BindableProperty)。

是数据绑定引用的对象(和属性)。

但是这里你试图将 source 绑定到另一个 source ,这违反了 mvvm 绑定的原则。

作为一个丑陋的解决方法,您可以如下定义FlyoutPageItem.Title

public static readonly BindableProperty TitleProperty = BindableProperty.Create("Title", typeof(string), typeof(FlyoutPageItem), "");
public string Title
{
    get { return (string)GetValue(TitleProperty); }
    set { SetValue(TitleProperty, value); }
}

并更改xaml中的绑定路径

 Title="Menu"
 x:Name="Self">

<x:Array Type="{x:Type res:FlyoutPageItem}">
    <res:FlyoutPageItem Title="{Binding Source={x:Reference Self.AnyString} ,Path=BindingContext}" IconSource="Icons/door_light.png" TargetType="{x:Type views:DoorPage}" />

但不建议这样做。

参考

https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/data-binding/basic-bindings.

https://docs.microsoft.com/en-us/xamarin/xamarin-forms/xaml/bindable-properties

【讨论】:

    【解决方案2】:

    解决了。请告诉我这是否政治正确。

    视图模型:

     public string CompanyTitle => "CompanyTitleString";
     public string ContactsTitle => "ContactsTitleString";
     public string AboutTitle => "AboutTitleString";
    
        public List<FlyoutPageItem> flyoutPageItems
        {
            get
            {
                return new List<FlyoutPageItem>
                    {
                        new FlyoutPageItem {
                            Title=CompanyTitle,
                            IconSource="company_light.png",
                            TargetType= typeof(CompanyPage)
                        },
                        new FlyoutPageItem {
                            Title=ContactsTitle,
                            IconSource="contacts_light.png",
                            TargetType= typeof(ContactsPage)
                        },
                        new FlyoutPageItem {
                            Title=AboutTitle,
                            IconSource="about_light.png",
                            TargetType= typeof(AboutPage)
                        }
                    };
            }
        }
    

    XAML:

            <ListView x:Name="listView" x:FieldModifier="public" SeparatorVisibility="None" ItemsSource="{Binding flyoutPageItems}">
    

    编辑:更新了 GitHub

    【讨论】:

      猜你喜欢
      • 2018-10-20
      • 2020-03-09
      • 2016-03-09
      • 2020-05-29
      • 2019-01-07
      • 1970-01-01
      • 1970-01-01
      • 2015-11-05
      • 2018-11-15
      相关资源
      最近更新 更多