【问题标题】:Compiled bindings in Xamarin FormsXamarin Forms 中的编译绑定
【发布时间】:2020-11-05 10:40:42
【问题描述】:

如果我有一个 BindingContext 设置为我的 ViewModel 的视图,如下所示:

<ContentPage ...>

<ContentPage.BindingContext>
    <vm:AddParkingSpotViewModel />
</ContentPage.BindingContext>

然后我将我的 Entry 值绑定到这样的属性:

<Entry Text="{Binding Address}" .../>

这足以让它使用编译绑定吗?还是我也必须在布局元素上设置x:DataType

<Grid x:DataType="vm:AddParkingSpotViewModel">

【问题讨论】:

    标签: xaml xamarin xamarin.forms data-binding


    【解决方案1】:

    这足以让它使用编译绑定吗?还是我还必须在布局元素上设置 x:DataType?

    使用编译绑定的过程是:

    启用 XAML 编译。有关 XAML 编译的更多信息。

    将 VisualElement 上的 x:DataType 属性设置为 VisualElement 及其子项将绑定到的对象的类型。

    绑定表达式仅针对定义了 x:DataType 属性的视图层次结构进行编译。相反,层次结构中未定义 x:DataType 属性的任何视图都将使用经典绑定。

    所以你的代码是经典绑定,下面的代码是编译绑定:

     <ContentPage.Content>
        <Grid x:DataType="vm:AddParkingSpotViewModel">
            <Grid.BindingContext>
                <vm:AddParkingSpotViewModel />
            </Grid.BindingContext>
            <Entry Text="{Binding Address}" />
        </Grid>
    </ContentPage.Content>
    

    有关已编译绑定的更多详细信息,请查看:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-09
      • 1970-01-01
      • 1970-01-01
      • 2018-03-01
      相关资源
      最近更新 更多