![](/img/trans.png)
[英].NET Maui The name 'InitializeComponent' does not exist in the current context
[英]Creating a Popup in .Net Maui but "InitializeComponent()" ist not being recognized
我想創建一個帶有兩個按鈕的彈出窗口。 實際上,我以前也像這樣創建過其他 Popup,但這次它不起作用,因為InitializeComponent()
方法does not exist in the current context
。
C#代碼:
using CommunityToolkit.Maui.Views;
using SiRiAs.Lib.Helpers;
namespace SiRiAs.Views.Popups;
public partial class DeletePopup : Popup {
public DeletePopup() {
InitializeComponent();
}
private void Delete_Clicked(object sender, EventArgs e) => Close(DeletePopupResult.Delete);
private void Keep_Clicked(object sender, EventArgs e) => Close(DeletePopupResult.Keep);
}
Xaml:
<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="SiRiAs.Views.Popups.DeletePopup">
<StackLayout>
<StackLayout Padding="15" Spacing="20">
<Button Style="{StaticResource BaseButtonStyle}"
Text="Ja,löschen"
Clicked="Delete_Clicked"
/>
<Button Style="{StaticResource BaseButtonStyle}"
Text="Nein, nicht löschen"
Clicked="Keep_Clicked"
/>
</StackLayout>
</StackLayout>
</toolkit:Popup>
我沒有看到,我正在做與文檔中所寫的不同的事情: https://learn.microsoft.com/de-de/do.net/communitytoolkit/maui/views/popup
此外,如果我在其他彈出窗口中查找InitializeComponent()
,它會將我帶到一個我無法添加的文件,該文件是自動生成的。
另一個 Popup 的自動生成文件,即MoreOptionsPopup
,作為我所說的自動生成文件的參考
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a .NET MAUI source generator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
[assembly: global::Microsoft.Maui.Controls.Xaml.XamlResourceId("SiRiAs.Views.Popups.MoreOptionsPopup.xaml", "Views/Popups/MoreOptionsPopup.xaml", typeof(global::SiRiAs.Views.Popups.MoreOptionsPopup))]
namespace SiRiAs.Views.Popups
{
[global::Microsoft.Maui.Controls.Xaml.XamlFilePath("Views\\Popups\\MoreOptionsPopup.xaml")]
public partial class MoreOptionsPopup : global::CommunityToolkit.Maui.Views.Popup
{
[global::System.CodeDom.Compiler.GeneratedCode("Microsoft.Maui.Controls.SourceGen", "1.0.0.0")]
#if NET5_0_OR_GREATER
#endif
private void InitializeComponent()
{
global::Microsoft.Maui.Controls.Xaml.Extensions.LoadFromXaml(this, typeof(MoreOptionsPopup));
}
}
}
我嘗試構建項目並關閉 VisualStudio,切換分支。 我必須監督一些事情,但我不知道什么和在哪里。
我很感激幫助。
所以,我測試了你的代碼。 現在我 100% 確定一切正常。 (我相信你的 MauiProgram.cs 中有 UseMauiCommunityToolkit())
大多數人忘記設置他們的 XAML 文件的生成操作。 你可以:
右鍵單擊 XAML > 屬性 > 生成操作 MauiXaml。
從項目中刪除該文件並重新添加它。 (它將默認為該構建操作)
我將您的代碼復制到我的應用程序並在我的 android 模擬器上進行了測試,但它在我這邊運行良好。
您可以嘗試刪除 bin 文件夾和 obj 文件夾並重建它。 如果它不起作用,請嘗試重新啟動 Visual Studio。
或者您可以創建一個新的演示並將您的代碼復制到這個新的演示中,然后重試。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.