What is the source of this C# MAUI ListView Warning: Microsoft.Maui.Controls.Xaml.Diagnostics.BindingDiagnostics: Warning: property not found

huangapple go评论92阅读模式
英文:

What is the source of this C# MAUI ListView Warning: Microsoft.Maui.Controls.Xaml.Diagnostics.BindingDiagnostics: Warning: property not found

问题

Here's the translated content you provided:

我是.net MAUI的新手,我试图理解为什么在这个非常简单的单页面项目中会收到警告。

下面的代码在Windows和Android上都运行良好,它正确地显示了我在SQLite数据库中的选民列表,但是当我填充ListView时,我会收到调试日志中的警告,并希望修复它们。

以下是代码和警告。

MainPage.xaml
```xml
<?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"
             x:Class="SOTest.MainPage">

    <ScrollView>
        <VerticalStackLayout>

            <Button 
                    Text="Search"
                    Clicked="OnButtonClicked" />

            <ListView 
                x:Name="MyListView"
                ItemsSource="{Binding DataToDisplay}">

                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <ViewCell>
                                <VerticalStackLayout Padding="5">
                                    <Label Text="{Binding first_name}" />
                                    <Label Text="{Binding last_name}" />
                                </VerticalStackLayout>
                            </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>

                </ListView>

            </VerticalStackLayout>
     </ScrollView>

</ContentPage>

MainPage.xaml.cs

using SQLite;
using System.Diagnostics;
using System.Collections.ObjectModel;

namespace SOTest;

public partial class MainPage : ContentPage
{
    // 数据模型
    public class MyDataModel
    {
        [PrimaryKey, AutoIncrement]
        public int voter_id { get; set; }
        public string first_name { get; set; }
        public string last_name { get; set; }
        public string street_name { get; set; }
    }

    // 数据库常量
    public static class DatabaseConstants
    {
        public const string DatabaseFilename = "TestDatabase.db3";

        public const SQLite.SQLiteOpenFlags Flags =
            // 以读/写模式打开数据库
            SQLite.SQLiteOpenFlags.ReadWrite |
            // 如果数据库不存在,则创建
            SQLite.SQLiteOpenFlags.Create |
            // 启用多线程数据库访问
            SQLite.SQLiteOpenFlags.SharedCache;

        public static string DatabasePath =>
            Path.Combine(FileSystem.Current.AppDataDirectory, DatabaseFilename);
    }

    // 数据库操作
    SQLiteAsyncConnection Database;

    public async Task SetUpDb()
    {
        if (Database is not null)
            return;

        Database = new SQLiteAsyncConnection(DatabaseConstants.DatabasePath, DatabaseConstants.Flags);
        await Database.CreateTableAsync<MyDataModel>();
    }

    public async Task AddRecord(MyDataModel myRecord)
    {
        await SetUpDb();
        await Database.InsertAsync(myRecord);
        Debug.WriteLine($"Inserted {myRecord.first_name} {myRecord.last_name}");
    }

    public async Task<List<MyDataModel>> GetSomeRecords(string LastNameFilter)
    {
        await SetUpDb();
        return await Database.QueryAsync<MyDataModel>($"SELECT * FROM [MyDataModel] WHERE [last_name] = '{LastNameFilter}' COLLATE NOCASE");
    }

    public async Task<List<MyDataModel>> GetAllRecords()
    {
        await SetUpDb();
        return await Database.Table<MyDataModel>().ToListAsync();
    }

    // 可观察集合
    public ObservableCollection<MyDataModel> DataToDisplay { get; set; }

    // 主页面和事件处理程序
    public MainPage()
    {
        InitializeComponent();
        DataToDisplay = new ObservableCollection<MyDataModel>() { };
        BindingContext = this;
    }

    protected override async void OnAppearing()
    {
        base.OnAppearing();
        await SetUpDb();

        MyDataModel Record1 = new MyDataModel();
        Record1.first_name = "Albert";
        Record1.last_name = "Albertson";
        await AddRecord(Record1);

        MyDataModel Record2 = new MyDataModel();
        Record2.first_name = "Betty";
        Record2.last_name = "Boop";
        await AddRecord(Record2);

        MyDataModel Record3 = new MyDataModel();
        Record3.first_name = "Chris";
        Record3.last_name = "Kringle";
        await AddRecord(Record3);
    }

    // 按钮点击触发查询和ListView填充
    private async void OnButtonClicked(object sender, EventArgs e)
    {
        DataToDisplay.Clear();
        var MyList = await GetAllRecords();
        if (MyList.Count > 0)
        {
            var i = 0;
            foreach (var item in MyList)
            {
                Debug.WriteLine($"Adding {item.first_name}");
                DataToDisplay.Add(item);
                i++;
            }
        }
        else 
        {
            Debug.WriteLine("No Records");
        }
    }
}

输出
按下Windows上的Search按钮后的外观如下所示:

警告消息
在填充ListView时,以下是我收到的警告:

添加Albert
添加Betty
Microsoft.Maui.Controls.Xaml.Diagnostics.BindingDiagnostics: 警告: 在'SOTest.MainPage'上找不到'first_name'属性,目标属性: 'Microsoft.Maui.Controls.Label.Text'
Microsoft.Maui.Controls.Xaml.Diagnostics.BindingDiagnostics: 警告: 在'SOTest.MainPage'上找不到'last_name'属性,目标属性: 'Microsoft.Maui.Controls.Label.Text'
添加Chris
Microsoft.Maui.Controls.Xaml.Diagnostics.BindingDiagnostics: 警告: 在'SOTest.MainPage'上找不到'first_name'属性,目标属性: 'Microsoft.Maui.Controls.Label.Text'
Microsoft.Maui.Controls.Xaml.Diagnostics.BindingDiagnostics: 警告: 在'SOTest.MainPage'上找不到'last_name'属性,目标属性: 'Microsoft.Maui.Controls.Label.Text'
Microsoft.Maui.Controls.Xaml.Diagnostics.BindingDiagnostics: 警告: 在'SOTest.MainPage'上找不到'first_name'属性,目标属性: 'Microsoft.Maui.Controls.Label.Text'
Microsoft.Maui.Controls.Xaml.Diagnostics.BindingDiagnostics: 警告: 在'SOTest.MainPage'上找不到'last_name'属性,目标属性: 'Microsoft.Maui.Controls.Label.Text'

为了消除这些警告,你需要确保在MainPage.xaml中的ListView的ItemTemplate中正确绑定了first_name和last_name属性。检查绑定路径是否正确。如果数据模型是正确的,这些警告可能是由于XAML绑定路径不正确引起的。

英文:

I'm new to .net MAUI and I'm trying to understand why I'm getting a warning in this very simple single page project.

The code below runs fine on both Windows and Android, in that it correctly displays a list of voters that I have in an SQLite database, however I am get warnings in my debug log as I populate the ListView and I'd like to fix them.

The code and the warnings are below.

MainPage.xaml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&gt;
&lt;ContentPage xmlns=&quot;http://schemas.microsoft.com/dotnet/2021/maui&quot;
xmlns:x=&quot;http://schemas.microsoft.com/winfx/2009/xaml&quot;
x:Class=&quot;SOTest.MainPage&quot;&gt;
&lt;ScrollView&gt;
&lt;VerticalStackLayout&gt;
&lt;Button 
Text=&quot;Search&quot;
Clicked=&quot;OnButtonClicked&quot; /&gt;
&lt;ListView 
x:Name=&quot;MyListView&quot;
ItemsSource=&quot;{Binding DataToDisplay}&quot;&gt;
&lt;ListView.ItemTemplate&gt;
&lt;DataTemplate&gt;
&lt;ViewCell&gt;
&lt;VerticalStackLayout Padding=&quot;5&quot;&gt;
&lt;Label Text=&quot;{Binding first_name}&quot; /&gt;
&lt;Label Text=&quot;{Binding last_name}&quot; /&gt;
&lt;/VerticalStackLayout&gt;
&lt;/ViewCell&gt;
&lt;/DataTemplate&gt;
&lt;/ListView.ItemTemplate&gt;
&lt;/ListView&gt;
&lt;/VerticalStackLayout&gt;
&lt;/ScrollView&gt;
&lt;/ContentPage&gt;

MainPage.xaml. cs

using SQLite;
using System.Diagnostics;
using System.Collections.ObjectModel;
namespace SOTest;
public partial class MainPage : ContentPage
{
//--- Data Model
public class MyDataModel
{
[PrimaryKey, AutoIncrement]
public int voter_id { get; set; }
public string first_name { get; set; }
public string last_name { get; set; }
public string street_name { get; set; }
}
//--- Database Constants
public static class DatabaseConstants
{
public const string DatabaseFilename = &quot;TestDatabase.db3&quot;;
public const SQLite.SQLiteOpenFlags Flags =
// open the database in read/write mode
SQLite.SQLiteOpenFlags.ReadWrite |
// create the database if it doesn&#39;t exist
SQLite.SQLiteOpenFlags.Create |
// enable multi-threaded database access
SQLite.SQLiteOpenFlags.SharedCache;
public static string DatabasePath =&gt;
Path.Combine(FileSystem.Current.AppDataDirectory, DatabaseFilename);
}
//--- Database Operations
SQLiteAsyncConnection Database;
public async Task SetUpDb()
{
if (Database is not null)
return;
Database = new SQLiteAsyncConnection(DatabaseConstants.DatabasePath, DatabaseConstants.Flags);
await Database.CreateTableAsync&lt;MyDataModel&gt;();
}
public async Task AddRecord(MyDataModel myRecord)
{
await SetUpDb();
await Database.InsertAsync(myRecord);
Debug.WriteLine($&quot;Inserted {myRecord.first_name} {myRecord.last_name}&quot;);
}
public async Task&lt;List&lt;MyDataModel&gt;&gt; GetSomeRecords(string LastNameFilter)
{
await SetUpDb();
return await Database.QueryAsync&lt;MyDataModel&gt;(&quot;SELECT * FROM [MyDataModel] WHERE [last_name] = &#39;&quot; + LastNameFilter + &quot;&#39; COLLATE NOCASE&quot;);
}
public async Task&lt;List&lt;MyDataModel&gt;&gt; GetAllRecords()
{
await SetUpDb();
return await Database.Table&lt;MyDataModel&gt;().ToListAsync();
}
//--- Observable Collection
public ObservableCollection&lt;MyDataModel&gt; DataToDisplay { get; set; }
//--- Main Page and Event Handlers
public MainPage()
{
InitializeComponent();
DataToDisplay = new ObservableCollection&lt;MyDataModel&gt;() { };
BindingContext = this;
}
protected override async void OnAppearing()
{
base.OnAppearing();
await SetUpDb();
MyDataModel Record1 = new MyDataModel();
Record1.first_name = &quot;Albert&quot;;
Record1.last_name = &quot;Albertson&quot;;
await AddRecord(Record1);
MyDataModel Record2 = new MyDataModel();
Record2.first_name = &quot;Betty&quot;;
Record2.last_name = &quot;Boop&quot;;
await AddRecord(Record2);
MyDataModel Record3 = new MyDataModel();
Record3.first_name = &quot;Chris&quot;;
Record3.last_name = &quot;Kringle&quot;;
await AddRecord(Record3);
}
//--- Button Click Initiates the Query and ListView Population
private async void OnButtonClicked(object sender, EventArgs e)
{
DataToDisplay.Clear();
var MyList = await GetAllRecords();
if (MyList.Count &gt; 0)
{
var i = 0;
foreach (var item in MyList)
{
Debug.WriteLine($&quot;Adding {item.first_name}&quot;);
DataToDisplay.Add(item);
i++;
}
}
else 
{
Debug.WriteLine(&quot;No Records&quot;);
}
}
}

Output

Here's what it looks like after pressing the Search button on Windows:
What is the source of this C# MAUI ListView Warning: Microsoft.Maui.Controls.Xaml.Diagnostics.BindingDiagnostics: Warning: property not found

Warning Message

Below are the warnings I receive while populating my ListView:

Adding Albert
Adding Betty
Microsoft.Maui.Controls.Xaml.Diagnostics.BindingDiagnostics: Warning: &#39;first_name&#39; property not found on &#39;SOTest.MainPage&#39;, target property: &#39;Microsoft.Maui.Controls.Label.Text&#39;
Microsoft.Maui.Controls.Xaml.Diagnostics.BindingDiagnostics: Warning: &#39;last_name&#39; property not found on &#39;SOTest.MainPage&#39;, target property: &#39;Microsoft.Maui.Controls.Label.Text&#39;
Adding Chris
Microsoft.Maui.Controls.Xaml.Diagnostics.BindingDiagnostics: Warning: &#39;first_name&#39; property not found on &#39;SOTest.MainPage&#39;, target property: &#39;Microsoft.Maui.Controls.Label.Text&#39;
Microsoft.Maui.Controls.Xaml.Diagnostics.BindingDiagnostics: Warning: &#39;last_name&#39; property not found on &#39;SOTest.MainPage&#39;, target property: &#39;Microsoft.Maui.Controls.Label.Text&#39;
Microsoft.Maui.Controls.Xaml.Diagnostics.BindingDiagnostics: Warning: &#39;first_name&#39; property not found on &#39;SOTest.MainPage&#39;, target property: &#39;Microsoft.Maui.Controls.Label.Text&#39;
Microsoft.Maui.Controls.Xaml.Diagnostics.BindingDiagnostics: Warning: &#39;last_name&#39; property not found on &#39;SOTest.MainPage&#39;, target property: &#39;Microsoft.Maui.Controls.Label.Text&#39;

What do i need to do to eliminate these warnings?

THANK YOU for any suggestions!

答案1

得分: 1

将一种DataType添加到DataTemplate中。

英文:

add a DataType to the DataTemplate

&lt;DataTemplate x:DataType=&quot;myxmlns:MyDataType&quot;&gt;

huangapple
  • 本文由 发表于 2023年5月14日 05:47:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76245019.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定