Xamarin形式数据绑定“。分隔器 [英] Xamarin Forms Databinding "." separator
问题描述
IsVisible ={Binding Path = UserContext.IsLoggedOut}
是 - 该属性绑定到视图模型的子对象。现在在Xamarin Forms中不支持这一点,或者我没有一个窍门。
如果Xamarin中不支持这一点,WPF支持哪些为了传播嵌套对象,我们应该做什么?平面视图模型正在使我写出令人难以置信的代码。
任何帮助赞赏。
支持嵌套属性,像其他相当复杂的表达式一样:
您可以测试它:
Xaml
< StackLayout间距=20>
< StackLayout Orientation =Horizontal>
< Label Text =Subproperties:HorizontalOptions =FillAndExpandFontSize =15>< / Label>
< Label Text ={Binding Item.SubItem.Text}HorizontalOptions =FillAndExpandFontSize =15>< / Label>
< / StackLayout>
< StackLayout Orientation =Horizontal>
< Label Text =Indexer:HorizontalOptions =FillAndExpandFontSize =15>< / Label>
< Label Text ={Binding Item.Dictionary [key] .Text}HorizontalOptions =FillAndExpandFontSize =15>< / Label>
< / StackLayout>
< StackLayout Orientation =Horizontal>
< Label Text =Array Indexer:HorizontalOptions =FillAndExpandFontSize =15>< / Label>
< Label Text ={Binding Item.Array [1] .Text}HorizontalOptions =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屋!