0

我正在用 Bindings 和 x:Bind 做一些测试,我发现了一些有趣的事情。

XAML

<ListView Background="White" ItemsSource="{x:Bind ViewModel.CurrenciesViewModel.Currencies.Currencies}">
            <ListView.ItemTemplate>
                <DataTemplate x:DataType="model:Currency">
                    <StackPanel>
                        <TextBlock Foreground="Red" Text="1"/>
                        <TextBlock Foreground="Red" Text="{x:Bind Name}"/>
                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
</ListView>

好的,所以首先 x:Bind 是 viewModel,它具有属性 CurrenciesViewModel(模型名称 CurrencyListViewModel),它具有属性 Currencies(模型名称 CurrencyList),它具有属性 Currencies,它是 ObservableColletion

所以当我添加这样的对象时这是有效的

CurrenciesViewModel.Currencies.Currencies.Add(new Currency() { Name = "test" });
CurrenciesViewModel.Currencies.Currencies.Add(new Currency() { Name = "test2" });

但是,当我通过我的 DataProvider(简单的 xml 休息)用一种方法下载我的模型时

Task<CurrencyList> GetCurrencyList();

所以我在主 ViewModel 中的第一个属性 CurrencyViewModel 是这样的

CurrenciesViewModel = new CurrencyListViewModel(await DataProvider.GetCurrencyList());

然后我的 listView 是空的!我检查了我的数据是否已下载,是的……我用 Binding 更改了 x:Bind,一切正常。

如果你们需要,我可以复制粘贴所有课程。但请告诉我 wtf ;)

4

1 回答 1

3

问题是默认绑定在 {x:Bind} 与 {Binding} 中的工作方式。在 {x:Bind} 中,默认绑定是 OneTime,但在 {binding} 中,默认绑定是 OneWay。因此,即使您更新视图模型,默认情况下 x:binding 中的数据也不会更改。

To Resolve Change 
<TextBlock Foreground="Red" Text="{x:Bind Name}"/>
To:
<TextBlock Foreground="Red" Text="{x:Bind Name Mode=OneWay} "/>
于 2015-10-08T23:28:29.223 回答