【问题标题】:Xamarin Forms Databinding "." separatorXamarin 表单数据绑定“。”分隔器
【发布时间】:2016-08-27 09:45:41
【问题描述】:

我在 Xamarin Forms 中遇到数据绑定问题。这就是为什么,我期望从以下 XAML 语句中发生的事情:

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

Is - 属性绑定到 View Model 的子对象。现在,要么 Xamarin Forms 不支持这一点,要么我错过了一个技巧。

如果 Xamarin 不支持这一点,而 WPF 支持它,那么我们应该怎么做才能传播嵌套对象?扁平化视图模型让我写了一大堆不优雅的代码。

【问题讨论】:

  • UserContext 长什么样子?
  • UserContext 是 viewModel 类的一个属性。 UserContext 对象有一个名为 IsLoggedOut 的属性
  • 你能粘贴代码吗?
  • 要粘贴的代码太多,但我真的不明白为什么检查 UserContext 对象会有很大的不同。我认为这是 Xamarin.Forms 不支持数据绑定中的嵌套对象的问题,但我还没有向我确认这一点
  • 绝对支持嵌套绑定。这就是为什么我想看看你的代码。

标签: data-binding xamarin.forms


【解决方案1】:

支持嵌套属性,就像其他非常复杂的表达式一样:

你可以测试一下:

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>

页面

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";
}

结果

【讨论】:

  • 你说得对,一定和我的代码有关。会更新。
  • 好的,不确定到底是什么导致了这个问题,但我从头开始重新实现了一切,现在一切正常,哈哈。谢谢斯文-迈克尔!
  • 确保您的属性具有公共 getter 和 setter,否则您将神秘地看不到任何数据。至少双向绑定就是这种情况。我没有尝试将单向绑定映射到只有 getter 的属性,但我怀疑它可能会起作用。
  • 当ItemSubModel有子类是列表时如何进行数据绑定? '公共类 ItemSubModel { 公共字符串文本 { 获取;放; } public List ChildList{get;set;} }'
【解决方案2】:

我假设您正在尝试使用 Xaml。尝试删除“路径”。

IsVisible="{Binding UserContext.IsLoggedOut}"

不过更重要的是,您的BindingContext 是什么?要使上述代码正常工作,您需要将BindingContext 设置为类Foo,它有一个名为UserContext 的属性,它本身也有一个属性IsLoggedOut

也看看here

【讨论】:

  • BindingContext 在代码中设置为我的视图模型。 View Model 有一个名为 UserContext 的属性,它本身也有一个名为 IsLoggedOut 的属性。在 Xamarin Forms 中,这种传播似乎不起作用,也没有在我认为的任何地方记录。 AllDayer 你能让这种事情发挥作用吗?
猜你喜欢
  • 1970-01-01
  • 2018-09-25
  • 1970-01-01
  • 2016-09-08
  • 1970-01-01
  • 2016-06-26
  • 2021-04-18
  • 1970-01-01
  • 2018-02-16
相关资源
最近更新 更多