Xamarin XAML not binding



我已经学习了关于XAML数据绑定的正式Xamarin doco中的许多示例,以及PluralSight教程,该教程说明了在C#代码中定义自定义ViewCells并遇到问题。首先,由于XAML的流动性,我宁愿使用它,但我遇到了一些重大问题。这个例子是最近的一个,对我来说似乎很有意义,但缺少了一些东西,因为如果我在XAML中指定ItemsSource,我的数据源永远不会绑定。这是我的XAML:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:TOFApp"
x:Class="MyApp.MainPage" 
Title="MyApp"
Padding="0,20,0,0">
<StackLayout>
<!-- Place new controls here -->
<ListView x:Name="thingyList"
ItemsSource="{Binding ThingyList}"
CachingStrategy="RecycleElement"
SelectedItem="{Binding SelectedThingy}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Horizontal" Padding="15,5,5,5">
<StackLayout.GestureRecognizers>
<TapGestureRecognizer Tapped="OnThingyTapped"></TapGestureRecognizer>
</StackLayout.GestureRecognizers>                                
<Image Source="" HeightRequest="50" WidthRequest="50" />
<StackLayout Orientation="Vertical">
<Label Text="{Binding Name}" 
VerticalOptions="Center" 
HorizontalOptions="StartAndExpand"
FontSize="Medium" />
<Label Text="{Binding Description}" 
VerticalOptions="Center" 
HorizontalOptions="StartAndExpand"
FontSize="Micro"
FontAttributes="Italic" />
<Label Text="" 
IsVisible="false" />
</StackLayout>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage>

以及背后的代码

public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
ThingyList = new ObservableCollection<Thingy>
{
new Thingy {Id = 1, Name = "First Thingy", Description = "Kinda fun mate!"},
new Thingy {Id = 2, Name = "Second Thingy", Description = "Not as fun"},
new Thingy {Id = 3, Name = "Third Thingy", Description = "Downright awful"}
};
Padding = new Thickness(0, Device.OnPlatform(20, 0, 0), 0, 0);
/////this.thingyList.ItemsSource = ThingyList;

}
private Thingy _selectedThingy;
public Thingy SelectedThingy
{
get { return _selectedThingy; }
set
{
if (_selectedThingy != value)
{
_selectedThingy = value;
}
}
}
public ObservableCollection<Thingy> ThingyList { get; set; }
private void OnThingyTapped(object sender, EventArgs e)
{
}
}
public class Thingy
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}

因此,我将看到一个空的ListView,因此绑定失败(我确信我已经在视频链接中完成了所有操作(,但如果我取消注释c行(来自PluralSight上的编码教程(以分配实例化的公共属性,那么它确实有效,我将在列表中看到项目。。。但是点击一个项目(更改所选项目(永远不会调用SelectedThingy setter(我已经在其中放置了断点,但它们永远不会被访问(。我已经尝试过使用和不使用定义的抽头处理程序。我需要的是能够访问底层的SelectedItem,在那里我将访问一些属性以进行进一步处理。

我在一个跨平台应用程序中使用与Visual Studio 2017社区版一起安装的Xamarin,该应用程序具有用于通用UI代码的共享项目。

有人知道我做错了什么吗?

您需要指定一个BindingContext

public MainPage()
{
InitializeComponent();
this.BindingContext = this;

在任一XAML 中添加BindingContext

<ContentPage.BindingContext>
<local:MyViewModel/>
</ContentPage.BindingContext>

或码后

this.BindingContext = new MyViewModel();

好的做法是为XAML页面创建一个ViewModel类,并像上面那样初始化它,在这种情况下,在ViewModel中添加所有Thingy属性以保留MVVM结构。