簡體   English   中英

MAUI 未在 VIew Model 列表中找到屬性

[英]MAUI not finding property in VIew Model List

我有一個簡單的視圖 model,其中一個屬性包含 model,另一個屬性包含模型列表。

我能夠毫無問題地綁定“測試”模型的屬性,但我無法讓 XAML 識別出“ListModel”包含一個具有其自身屬性的列表。 我已經查看了幾個示例,了解如何設置視圖 model 並在將其綁定到視圖之前正確初始化列表,雖然 XAML 理解“ListModel”是一個屬性,但我無法讓它識別它是一個列表,因此它不會編譯,這樣我至少可以看看它是否不是由於某種原因而失敗的智能感知。

這是有問題的視圖 model,列表名為“ListModel”

public class TestViewModel
{        
    public TestModel Test { get; } = new TestModel();
    public List<TestListModel> ListModel { get; set; }


    public TestViewModel()
    {
        Initialize();
    }

    public void Initialize()
    {
        ListModel = new List<TestListModel>();
        ListModel.Add(new TestListModel
        {
            ListProp1 = "First",
            ListProp2 = "Second",
            ListProp3 = "Third"
        });
    }     
}

這是被放入列表中的 Model。 似乎視圖沒有看到這些屬性。

public class TestListModel
    {        
        public string ListProp1 { get; set; }
        public string ListProp2 { get; set; }
        public string ListProp3 { get; set; }
    }

這是我目前的 XAML。

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MauiApp1.MainPage"
             xmlns:local="clr-namespace:ViewModels" 
             x:DataType="local:TestViewModel"
    >

    <ScrollView>

        <VerticalStackLayout 
            Spacing="25" 
            Padding="30,0" 
            VerticalOptions="Center">          

            <!--This works-->
            <Entry Text="{Binding Test.Property1}"/>
            <Entry Text="{Binding Test.Property2}"/>
            <Entry Text="{Binding Test.Property3}"/>            
            
            <!--This does not work-->
            <ListView
                ItemsSource="{Binding ListModel}">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <Label Text="{Binding ListProp1}"/>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>    
            </ListView>    
        </VerticalStackLayout>    
    </ScrollView>
</ContentPage>
 

viewviewmodel通常通過 XAML 中定義的數據綁定連接。 視圖的BindingContext通常是viewmodel的一個實例。所以我認為您忘記將這兩個元素與BindingContext連接起來。

視圖背后的代碼:

public partial class MainPage : ContentPage
{

    TestViewModel tv = new TestViewModel();
    public MainPage()
    {
        InitializeComponent();
        BindingContext = tv;
    }
}

Xaml 中的代碼:

 <ScrollView>
        <VerticalStackLayout 
            Spacing="25" 
            Padding="30,0" 
            VerticalOptions="Center">

            <!--This works-->
            <Entry Text="{Binding Test.Property1}"/>
            <Entry Text="{Binding Test.Property2}"/>
            <Entry Text="{Binding Test.Property3}"/>

            <!--This works too-->
            <ListView
                HasUnevenRows="True"
                ItemsSource="{Binding ListModel}">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <VerticalStackLayout>
                                <Label Text="{Binding ListProp1}"/>
                                <Label Text="{Binding ListProp2}"/>
                                <Label Text="{Binding ListProp3}"/>
                            </VerticalStackLayout>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </VerticalStackLayout>
    </ScrollView>

結果:

在此處輸入圖像描述

參考鏈接

對於任何絆倒此的人:評論中的傑森已經回答了這個問題。 修復只是從 XAML 頂部刪除 x:DataType,盡管我沒有從中刪除“xmlns:local”。

我所擁有的是一個視圖模型,其中包含多個模型,這在刪除 x:DataType 時似乎擾亂了智能感知。 刪除它最初會阻止應用程序編譯,因為它找不到我在 XAML 中擁有的屬性。 一旦我清理並重建了解決方案,它就可以順利編譯和工作。

通過使 ItemSource 綁定到具有私有支持字段的 List,我能夠修復錯誤,同時保持已編譯的綁定就位。 如果沒有私有屬性,編譯器似乎無法正確解析列表。 在我添加 listModel 后它編譯了。 所以問題似乎是 setter 丟失了。

    private List<TestListModel> listModel;
    public List<TestListModel> ListModel { get => listModel; set => listModel = value; }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM