.NET MAUI 绑定:未找到属性

问题描述 投票:0回答:1

我正在 VS 22 中使用 .Net MAUI 制作一个应用程序,我设置了一个简单的页面,在其中显示项目列表,但是当我尝试运行代码时,出现此错误:

严重性代码描述项目文件行抑制状态详细信息 错误(活动)XFC0045 绑定:在“ProjectName .ViewModels.ParkingSistemiViewModel”上找不到属性“DodatniNaziv”。项目名称 (net8.0-windows10.0.19041.0) C:*** epos\项目名称\项目名称\Views\MainDashboard.xaml 64

我花了好几个小时才弄清楚,但我就是不明白问题是什么,如果有人可以提供帮助,我将不胜感激

这是我的代码: 1.) XAML 页面

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:viewModels="clr-namespace:ProjectName.ViewModels"
             x:Class="ProjectName.Views.MainDashboard"
             xmlns:models="ProjectName.Models"
             x:DataType="viewModels:ParkingSistemiViewModel"
             Shell.NavBarIsVisible="false">
            
    <ContentPage.BindingContext>
        <viewModels:ParkingSistemiViewModel />
    </ContentPage.BindingContext>

    <ScrollView>
        <StackLayout>
            <!-- Header Stranice -->
            <StackLayout BackgroundColor="#744fa0" Padding="20,40,20,20">
                <Button x:Name="odjavaBtn"
                        HorizontalOptions ="End"
                        WidthRequest="100"
                        FontSize="16"
                        FontAttributes="Bold"
                        Text="ODJAVA"
                        FontFamily="Bahnschrift"
                        Clicked="OnOdjavaClicked"
                        Margin="0,0,0,20"/>
                <Label Text="PAMETNI PARKING"
                       FontSize="25"
                       FontAttributes="Bold"
                       FontFamily="Bahnschrift"
                       VerticalOptions="Center" 
                       HorizontalOptions="Start" FontAutoScalingEnabled="False" />
                <Label Text="Pregled dostupnih sistema"
                       VerticalOptions="Center" 
                       FontFamily="Bahnschrift"
                       HorizontalOptions="Start" />
            </StackLayout>
        
            <!-- Body Stranice (Lista svih Sistema)-->
            <CollectionView x:Name="listaParkingSistema" SelectionMode="Single" SelectionChanged="OpenElement">
                <CollectionView.ItemTemplate>
                    <DataTemplate>
                        <Border StrokeShape="RoundRectangle 20"
                                Padding="5"
                                Margin="20,5,20,0"
                                BackgroundColor="White">
                            <Grid BackgroundColor="White">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="30"/>
                                    <RowDefinition Height="30"/>
                                </Grid.RowDefinitions>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="60"/>
                                    <ColumnDefinition Width="*"/>
                                    <ColumnDefinition Width="60"/>
                                </Grid.ColumnDefinitions>
                                <Image Source="parking.png"
                                       Grid.RowSpan="2"
                                       Grid.Column="0"
                                       HorizontalOptions="CenterAndExpand"
                                       VerticalOptions="CenterAndExpand"/>
                                <Label x:Name="Additional"
                                       Margin="10,0,0,0"
                                       Text="{Binding DodatniNaziv}"
                                       FontSize="15"
                                       FontFamily="Bahnschrift"
                                       HorizontalTextAlignment="Start"
                                       VerticalTextAlignment="Center"
                                       TextColor="#404040"
                                       Grid.Row="0"
                                       Grid.Column="1"/>
                                <Label x:Name="Title"
                                       Margin="10,0,0,0"
                                       Text="{Binding Naziv}"
                                       FontSize="20"
                                       FontFamily="Bahnschrift"
                                       FontAttributes="Bold"
                                       CharacterSpacing="1"
                                       HorizontalTextAlignment="Start"
                                       VerticalTextAlignment="Center"
                                       TextColor="#404040"
                                       Grid.Row="1"
                                       Grid.Column="1"/>
                                <Image x:Name="StatusBtn"
                                       Source="{Binding Connection}"
                                       Grid.RowSpan="2"
                                       Grid.Column="2"
                                       HorizontalOptions="CenterAndExpand"
                                       VerticalOptions="CenterAndExpand"/>
                            </Grid>
                        </Border>
                    </DataTemplate>
                </CollectionView.ItemTemplate>
            </CollectionView>
        </StackLayout>
    </ScrollView>
</ContentPage>


2.) 背后的代码:

namespace ProjectName.Views;
public partial class MainDashboard : ContentPage
{
    public MainDashboard()
    {
        InitializeComponent();
    
    }

    //Metoda koja odjavljuje korisnika sa aplikacije
    private async void OnOdjavaClicked(object sender, EventArgs e)
    {
        await Navigation.PopAsync();
    }

    //Metoda koja Vodi na sljedeci screen
    private async void OpenElement(object sender, SelectionChangedEventArgs e)
    {
        await Navigation.PushAsync(new ParkingSistemDashboard());

3.) ViewModel 类

using ProjectName.Entities;
using CommunityToolkit.Mvvm.ComponentModel;
using System.Collections.ObjectModel;

namespace ProjectName.ViewModels
{
    class ParkingSistemiViewModel : ObservableObject
    {
        private ObservableCollection<ParkingSistemiModel> _parkingSitemi;

        public ObservableCollection<ParkingSistemiModel> ParkingSitemi
        {
            get => _parkingSitemi;
            set => SetProperty(ref _parkingSitemi, value);
        }

        public ParkingSistemiViewModel()
        {
            ParkingSitemi = new ObservableCollection<ParkingSistemiModel>
            {
                new ParkingSistemiModel{ParkingId = Guid.Parse("5177bbc1-c96a-473b-af8b-fc4a5bc7ed91"), Naziv = "Test Parking 1", DodatniNaziv = "Najbolji Parking mjkm", Ip = "8.8.8.8"},
                new ParkingSistemiModel{ParkingId = Guid.Parse("43cde37a-c097-4eee-b320-1a5edd30ffe8"), Naziv = "Test Parking 2", DodatniNaziv = "Najbolji Parking mjkm", Ip = "8.8.8.8"},
                new ParkingSistemiModel{ParkingId = Guid.Parse("810c76c7-094f-4d92-8c31-ec9a98415678"), Naziv = "Test Parking 3", DodatniNaziv = "Najbolji Parking mjkm", Ip = "8.8.8.8"},
                new ParkingSistemiModel{ParkingId = Guid.Parse("276431a4-6f70-4b64-9520-fd42110a790c"), Naziv = "Test Parking 4", DodatniNaziv = "Najbolji Parking mjkm", Ip = "8.8.8.8"},
                new ParkingSistemiModel{ParkingId = Guid.Parse("1debd0c6-0fb0-4bc7-b324-5b15ad185b40"), Naziv = "Test Parking 5", DodatniNaziv = "Najbolji Parking mjkm", Ip = "0.0.0.0"},
                new ParkingSistemiModel{ParkingId = Guid.Parse("11a58136-2058-4194-8e48-c3e64de90088"), Naziv = "Test Parking 6", DodatniNaziv = "Najbolji Parking mjkm", Ip = "8.8.8.8"},
                new ParkingSistemiModel{ParkingId = Guid.Parse("14f1e1fb-db63-4b8d-9601-f99988890467"), Naziv = "Test Parking 7", DodatniNaziv = "Najbolji Parking mjkm", Ip = "8.8.8.8"},
                new ParkingSistemiModel{ParkingId = Guid.Parse("8a6d818e-8839-43a9-9bed-e0afae0c38c1"), Naziv = "Test Parking 8", DodatniNaziv = "Najbolji Parking mjkm", Ip = "8.8.8.8"},
                new ParkingSistemiModel{ParkingId = Guid.Parse("56b610e4-bb71-49b8-b0f7-444a6c6b2f53"), Naziv = "Test Parking 9", DodatniNaziv = "Najbolji Parking mjkm", Ip = "8.8.8.8"},
            };

        }
    }
}

4.) 模型类

using System.Net.NetworkInformation;

namespace ProjectName.Entities
{
    public class ParkingSistemiModel
    {
        public Guid ParkingId { get; set; }
        public string Naziv { get; set; }
        public string DodatniNaziv { get; set; }
        public string Ip { get; set; }
        public string Connection { get; private set; }


        public ParkingSistemiModel()
        {
            Connection = CheckConnection();
        }

        //Method that is used to check the connection to the server
        public string CheckConnection()
        {
            if (string.IsNullOrWhiteSpace(Ip))
            {
                return "cancel.png";
            }

            using (Ping ping = new Ping())
            {
                try
                {
                    PingReply reply = ping.Send(Ip);
                    return reply.Status == IPStatus.Success
                        ? "checked.png"
                        : "cancel.png";
                }
                catch
                {
                    return "cancel.png";
                }
            }
        }
    }
}

诚实不知道从哪里开始

.net mvvm maui maui-community-toolkit
1个回答
0
投票
  1. 如果您不打算动态添加/删除项目,您可以这样做:

    [ObservableProperty]
    List<ParkingSistemiModel> _parkingSitemi;
    

(这将自动生成 ParkingSitemi,您可以重新分配)

  1. 在页面的 XAML 中定义 CollectionView 的位置,添加
    ItemsSource={Binding ParkingSitemi}

(这告诉您的集合视图显示此列表)

  1. 在 DataTemplate 行中,添加
    x:DataType="ParkingSistemiModel"

(这表明集合视图的每个“行”正在显示 ParkingSistemiModel 类型的对象)

  1. ViewModel 与您的页面一起构建。您在视图模型的构造函数中填充列表,这意味着它会在页面的构造函数中填充。现在没有问题。但是如果你开始从 HTTP 请求加载这个列表 - 这将是不行的。

您应该在页面事件中执行此操作。我的理解是,普遍的共识是,这是“OnNavigedTo”事件。但页面生命周期的其他事件也是可以接受的。 (在某种程度上,它们不会导致页面构建过程中繁重操作导致的相同问题)

© www.soinside.com 2019 - 2024. All rights reserved.