英文:
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
<?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
{
//--- 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 = "TestDatabase.db3";
public const SQLite.SQLiteOpenFlags Flags =
// open the database in read/write mode
SQLite.SQLiteOpenFlags.ReadWrite |
// create the database if it doesn't exist
SQLite.SQLiteOpenFlags.Create |
// enable multi-threaded database access
SQLite.SQLiteOpenFlags.SharedCache;
public static string DatabasePath =>
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<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();
}
//--- Observable Collection
public ObservableCollection<MyDataModel> DataToDisplay { get; set; }
//--- Main Page and Event Handlers
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);
}
//--- 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 > 0)
{
var i = 0;
foreach (var item in MyList)
{
Debug.WriteLine($"Adding {item.first_name}");
DataToDisplay.Add(item);
i++;
}
}
else
{
Debug.WriteLine("No Records");
}
}
}
Output
Here's what it looks like after pressing the Search button on Windows:
Warning Message
Below are the warnings I receive while populating my ListView:
Adding Albert
Adding Betty
Microsoft.Maui.Controls.Xaml.Diagnostics.BindingDiagnostics: Warning: 'first_name' property not found on 'SOTest.MainPage', target property: 'Microsoft.Maui.Controls.Label.Text'
Microsoft.Maui.Controls.Xaml.Diagnostics.BindingDiagnostics: Warning: 'last_name' property not found on 'SOTest.MainPage', target property: 'Microsoft.Maui.Controls.Label.Text'
Adding Chris
Microsoft.Maui.Controls.Xaml.Diagnostics.BindingDiagnostics: Warning: 'first_name' property not found on 'SOTest.MainPage', target property: 'Microsoft.Maui.Controls.Label.Text'
Microsoft.Maui.Controls.Xaml.Diagnostics.BindingDiagnostics: Warning: 'last_name' property not found on 'SOTest.MainPage', target property: 'Microsoft.Maui.Controls.Label.Text'
Microsoft.Maui.Controls.Xaml.Diagnostics.BindingDiagnostics: Warning: 'first_name' property not found on 'SOTest.MainPage', target property: 'Microsoft.Maui.Controls.Label.Text'
Microsoft.Maui.Controls.Xaml.Diagnostics.BindingDiagnostics: Warning: 'last_name' property not found on 'SOTest.MainPage', target property: 'Microsoft.Maui.Controls.Label.Text'
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
<DataTemplate x:DataType="myxmlns:MyDataType">
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论