簡體   English   中英

New.Net MAUI App 項目拋出“名稱‘InitializeComponent’在當前上下文中不存在”構建錯誤

[英]New .Net MAUI App project throws 'The name 'InitializeComponent' does not exist in the current context' build errors

我嘗試開始使用 .Net MAUI 並按照以下所述的步驟設置我的開發環境:

  1. https://learn.microsoft.com/en-us/do.net/maui/get-started/first-app?pivots=windows
  2. https://learn.microsoft.com/en-us/windows/apps/project-reunion/set-up-your-development-environment#required-workloads-and-components
  3. https://learn.microsoft.com/en-us/xamarin.android/get-started/installation/android-emulator/device-manager?tabs=windows&pivots=windows

我還運行了“maui-check”CLI 工具並檢查了所有內容,但是當我使用 Visual Studio 2019 v16.11.0 Preview 2.0(在 Windows 10 Home 20H2 上運行)創建新的 .NET MAUI 應用程序時,我得到了“The當前上下文中不存在名稱“InitializeComponent”構建錯誤。 它還沒有找到對表單上任何控件的引用,例如“名稱‘CounterLabel’在當前上下文中不存在”

我已經嘗試了這篇文章中的幾乎所有內容當前上下文中不存在名稱“InitializeComponent”,其中包含添加和刪除文件、進行更改以及將它們改回原樣等建議……基本上除了在許願井中扔一分錢之外的所有內容。

我發現一個常見的錯誤是名稱空間不匹配,但這里是我所展示的名稱空間是正確的:

申請.xaml:

<Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:MauiApp1"
             x:Class="MauiApp1.App">
    ...
</Application>

應用程序.xaml.cs

using Microsoft.Maui;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.PlatformConfiguration.WindowsSpecific;
using System;
using Application = Microsoft.Maui.Controls.Application;

namespace MauiApp1
{
    public partial class App : Application
    {
        public App()
        {
            InitializeComponent(); <-- This is throwing the build error...
        }

        protected override IWindow CreateWindow(IActivationState activationState)
        {
            this.On<Microsoft.Maui.Controls.PlatformConfiguration.Windows>()
                .SetImageDirectory("Assets");

            return new Microsoft.Maui.Controls.Window(new MainPage());
        }
    }
}

主頁.xaml:

ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MauiApp1.MainPage"
             BackgroundColor="{DynamicResource PageBackgroundColor}">

            ...
</ContentPage>

MainPage.xaml.cs

using System;
using Microsoft.Maui.Controls;

namespace MauiApp1
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent(); <-- This is throwing the build error...
        }

        int count = 0;
        private void OnCounterClicked(object sender, EventArgs e)
        {
            count++;
            CounterLabel.Text = $"Current count: {count}"; <-- This is throwing the build error...
        }
    }
}

任何幫助將不勝感激!

---===更新===---

我創建的項目的路徑是 c:\develop\c#...... 只要我將項目復制到不包含“c#”的文件夾,它就可以工作。 這顯然會導致后台的某些解析失敗。

我遇到了同樣的問題,看起來當我在 VS 中創建 ContentPage 時它仍然指向 Xamarin Forms。 將命名空間更改為 MAUI 后,我將 XAML 頁面的構建操作(右鍵單擊 Xaml 頁面>>屬性>>構建操作)更新為 MauiXaml,它對我有用。

  1. 關閉 VS2022
  2. 打開VS2022
  3. 使用菜單 VS2022 打開項目(文件 - 打開 - 項目...)

我創建的項目的路徑是 c:\develop\c#...... 只要我將項目復制到不包含“c#”的文件夾,它就可以工作。 這顯然會導致后台的某些解析失敗。

一個非常簡單的答案,但發生在我身上幾次。

確保您使用的是VS 2022的預覽版。很容易意外打開任何其他版本的VS,這將導致錯誤發生。

作為一個絕對初學者並且在 XAML 文件中缺少結尾的“>”,同樣的錯誤突然出現。 因此,也可能是 XAML 錯誤導致此錯誤。

What caused it for me was that I had renamed the xaml and xaml.cs file something else but I hadn't updated it in the ContentPage node in the xaml under x:Class

我正在使用 VS 2022 Preview 17.4.0 Preview 1.0,我不知道為什么,但是當您創建一個新的內容頁面時,它會使用錯誤的命名空間創建。 查看 C# 文件並修復命名空間。 它對我有用。

我正在使用 17.5 VS2022 的預覽版,我添加了以下更改以在添加新頁面后運行應用程序。

*.Xmal 構建操作 -> BundleResource

*.Xmal.cs 生成操作 -> 編譯

我的VS版本

新的 MAUI XAML 頁面

Xmal 構建 -> BundleResource

Xmal.cs 構建 -> 編譯

我找到了解決此問題的方法。 我是 c# 的新手,尤其是 .net Maui 的新手,因此,如果我對事情有不同的誤解,我深表歉意。

首先要做的事情錯誤與 InitializeComponent() 所在的上下文有關。上下文只不過是文件正在查找的 nuget 包依賴項。

更改上下文

  1. 打開 .cs 文件並在編輯器頂部單擊導航欄,如圖所示
  2. 從列表中將上下文更改為 Windows 特定的依賴項。

正如其他人提到的那樣,另一種解決方法是將xaml文件的構建操作更改為MauiXaml

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM