Xamarin表单绑定到对象属性

Sel*_*Net 3 c# data-binding xaml mvvm xamarin.forms

我似乎在Xamarin Forms项目中的数据绑定有问题。我有两种视图:一种用于列出项目,另一种用于查看详细信息。

列表视图可以很好地加载项目列表及其所有属性,但是详细信息页面为空白,并且不会绑定到属性。

列表的视图模型如下所示:

public class MainPageViewModel : ViewModelBase
{
    public MainPageViewModel(IItemService itemService) : base(itemService) { }

    public ObservableCollection<ItemModel> Items { get; set; } = new ObservableCollection<ItemModel>();

    public async override void Start()
    {
        base.Start();

        Items.Clear();

        var results = await service.GetItems();
        foreach (var result in results)
        {
            Items.Add(result);
        }

        IsLoading = false;
    }
}
Run Code Online (Sandbox Code Playgroud)

并绑定到此xaml:

<ListView x:Name="ItemListView" ItemsSource="{Binding Items}" ItemSelected="ItemListView_ItemSelected">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="100" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                    <StackLayout Orientation="Vertical">
                        <Image Source="{Binding Image}" />
                    </StackLayout>
                    <StackLayout Orientation="Vertical" Grid.Column="1">
                        <Label Text="{Binding Name}" />
                        <Label Text="{Binding Description}" />
                    </StackLayout>
                </Grid>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>
Run Code Online (Sandbox Code Playgroud)

同样,这很好。

但是详细信息视图如下所示:

public class DetailsPageViewModel : ViewModelBase
{
    public DetailsPageViewModel(IItemService itemService) : base(itemService) { }

    private ItemModel item;
    public ItemModel Item
    {
        get { return item; }
        set
        {
            item = value;
            RaisePropertyChanged();
        }
    }

    protected string ItemId { get; set; }

    public void Init(string itemId)
    {
        this.ItemId = itemId;
    }

    public async override void Start()
    {
        base.Start();

        Item = await service.GetItem(ItemId);

        IsLoading = false;
    }
}
Run Code Online (Sandbox Code Playgroud)

并且我试图将ItemModel属性绑定到详细信息视图上的标签:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MvvmCrossFormsDemo.Views.DetailsPageView">
    <StackLayout>
    <Label Text="{Binding Item.Name}" />
    <Image Source="{Binding Item.Image}" />
    </StackLayout>
</ContentPage>
Run Code Online (Sandbox Code Playgroud)

该页面完全空白。

我必须将InotifyPropertyChanged添加到我的ItemModel吗?如果是这样,我不明白为什么,因为ListView绑定相同的模型而没有任何此类需求,那么为什么不能常规标签绑定?

我究竟做错了什么?

Ada*_*ley 7

您需要首先在ViewModel中指定属性。ListView已经直接绑定到ViewModel中的属性,然后ViewCell已经绑定到单个项目。

在详细信息页面中,直接绑定到ViewModel,因此没有属性Name或Image,它们是Item的属性,您需要首先指定它们。

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MvvmCrossFormsDemo.Views.DetailsPageView">
    <StackLayout>
        <Label Text="{Binding Item.Name}" />
        <Image Source="{Binding Item.Image}" />
    </StackLayout>
</ContentPage>
Run Code Online (Sandbox Code Playgroud)