【问题标题】:.NET MAUI Community Toolkit Popup PopupHandler is incompatible.NET MAUI Community Toolkit Popup PopupHandler 不兼容
【发布时间】:2022-08-04 02:31:04
【问题描述】:

我开始使用 .NET MAUI。我刚开始开发就遇到了问题。 我想显示一个弹出窗口,我正在使用社区工具包。

我所做的只是:

我创建了一个新的 .NET MAUI 应用程序项目,安装了社区工具包 NuGet 包(当然还有启动类中的 .UseMauiCommunityToolkit)并为弹出窗口添加了一个 XAML 文件:

<?xml version=\"1.0\" encoding=\"utf-8\" ?>
<toolkit:Popup xmlns=\"http://schemas.microsoft.com/dotnet/2021/maui\"
               xmlns:x=\"http://schemas.microsoft.com/winfx/2009/xaml\"
               xmlns:toolkit=\"http://schemas.microsoft.com/dotnet/2022/maui/toolkit\"
               x:Class=\"TestApp.ProfilePopup\">

    <VerticalStackLayout>
        <Label Text=\"This is a very important message!\" />
    </VerticalStackLayout>

</toolkit:Popup>

我没有这个弹出窗口的部分课程

我刚刚修改了 MainPage 上的按钮以显示弹出窗口:

    private void OnCounterClicked(object sender, EventArgs e)
    {

        var popup = new ProfilePopup();

        this.ShowPopup(popup);

    }

如果我运行此应用程序并单击按钮以显示弹出窗口,我将收到错误消息:

CommunityToolkit.Maui.Core.Handlers.PopupHandler found for TestApp.ProfilePopup is incompatible

如果我在 C# 中创建弹出窗口,它可以工作:

    private void OnCounterClicked(object sender, EventArgs e)
    {
        var popup = new Popup
        {
            Content = new VerticalStackLayout
            {
                Children =
        {
            new Label
            {
                Text = \"This is a very important message!\"
            }
        }
            }
        };

        this.ShowPopup(popup);

    }

知道我做错了什么吗?

谢谢!

马库斯

  • 我认为这看起来像是工具包上的一个错误。您可以在存储库中提出问题吗? github.com/CommunityToolkit/Maui
  • 错误出现在有问题的代码中,而不是 Toolkit Popup 中。看我的回答。

标签: popup maui


【解决方案1】:

我重现了错误消息。

原因:“我没有这个弹出窗口的部分课程”.

那是行不通的。没有它,就没有InitializeComponent 电话。结果不是有效的视图。


要解决问题,您必须拥有

文件ProfilePopup.xaml.cs 包含:

public partial class ProfilePopup : CommunityToolkit.Maui.Views.Popup
{
    public ProfilePopup()
    {
        InitializeComponent();
    }
}

我使用以下步骤生成自定义视图:

  1. 项目/添加新项目/.NET MAUI ContentView (XAML)。
  2. 命名为“MyView”。这会将两个文件添加到项目中:MyView.xaml 和 MyView.xaml.cs。
  3. 在MyView.xaml 中,添加所需的xmlns 并更改基类。
    曾是:
    <ContentView xmlns=...
        ...
        x:Class=...>
    ...
    </ContentView>
    

    改成:

    <toolkit:Popup xmlns=
        ...
        xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
        x:Class=...>
    ...
    </toolkit:Popup>
    
    1. 在MyView.xaml.cs 中,更改基类。
      曾是:
    public partial class ProfilePopup : ContentView
    

    改成:

    public partial class ProfilePopup : CommunityToolkit.Maui.Views.Popup
    

【讨论】:

  • 如果 InitializeComponent() 给出“未定义”的编译错误,请确保 XAML x:Class="MyProj.a.b.popup" 与 C# 代码的命名空间匹配。有人可能认为 InitializeComponent() 应该存在于类 Popup 中,但显然它是从 XAML 创建的。
【解决方案2】:

Microsoft页面上的文档应该完成(我用作模板):

https://docs.microsoft.com/en-us/dotnet/communitytoolkit/maui/views/popup

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center。
  • 具体来说,不要只依赖链接。将基本细节复制到答案中,以便它可以单独使用。 (请保留链接,并说明信息来自那里。)
  • 实际上,问题是该页面上 xaml 的副本。那个链接才不是说明如果您使用 XAML 代码 sn-p,您还必须将 c# 构造函数包含在InitializeComponent 中,如我的回答中所述。
【解决方案3】:

我遇到了同样的问题。我忘了添加 .UseMauiCommunityToolkit();到 MauiProgram.cs

你也可以试试这个: var popup = new PopupPage(); 等待 PopupExtensions.ShowPopupAsync(this, popup);

【讨论】:

    猜你喜欢
    • 2022-12-11
    • 2023-01-16
    • 2021-11-16
    • 2021-12-28
    • 2010-11-13
    • 1970-01-01
    • 2022-11-18
    • 1970-01-01
    • 2022-12-01
    相关资源
    最近更新 更多