【问题标题】:Xamarin XAML not bindingXamarin XAML 未绑定
【发布时间】:2018-07-26 18:49:28
【问题描述】:

我遵循了 XAML 数据绑定的正式 Xamarin 文档以及 PluralSight 教程中的一些示例,该教程说明了在 C# 代码中定义自定义 ViewCell 并遇到问题。首先,由于其流动性,我宁愿使用 XAML,但我遇到了重大问题。 This example 是最近的一个,对我来说似乎很清楚,但是如果我在 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 设置器(我已经放置了断点并且它们永远不会到达)。我已经尝试过定义和不定义分接处理程序。我需要的是能够访问底层的 SelectedItem,我将在其中访问一些属性以进行进一步处理。

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

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

【问题讨论】:

  • 虽然将 xaml 绑定到视图类并没有什么问题,但如果你想用 MVVM “正确”地做到这一点,你应该创建一个 ViewModel 类并将你的上下文绑定到它,而不是查看。

标签: c# xaml xamarin.forms


【解决方案1】:

您需要指定BindingContext

public MainPage()
{
    InitializeComponent();

    this.BindingContext = this;

【讨论】:

  • 非常感谢,这很有道理。哇,我不明白教程是如何工作的。所以我现在更新了 SelectedItem,但前提是我不使用手势识别器。是否可以检测到点击但也可以更改 SelectedItem?
  • 在 ListView 上使用 ItemSelected 事件
【解决方案2】:

在任一 XAML 中添加 BindingContext

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

或代码隐藏

this.BindingContext = new MyViewModel();

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

【讨论】:

    猜你喜欢
    • 2017-02-09
    • 1970-01-01
    • 1970-01-01
    • 2018-09-28
    • 2017-12-15
    • 2019-03-03
    • 1970-01-01
    • 1970-01-01
    • 2017-04-22
    相关资源
    最近更新 更多