When implementing C# Maui XAML popup what can cause error 'InitializeComponent does not exist' or 'Popup could not be found' and what is resolution?

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

When implementing C# Maui XAML popup what can cause error 'InitializeComponent does not exist' or 'Popup could not be found' and what is resolution?

问题

当实现一个C# Maui XAML弹出窗口时,可能会出现一些问题和错误,导致它无法正常工作。

最常见的错误信息如下:

  • 当前上下文中不存在名称为'InitializeComponent'的内容
  • 找不到类型或命名空间名为'Popup'(是否缺少using指令或程序集引用?)

这些错误信息的原因是什么,有哪些解决方法?

英文:

When implementing a C# Maui XAML popup, there are a few types of problems and\or mistakes that can occur which cause it to fail.

The most common error messages appear to be the following:

  • The name 'InitializeComponent' does not exist in the current context
  • The type or namespace name 'Popup' could not be found (are you
    missing a using directive or an assembly reference?)

What can cause these error messages and what are\is the resolution(s)?

答案1

得分: 1

我创建了这个问题,也是为了提供这个答案。我想解释一下为什么我认为这种情况会发生在一些人身上,特别是那些对MAUI、XAML或MAUI弹出窗口不太熟悉的人。

如果其他开发人员知道这些错误消息和解决方法的其他原因,请提供这些信息以帮助其他人。

特别是如果你正在按照下面链接中的Microsoft示例进行操作。如果你不小心,可能会遇到麻烦。

https://learn.microsoft.com/en-us/dotnet/communitytoolkit/maui/views/popup#building-a-popup

我认为Microsoft的文章经常过于假设每个读者都对所讨论的技术、工具或主题有经验(在这种情况下是MAUI和ASPX文件)。他们从他们(有经验的)的角度来看待问题,而不是从读者的角度来看,读者的经验和信心水平各不相同。我想大多数人/公司都这样做。然而,这是不幸的,因为他们没有帮助最重要的(对于他们的用户群体来说)读者:新手和“我正在尝试看看是否喜欢它”的开发人员。

简单回答一下,这两个错误消息可能是由以下任何一个或所有三个原因造成的:

  1. 弹出窗口的.xaml文件中以“x:Class=”开头的行中的命名空间不正确或缺失。
  2. 弹出窗口的.xaml.cs文件中可能缺少语句“using CommunityToolkit.Maui.Views;”。
  3. 弹出窗口的.xaml.cs文件中可能缺少命名空间行。

如果这个回答解决了你的问题,那么可以停在这里。但是,如果你在解决这些可能的问题时遇到困难,请继续阅读,特别是如果你依赖上面链接中的Microsoft文章。

大多数人可能会想要将弹出窗口添加到他们已经在工作中的项目中。因此,他们的项目名称(和主项目命名空间)已经定义好了。注意:如果你对C#中的“命名空间”不熟悉,那么你是新手,应该在继续之前阅读“C#命名空间”的相关内容。

在上面提供的Microsoft文章中,在“定义弹出窗口”下面提供了创建弹出窗口的步骤。如果你是新手,可能很难理解这些步骤。如果你在按照步骤操作时不小心,可能会导致上述3个错误消息之一。下面的步骤与Microsoft文章中的“定义弹出窗口”部分相同,但提供了更多细节,并指出可能导致错误消息的3个原因所在的位置。

  1. 在Visual Studio中右键单击你的项目
  2. 选择“添加新项”
  3. 在左侧菜单中选择“.NET MAUI”
  4. 在中间弹出的“子菜单”中选择“.NET MAUI ContentView (XAML)”(在点击“添加”按钮之前...)
  5. 为了这个示例,将文件命名为“SimplePopup.xaml”(在点击“添加”按钮之前...)我们使用SimplePopup.xaml,因为示例中就是这样的。
  6. 现在点击“添加”按钮。
  7. SimplePopup.xaml和SimplePopup.xaml.cs代码文件将被创建。
  8. 现在这是你可能会遇到麻烦的地方,如果你对Maui和xaml文件不够熟悉的话。有经验的程序员应该忍住。我们都曾经历过这个阶段。
  9. 他们展示的下一步是将他们的代码复制粘贴到你的XAML文件中。所以,去做吧。像“复制”按钮建议你那样逐字逐句地复制。
  10. 他们展示的下一步是将他们的代码复制粘贴到你的C#(xaml.cs)文件中。所以,去做吧。像“复制”按钮建议你那样逐字逐句地复制。
  11. 现在构建你的项目(即使你已经看到了错误)
  12. 你可能会注意到在你的C#(xaml.cs)文件中,IntializeComponent()命令被下划线标记。如果你尝试构建你的项目,你会得到以下错误:
    (A) 当前上下文中不存在“InitializeComponent”名称
    (B) 找不到类型或命名空间名称“Popup”(是否缺少using指令或程序集引用?)
  13. 你可能还会注意到.cs文件中的Popup显示为下划线,并且显示找不到命名空间“Popup”。

所以,在这一点上,你开始尝试其他网站上关于这个问题的一些疯狂建议。

在你做所有这些之前,先停下来检查这些事情。

首先,在文章中,示例代码显示XAML文件中有x:Class="MyProject.SimplePopup"。
其中的MyProject应该有一个免责声明,类似于x:Class="[MyProject].SimplePopup"(其中[MyProject]是你项目中使用的命名空间)。

如果你的XAML文件中确实有"x:Class="MyProject.SimplePopup",你需要将[MyProject]更改为你项目的命名空间。如果你不知道命名空间,打开与你的Popup文件位于同一位置的另一个.cs文件。你会看到类似于“namespace [MyExampleMauiNameSpace];”的内容,其中[MyExampleMauiNameSpace]是你在Popup .cs文件中需要的文本。很可能[MyExampleMauiNameSpace]与你的项目名称类似。将Popup .cs文件中显示的[MyProject]命名空间替换为你项目中的[MyExampleMauiNameSpace]。

所以,一旦你在你的XAML中有了那行,它可能看起来像这样:

x:Class="[MyExampleMauiNameSpace].SimplePopup">

其中[MyExampleMauiNameSpace]应该由与你的Popup文件位于同一位置的其他.cs文件中显示的命名空间替换。

现在,如果你犯了一个错误,即逐字逐句地从Microsoft的示例代码中复制和粘贴C#(xaml.cs)代码(就像“复制”按钮建议你那样做),可能会覆盖你的xaml.cs文件中的以下行。

`using CommunityToolkit.Maui.Views;

"namespace [MyExampleMauiNameSpace];"`

...其中[MyExampleMauiNameSpace]应该由你的其他.cs文件中显示的命名空间替换。

如果你在这个示例中的xaml.cs文件中没有上面显示的using语句或命名空间行,那么你可能已经复制和粘贴覆盖了它们。你需要将它们放回到你的xaml.cs文件的顶部。

最终,你的文件应该如下所示:

(SimplePopup.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="[MyExampleMauiNameSpace].SimplePopup">
    <VerticalStackLayout>
        <Label Text="This is a very important message!" />
    </VerticalStackLayout>
</toolkit:Popup>

(SimplePopup.xaml.cs代码如下)

using CommunityToolkit.Maui.Views;
namespace [MyExampleMauiNameSpace];

public partial class SimplePopup : Popup
{
    public SimplePopup()
    {
        InitializeComponent();
    }
}

希望这对于一些在实现C# Maui XAML弹出窗口时遇到困难的人有所帮助。

英文:

I created this question with the intention of providing this answer also. I want to explain why I think this happens to some people, especially if they are not very experienced in MAUI, XAML, or the MAUI popup.

If other developers know of additional reasons for these error messages and the resolution, please provide that also to help others.

This especially applies if you are following this Microsoft Example at the link below. If you aren't careful, it can get you in trouble.

https://learn.microsoft.com/en-us/dotnet/communitytoolkit/maui/views/popup#building-a-popup

I think Microsoft articles often assume too much that every reader is experienced in the technology, tool, or topic that the article is about (in this case MAUI and ASPX files). They view it from their (experienced) perspective, not from the readers perspective who will have varying levels of experience and confidence. I suppose most people\companies do this. This is unfortunate though because they aren't helping the most important (to their user base) readers: The newbies and the "I'm trying it out to see if I like it" developers.

To give a quick answer, the two error messages could be caused by any one of a combination of, or all three of the following reasons:

  1. The namespace in the line that begins with "x:Class=" in the popup’s .xaml file is incorrect or missing.
  2. The statement "using CommunityToolkit.Maui.Views;" could be missing from the popup’s .xaml.cs file.
  3. The namespace line could be missing from the popup’s .xaml.cs file.

If that answers the problem for you, then feel free to stop here. But, if you are having difficulty resolving these possible issues read on, especially if you are relying on the Microsoft article at the link above.

Most people will probably be looking to add the popup to a project they already are working on. So, their project name (and main project namespace) will already be defined. Note: If you aren't familiar with "namespaces" in C#, then you are new and you should read up on "C# namespaces" before continuing.

In the Microsoft article above provided under: "Defining your Popup" are steps for creating a popup. If you are new, they may be difficult to follow. They also may lead to one of the 3 error messages if you aren't careful when following the steps. The steps below are the same as the section "Defining your Popup" in the Microsoft article with some more detail and pointing out where it might cause one of the 3 reasons (listed above) for the error messages.

  1. Right click on your project within Visual Studio
  2. Select Add New Item
  3. In the menu on the left side select ".NET MAUI"
  4. In the "sub-menu" that comes up in the middle, select ".NET MAUI ContentView (XAML)" (Before clicking "Add" button...)
  5. For this example, name the file "SimplePopup.xaml" (Before you click "Add" button...) We are using SimplePopup.xaml since that is what is in the example.
  6. Now click the "Add" button.
  7. The SimplePopup.xaml and the SimplePopup.xaml.cs code behind files will be created.
  8. Now this is where you can get in trouble if you aren't experienced enough with Maui and xaml files. Experienced programmers should bite their tongue. We've all been there.
  9. The next step they show is to copy and paste their code into your XAML file. So, go ahead and do that. Copy it verbatim like the "Copy" button suggests you should do.
  10. The next step they show is to copy and paste their code into your C# (xaml.cs) file. So, go ahead and do that. Copy it verbatim like the "Copy" button suggests you should do.
  11. Now build your project (even if you see the errors already)
  12. You might notice that in your C# (xaml.cs) file, the IntializeComponent() command is underlined. And if you tried to build your project, you get the following errors:
    (A) The name 'InitializeComponent' does not exist in the current context
    (B) The type or namespace name 'Popup' could not be found (are you missing a using directive or an assembly reference?)
  13. You might also notice that Popup in the .cs file is showing underlined and saying that the namespace 'Popup' could not be found.

So, at this point you start trying a bunch of the crazy suggestions given on other sites about this problem.

Before you do all that, stop and check these things first.

First in the article the example code shows the XAML file has x:Class="MyProject.SimplePopup".
The MyProject in that should have a disclaimer like x:Class="[MyProject].SimplePopup" (where [MyProject] is the the namespace used in your project).

If you literally have "x:Class="MyProject.SimplePopup" in your XAML file, you need to change [MyProject] to the namespace of your project. If you don't know the namespace, open another .cs file in the same location as your Popup files. You will see something like "namespace [MyExampleMauiNameSpace];" where [MyExampleMauiNameSpace] is the text you need in your popup .cs file. Most likely the [MyExampleMauiNameSpace] will be similar to the name of your project. Replace the [MyProject] namespace showing in your popup .cs file with the [MyExampleMauiNameSpace] from the other .cs file in your project.

So, once you have that line in your XAML, it will probably look like:

x:Class="[MyExampleMauiNameSpace].SimplePopup">

where [MyExampleMauiNameSpace] should be replaced by the namespace showing in your other .cs file that is in same location as your popup files.

Now, there may be one other change that you need to make if you made the mistake of copying and pasting Microsoft's C# (xaml.cs) code verbatim from their example code (like the "copy" button suggests you should do).

If you did that, you may have overwritten the following lines in your xaml.cs file.

`using CommunityToolkit.Maui.Views;

"namespace [MyExampleMauiNameSpace];" `

...where [MyExampleMauiNameSpace] should be replaced by the namespace showing in your other .cs files in the same location.

If you don't have the using statement shown above or the namespace line above in your xaml.cs file in this example, you probably copied and pasted over them. You need to put them back at the top of your xaml.cs file.

Ultimately your files should look like this:

(SimplePopup.xaml code below)

<?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="[MyExampleMauiNameSpace].SimplePopup">
    <VerticalStackLayout>
        <Label Text="This is a very important message!" />
    </VerticalStackLayout>
</toolkit:Popup>

(SimplePopup.xaml.cs code below)

using CommunityToolkit.Maui.Views;
namespace [MyExampleMauiNameSpace];

public partial class SimplePopup : Popup
{
    public SimplePopup()
    {
        InitializeComponent();
    }
}

I hope this helps some people having trouble implementing C# Maui XAML popup.

huangapple
  • 本文由 发表于 2023年8月9日 06:46:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76863592.html
匿名

发表评论

匿名网友

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

确定