xamarin - Xamarin 表单 - 无法更新选取器
问题描述
以下 Xamarin Forms 应用程序包含一个Picker
和一个CollectionView
绑定到相同List
动物 (Chicken
和Cow
) 的控件。当我按下升级按钮时,动物应该升级到Eagle
and Elephant
。动物已升级,但 Picker 并未反映更改。CollectionView
作品很好。
我在这里做错了什么?如果需要,可以在 Github上找到该项目的源代码。
点击查看演示
主页.xaml
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="SampleApp.MainPage">
<StackLayout Margin="0,100,0,0">
<Button Text="Upgrade Animals"
Margin="10"
HorizontalOptions="Center"
VerticalOptions="Start"
Command="{Binding Upgrade}" />
<Picker ItemsSource="{Binding Animals}" />
<CollectionView ItemsSource="{Binding Animals}" />
</StackLayout>
</ContentPage>
MainPageModel.cs
public class MainPageModel : INotifyPropertyChanged
{
public MainPageModel()
{
_Animals = new List<string>() { "Chicken", "Cow" };
}
private List<string> _Animals;
public List<string> Animals
{
get { return _Animals; }
set
{
_Animals = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Animals"));
}
}
public Command Upgrade
{
get
{
return new Command(_ =>
{
Animals = new List<string>() { "Eagle", "Elephant" };
});
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
更新
上面的代码正在运行 XF 4.3.0.908675。我降级到 4.2.0.778463,问题就消失了。这可能是最新 XF 版本中引入的一个新错误。
解决方案
该问题已在报告的频道上得到确认:Issue 8177,我已经尝试了 Xamarin.Forms(4.4.0.991210-pre2) 的预发布版本,现在绑定在 UWP 上工作正常。
推荐阅读
- php - PHP没有将变量传递给插入?
- azure-devops - 持久化由代理上的任务产生的进程的方法?
- kubernetes - Kube-controller-manager HPA 计算未就绪的 pod 并且不考虑将它们用于所需的副本计算
- javascript - 按钮单击事件后如何正确倒计时秒数
- generics - Kotlin 无法通过方法引用推断 Function 的类型
- java - 使用我的代码中的 2 个变量创建循环(Android)
- python - 创建一列,按条件将一组行的最大值分配给该组中的所有行
- jenkins - Jenkins 在加载插件之前执行 Groovy 脚本
- c - 如何在 C 中打印“整洁”的二维数组
- javascript - 我想用 rxjs observable 替换 addEventListener