Xamarin XAML没有约束力

Ste*_*ork 0 c# xaml xamarin.forms

我从XAML数据绑定的正式Xamarin doco以及PluralSight教程中了解了一些示例,该教程说明了在C#代码中定义自定义ViewCell并遇到问题.首先,我宁愿使用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>
Run Code Online (Sandbox Code Playgroud)

和背后的代码

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

}
Run Code Online (Sandbox Code Playgroud)

因此我将看到一个空的ListView,因此绑定失败(我确定我已经完成了视频链接中的所有内容),但如果我取消注释c#行(来自PluralSight上的编码教程)以分配实例公共属性,然后它确实工作,我会看到我的列表中的项目...但是点击一个项目(更改所选项目)永远不会调用SelectedThingy setter(我已经把断点放入并且它们永远不会到达).我已经尝试过,无论是否定义了tapped处理程序.我需要的是能够到达底层的SelectedItem,在那里我将访问一些属性以进行进一步处理.

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

谁知道我做错了什么?

Jas*_*son 6

你需要指定一个 BindingContext

public MainPage()
{
    InitializeComponent();

    this.BindingContext = this;
Run Code Online (Sandbox Code Playgroud)

  • 改为使用ListView上的ItemSelected事件 (2认同)