Xamarin形式数据绑定“。分隔器 [英] Xamarin Forms Databinding "." separator

查看:221
本文介绍了Xamarin形式数据绑定“。分隔器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Xamarin表单中处理数据绑定。这就是为什么我从以下Xaml语句中发生的事情:

  IsVisible ={Binding Path = UserContext.IsLoggedOut} 

是 - 该属性绑定到视图模型的子对象。现在在Xamarin Forms中不支持这一点,或者我没有一个窍门。



如果Xamarin中不支持这一点,WPF支持哪些为了传播嵌套对象,我们应该做什么?平面视图模型正在使我写出令人难以置信的代码。



任何帮助赞赏。

解决方案

支持嵌套属性,像其他相当复杂的表达式一样:



您可以测试它:



Xaml

 < StackLayout间距=20> 
< StackLayout Orientation =Horizo​​ntal>
< Label Text =Subproperties:Horizo​​ntalOptions =FillAndExpandFontSize =15>< / Label>
< Label Text ={Binding Item.SubItem.Text}Horizo​​ntalOptions =FillAndExpandFontSize =15>< / Label>
< / StackLayout>
< StackLayout Orientation =Horizo​​ntal>
< Label Text =Indexer:Horizo​​ntalOptions =FillAndExpandFontSize =15>< / Label>
< Label Text ={Binding Item.Dictionary [key] .Text}Horizo​​ntalOptions =FillAndExpandFontSize =15>< / Label>
< / StackLayout>
< StackLayout Orientation =Horizo​​ntal>
< Label Text =Array Indexer:Horizo​​ntalOptions =FillAndExpandFontSize =15>< / Label>
< Label Text ={Binding Item.Array [1] .Text}Horizo​​ntalOptions =FillAndExpandFontSize =15>< / Label>
< / StackLayout>
< / StackLayout>



< pre class =lang-cs prettyprint-override> public partial class Page2:ContentPage
{
public ItemModel Item {get; }

public Page2()
{
InitializeComponent();
Item = new ItemModel();
BindingContext = this;

}
}

public class ItemModel
{
public ItemSubModel SubItem {get;组; }
public Dictionary< string,ItemSubModel>字典{get;组; }
public ItemSubModel [] Array {get;组;

public ItemModel()
{
SubItem = new ItemSubModel();
Dictionary =新词典< string,ItemSubModel>
{
{key,new ItemSubModel()}
};
Array = new [] {new ItemSubModel(),new ItemSubModel()};
}
}

public class ItemSubModel
{
public string Text {get;组; } =支持;
}

结果




I'm struggling with databinding in Xamarin Forms. Here's why, what I expect to happen from the following Xaml statement:

IsVisible="{Binding Path=UserContext.IsLoggedOut}"

Is - That the property is bound to a child object of the View Model. Now either this isn't supported in Xamarin Forms or I am missing a trick.

If this is not supported in Xamarin, where it is supported in WPF, then what are we supposed to do in order to propagate nested objects? Flattening the View Model is making me write reams and reams of inelegant code.

Any help appreciated.

解决方案

Nested properties are supported, like other pretty complex expression as well:

You can test it:

Xaml

<StackLayout Spacing="20">
  <StackLayout Orientation="Horizontal">
    <Label Text="Subproperties: " HorizontalOptions="FillAndExpand" FontSize="15"></Label>
    <Label Text="{Binding Item.SubItem.Text}" HorizontalOptions="FillAndExpand" FontSize="15"></Label>
  </StackLayout>
  <StackLayout Orientation="Horizontal">
    <Label Text="Indexer: " HorizontalOptions="FillAndExpand" FontSize="15"></Label>
    <Label Text="{Binding Item.Dictionary[key].Text}" HorizontalOptions="FillAndExpand" FontSize="15"></Label>
  </StackLayout>
  <StackLayout Orientation="Horizontal">
    <Label Text="Array Indexer: " HorizontalOptions="FillAndExpand" FontSize="15"></Label>
    <Label Text="{Binding Item.Array[1].Text}" HorizontalOptions="FillAndExpand" FontSize="15"></Label>
  </StackLayout>
</StackLayout>

Page

public partial class Page2 : ContentPage
{
    public ItemModel Item { get; }

    public Page2()
    {
        InitializeComponent();
        Item = new ItemModel();
        BindingContext = this;

    }
}

public class ItemModel
{
    public ItemSubModel SubItem { get; set; }
    public Dictionary<string, ItemSubModel>  Dictionary { get; set; }
    public ItemSubModel[] Array { get; set; }

    public ItemModel()
    {
        SubItem = new ItemSubModel();
        Dictionary = new Dictionary<string, ItemSubModel>
        {
            {"key", new ItemSubModel()}
        };
        Array = new [] {new ItemSubModel(), new ItemSubModel() };
    }
}

public class ItemSubModel
{
    public string Text { get; set; } = "Supported";
}

Result

这篇关于Xamarin形式数据绑定“。分隔器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆