首页 文章

Xamarin表单绑定到对象属性

提问于
浏览
2

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

列表视图加载项目列表及其所有属性,但详细信息页面为空白,不会绑定到属性 .

列表的viewmodel看起来像这样:

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

并绑定到这个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>

再次,这很好 .

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

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

我正在尝试将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>

页面完全空白 .

我必须将INotifyPropertyChanged添加到我的ItemModel?如果是这样我不明白为什么,因为ListView绑定相同的模型没有任何这样的需要,所以为什么不能定期标签绑定?

我究竟做错了什么?

1 回答

  • 4

    您需要先在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>
    

相关问题