0

我的问题与编译绑定有关。如果我在使用 x:DataType 时尝试构建,编译器会显示此错误:错误 XFC0045:绑定:在“_0waste.ViewModels.ProductListViewModel”上找不到属性“Image”。(XFC0045)(_0waste)XamlC

这显示在 CollectionView 项目的每个元素上,我什至对我所做的每个绑定都收到这种类型的警告信息:XLS1108:在数据上下文“ProductListViewModel”中找不到成员(_0waste)IntelliSense

当我删除编译的绑定显然我没有收到任何错误,但集合工作得很好,因此我们可以排除任何错误的路径或缺少数据错误。
这个错误可能与我以“_”开头的项目名称有关吗?

查看: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:viewmodels="clr-namespace:_0waste.ViewModels"
    xmlns:mvvm="clr-namespace:MvvmHelpers;assembly=MvvmHelpers"
    xmlns:models="clr-namespace:_0waste.Models"
    x:Class="_0waste.Views.ProductListPage"
    x:DataType="viewmodels:ProductListViewModel">
    <ContentPage.BindingContext>
        <viewmodels:ProductListViewModel/>
    </ContentPage.BindingContext>

    <RefreshView
        Command="{Binding RefreshCommand}"
        IsRefreshing="{Binding IsBusy, Mode=OneWay}"
        Style="{StaticResource BaseRefreshView}">

        <CollectionView
            BackgroundColor="Transparent"
            IsGrouped="True"
            ItemSizingStrategy="MeasureAllItems"
            ItemsLayout="VerticalList"
            ItemsSource="{Binding ProductsGrouped}"
            SelectionMode="Single">
            <CollectionView.EmptyView>
                <StackLayout Padding="12">
                    <Label HorizontalOptions="Center" Text="Nessun prodotto" />
                </StackLayout>
            </CollectionView.EmptyView>
            <CollectionView.GroupHeaderTemplate>
                <DataTemplate x:DataType="{x:Null}">
                    <StackLayout Padding="12">
                        <Label Style="{StaticResource LabelMedium}" Text="{Binding Key}" />
                    </StackLayout>
                </DataTemplate>
            </CollectionView.GroupHeaderTemplate>
            <CollectionView.ItemTemplate>
                <DataTemplate>
                    <SwipeView>
                        <SwipeView.RightItems>
                            <SwipeItems>
                                <SwipeItem
                                    BackgroundColor="Red"
                                    IsDestructive="True"
                                    Text="Elimina" />
                            </SwipeItems>
                        </SwipeView.RightItems>
                        <Grid Padding="10">
                            <Frame Style="{StaticResource ProductCard}">
                                <StackLayout Orientation="Horizontal">
                                    <Image Source="{Binding Image}" WidthRequest="66" />
                                    <StackLayout VerticalOptions="Center">
                                        <Label
                                            Style="{StaticResource LabelLarge}"
                                            Text="{Binding Name}"
                                            VerticalOptions="Center" />
                                        <Label
                                            Style="{StaticResource LabelLarge}"
                                            Text="{Binding Section}"
                                            VerticalOptions="Center" />
                                    </StackLayout>
                                </StackLayout>
                            </Frame>
                        </Grid>
                    </SwipeView>
                </DataTemplate>
            </CollectionView.ItemTemplate>
            <CollectionView.Header>
                <StackLayout Orientation="Horizontal">
                    <Label
                        HorizontalOptions="Center"
                        Style="{StaticResource LabelMedium}"
                        Text="Coffee of the World" />
                </StackLayout>
            </CollectionView.Header>
            <CollectionView.Footer>
                <StackLayout HorizontalOptions="Center" Orientation="Horizontal">
                    <Button
                        Style="{StaticResource ButtonOutline}"
                        Text="Carica altri" />
                    <Button
                        Style="{StaticResource ButtonOutline}"
                        Text="Libera" />
                </StackLayout>
            </CollectionView.Footer>
        </CollectionView>
    </RefreshView>
    
</ContentPage>



视图模型代码

using System;
using System.Linq;
using System.Threading.Tasks;
using _0waste.Models;
using MvvmHelpers;
using MvvmHelpers.Commands;
using Command = MvvmHelpers.Commands.Command;
namespace _0waste.ViewModels
{
    public class ProductListViewModel : BaseViewModel
    {
        public ObservableRangeCollection<ProductModel> Products { get; set; }
        public ObservableRangeCollection<Grouping<string, ProductModel>> ProductsGrouped { get; set; }

        public AsyncCommand RefreshCommand { get; }
        public Command FilterSort { get; }

        public ProductListViewModel()
        {
            Products = new ObservableRangeCollection<ProductModel>();
            ProductsGrouped = new ObservableRangeCollection<Grouping<string, ProductModel>>();

            var image = "https://icatcare.org/app/uploads/2018/07/Thinking-of-getting-a-cat.png";

            Products.Add(new ProductModel { Name = "Micio", Section = "Gatto", Image = image, Price = 0, EAN = "80000000000001", ExpiringDate = new DateTime() });
            Products.Add(new ProductModel { Name = "Micino", Section = "Gatto", Image = image, Price = 0, EAN = "80000000000001", ExpiringDate = new DateTime() });

            ProductsGrouped.Add(new Grouping<string, ProductModel>("Gatto", Products.Where(el => el.Section == "Gatto")));

            RefreshCommand = new AsyncCommand(Refresh);
            FilterSort = new Command(OnFilter);
        }

        async Task Refresh()
        {
            IsBusy = true;

            await Task.Delay(2000);

            IsBusy = false;
        }

        void OnFilter()
        {

        }
    }
}
4

1 回答 1

0

根据您提供的代码,您首先定义一个xmlns:viewmodels然后使用此属性来定位 ProductListViewModel 类。

但是根据您提供的模型,您没有在ProductListViewModel中定义 Image 属性,而是在ProductModel中存在,因此在 ProductListViewModel 中找不到 Image 属性。

于 2021-10-07T09:40:05.123 回答