我正在使用毛伊岛开发一款应用程序.我有四页几乎一模一样的书.在第四个示例中,对于从视图传递的模型,我总是得到NULL.我try 了各种方法,从创建另一个页面到复制,再到将工作页面粘贴到XAML和C #中.我知道这可能是一件简单的事情,但它让我难以理解.

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.型号s"
             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.型号s;

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("..");
    }
}

型号

using SQLite;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WhatGoesIn.型号s
{
    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; }
    }
}

推荐答案

似乎问题在于没有正确设置Item属性.XAML已经为ContentPage设置了BindingContext,而您正在try 再次设置BindingContext.这会导致BindingContext被覆盖,而Item未正确初始化.

看起来您需要从ExportPage构造函数中删除此行:

BindingContext = this;

Csharp相关问答推荐

将C#字符串转换为其UTF8编码字符的十六进制表示

发布.NET框架项目将.NET核心元素注入到web. connect中

IComponition.获取IReadOnlyCollection的返回默认属性值

HttpContext. RequestAborted当Android APP失go 连接时未取消

一小时后,自定义缓存停止在App Insight中保存

C#方法从AJAX调用接收NULL

无法创建';';类型的';DbContext';.异常';无法解析类型';Microsoft.EntityFrameworkCore.DbContextOptions`1[Comm的服务

应用程序重新启动后,EFCore列表的BSON反序列化错误

C#-VS2022:全局使用和保存时的代码清理

NET8 MAUI并部署到真实设备上进行测试

C# CompareTo()和Compare()可以返回除-1和1以外的整数吗?

C#普罗米修斯指标

是否有必要在ASP.NET Core中注册可传递依赖项?

如何在我的C#应用程序中设置带有reactjs前端的SignalR服务器?

当试图限制EF Select 的列时,如何避免重复代码?

在ASP.NET Core 8 MVC中本地化共享视图

Celler ArgumentExpression是否期望在所有情况下都捕获允许空值的运算符?

使用动态键从请求体反序列化JSON

为什么我的自定义Json.net转换器不工作?

从HTML元素获取 colored颜色