c# - Xamarin.Forms Picker ItemsSource在XAML中保持为空

标签 c# xaml mvvm xamarin.forms

我的选择器保持空着。我已经创建了一个测试列表来对其进行特别测试,但是它也不起作用。

这是我的XAML

<Picker x:Name="picker1" Grid.Row="1" Grid.Column="1" ItemsSource="{Binding TestList}" ItemDisplayBinding="{Binding Name}" SelectedItem="{Binding AdditionSort}"/>

这是我的代码背后
List<AdditionSort> TestList
{
    get => testList;
    set => SetValue(ref testList, value);
}

List<AdditionSort> testList = new List<AdditionSort>();

void LoadList()
{
    TestList.Add(new AdditionSort { Name = "test1" });
    TestList.Add(new AdditionSort { Name = "test2" });
}

当我调试时,我可以看到我的列表是正确的。

最佳答案

1)使用System.Generics.ObjectModel.ObservableCollection代替List
ObservableCollection通知View on CollectionChanges,而List则不这样做。

(或)

2)在列表初始化时将项目添加到列表

List<AdditionSort> testList = new List<AdditionSort>()
{
    new AdditionSort(),
    new AdditionSort(),
    new AdditionSort(),
    new AdditionSort(),
}

关于c# - Xamarin.Forms Picker ItemsSource在XAML中保持为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60668748/

相关文章:

c# - WPF xaml Visual Studio 不断自动更改到我的资源的路径

c# - 在 MVVM 之后从 WPF 中的组框确定选中的单选按钮

c# - .Net core 3.x Keyless Entity Types 避免表创建

c# - 将 IEnumerable<T> 转换为 IEnumerator<T> 有什么作用

c# - 在对其中一列求和时合并 N 个数据表

c# - LocalStorage 重定向 Windows Phone 7

c# - 如果结果返回 null,则为 TextBlock 提供默认值

c# - 设计决策: MVVM and WPF with two Views?

wpf - 如何根据 ViewModel 属性的值让 View 元素淡入/淡出?

c# - 在 C# 中处理字体对象