英文:
.NET MAUI item is always null
问题
以下是您提供的内容的中文翻译:
XAML:
我正在使用 MAUI 开发应用程序。我有四个页面,它们几乎相同。在第四个页面上,我始终得到从视图传递的模型为 NULL。我尝试了各种方法,从创建另一个页面到复制并粘贴一个工作的页面到 XAML 和 C# 中。我知道这可能是一些简单的东西,但它一直在躲避我。
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:models="clr-namespace:WhatGoesIn.Models"
x:Class="WhatGoesIn.Views.ExportPage"
xmlns:ios="clr-namespace:Microsoft.Maui.Controls.PlatformConfiguration.iOSSpecific;assembly=Microsoft.Maui.Controls"
ios:Page.UseSafeArea="True"
Title="Export">
<ContentPage.BindingContext>
<models:ExportItem />
</ContentPage.BindingContext>
<ScrollView>
<VerticalStackLayout
Padding="20"
Spacing="10"
VerticalOptions="StartAndExpand">
<Label Text="Date" FontAttributes="Bold"/>
<DatePicker Date="{Binding StartDate}" MinimumDate="01/01/2023"/>
<Label Text="Time" FontAttributes="Bold"/>
<DatePicker Date="{Binding ToDate}"/>
<Label Text="Recipient" FontAttributes="Bold" FontSize="16"/>
<Entry Text="{Binding Recipient}" />
<Button Text="Export" Clicked="OnExportClicked" />
<Button Text="Cancel"
Clicked="OnCancelClicked" />
</VerticalStackLayout>
</ScrollView>
</ContentPage>
C#:
我正在使用 WhatGoesIn.Models 命名空间。
namespace WhatGoesIn.Views
{
[QueryProperty("Item", "Item")]
public partial class ExportPage : ContentPage
{
WhatGoesInDatabase database;
public ExportItem Item
{
get => BindingContext as ExportItem;
set => BindingContext = value;
}
public ExportPage(WhatGoesInDatabase whatGoesInDatabase)
{
InitializeComponent();
BindingContext = this;
database = whatGoesInDatabase;
}
private async void OnExportClicked(object sender, EventArgs e)
{
ExportItem item = Item as ExportItem; // <= 始终为 NULL!
string body = await database.ExportItemAsync(item);
if (string.IsNullOrEmpty(Item.Recipient))
{
await DisplayAlert("No Recipient", "Please choose the recipient of the report.", "OK");
return;
}
if (Email.Default.IsComposeSupported)
{
string subject = "What Goes In Food Log!";
string[] recipients = new[] { Item.Recipient };
var message = new EmailMessage
{
Subject = subject,
Body = body,
BodyFormat = EmailBodyFormat.PlainText,
To = new List<string>(recipients)
};
await Email.Default.ComposeAsync(message);
}
await Shell.Current.GoToAsync("..");
}
private async void OnCancelClicked(object sender, EventArgs e)
{
await Shell.Current.GoToAsync("..");
}
}
}
模型:
我正在使用 SQLite 命名空间。
namespace WhatGoesIn.Models
{
public class ExportItem
{
[PrimaryKey, AutoIncrement]
public long ID { get; set; }
public DateTime StartDate { get; set; }
public DateTime ToDate { get; set; }
public string Recipient { get; set; }
}
}
请注意,代码中可能包含 HTML 编码,需要根据需要进行处理。如果您有任何其他问题或需要进一步的帮助,请告诉我。
英文:
I'm developing an app using MAUI. I have four pages that are virtually identical. On the fourth, I always get NULL for the model being passed from the view. I've tried every number of things, from creating another page to copy and pasting a working page into the XAML and C#. I know it is probably something simple, but it is eluding me.
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:models="clr-namespace:WhatGoesIn.Models"
x:Class="WhatGoesIn.Views.ExportPage"
xmlns:ios="clr-namespace:Microsoft.Maui.Controls.PlatformConfiguration.iOSSpecific;assembly=Microsoft.Maui.Controls"
ios:Page.UseSafeArea="True"
Title="Export">
<ContentPage.BindingContext>
<models:ExportItem />
</ContentPage.BindingContext>
<ScrollView>
<VerticalStackLayout
Padding="20"
Spacing="10"
VerticalOptions="StartAndExpand">
<Label Text="Date" FontAttributes="Bold"/>
<DatePicker Date="{Binding StartDate}" MinimumDate="01/01/2023"/>
<Label Text="Time" FontAttributes="Bold"/>
<DatePicker Date="{Binding ToDate}"/>
<Label Text="Recipient" FontAttributes="Bold" FontSize="16"/>
<Entry Text="{Binding Recipient}" />
<Button Text="Export" Clicked="OnExportClicked" />
<Button Text="Cancel"
Clicked="OnCancelClicked" />
</VerticalStackLayout>
</ScrollView>
</ContentPage>
C#
using WhatGoesIn.Models;
namespace WhatGoesIn.Views;
[QueryProperty("Item", "Item")]
public partial class ExportPage : ContentPage
{
WhatGoesInDatabase database;
public ExportItem Item
{
get => BindingContext as ExportItem;
set => BindingContext = value;
}
public ExportPage(WhatGoesInDatabase whatGoesInDatabase)
{
InitializeComponent();
BindingContext = this;
database = whatGoesInDatabase;
}
private async void OnExportClicked(object sender, EventArgs e)
{
ExportItem item = Item as ExportItem; // <= always NULL!
string body = await database.ExportItemAsync(item);
if (string.IsNullOrEmpty(Item.Recipient))
{
await DisplayAlert("No Recipient", "Please choose the recipient of the report.", "OK");
return;
}
if (Email.Default.IsComposeSupported)
{
string subject = "What Goes In Food Log!";
string[] recipients = new[] { Item.Recipient };
var message = new EmailMessage
{
Subject = subject,
Body = body,
BodyFormat = EmailBodyFormat.PlainText,
To = new List<string>(recipients)
};
await Email.Default.ComposeAsync(message);
}
await Shell.Current.GoToAsync("..");
}
private async void OnCancelClicked(object sender, EventArgs e)
{
await Shell.Current.GoToAsync("..");
}
}
Model
using SQLite;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WhatGoesIn.Models
{
public class ExportItem
{
[PrimaryKey, AutoIncrement]
public long ID { get; set; }
public DateTime StartDate { get; set; }
public DateTime ToDate { get; set; }
public string Recipient { get; set; }
}
}
答案1
得分: 2
问题似乎是Item
属性未正确设置。XAML已经为ContentPage
设置了BindingContext
,而您尝试再次设置BindingContext
。这会导致BindingContext
被覆盖,Item
未正确初始化。
看起来您需要从ExportPage
构造函数中删除这行代码:
BindingContext = this;
英文:
It seems like the issue is that the Item
property is not being set properly. The XAML is already setting the BindingContext
for the ContentPage
, and you are trying to set the BindingContext
again. This is causing the BindingContext
to be overwritten and the Item
is not being initialized correctly.
Looks like you need to remove this line from the ExportPage
constructor:
BindingContext = this;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论