簡體   English   中英

創建新的XAML文件時,當前上下文中不存在名稱“ InitializeComponent”

[英]The name 'InitializeComponent' does not exist in the current context when creating new xaml file

我搜索了這個問題的答案已有一段時間,因此希望我的實例可以解決。 我在Visual Studio 2015社區中創建了一個新的Xamarin.Forms共享項目。 我創建了一個新的xaml文件,並將其命名為LoginPage ,並將其放置在共享應用程序項目中名為Pages的文件夾中。 添加Xamarin.Forms命名空間后,它可以識別Page並將其刪除 InitializeComponent之前,我有這個:

using Xamarin.Forms;

namespace BeneFit.Pages
{

    public sealed partial class LoginPage : Page
    {
        public LoginPage()
        {
        InitializeComponent(); //This has the red squiggly
        }
    }
}

這里的Xaml文件完全沒有更改:

<Page
    x:Class="BeneFit.Pages.LoginPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:BeneFit.Pages"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    </Grid>
</Page>

我注意到的一件事是,在這種情況下,構建動作必須是我確定的Page 建議的快速解決方法是添加以下內容:

    private void InitializeComponent()
    {
        throw new NotImplementedException();
    }

但這並不能真正解決問題,因為我知道我必須這樣做。 我一直在嘗試解決這個問題,所以任何幫助或建議都是巨大的。

當您在XAML中創建新的控件或頁面時,也會在后台生成一個隱藏的局部類文件,這將保存您的InitializeComponent()方法。 對於名為LoginPage的頁面,后面生成的代碼將稱為LoginPage.gics ,它應位於obj文件夾下的文件夾結構中。

您的XAML行

xmlns:local="using:BeneFit.Pages"

確實有點時髦,我將其更改為

xmlns:local="clr-namespace:BeneFit.Pages"

轉到每個項目文件夾(共享,.Android,.ios等)並刪除obj和bin文件夾(這將刪除隱藏的部分.g文件)

重建解決方案。

要完成的三項檢查:

1-最新的Xamarin Forms nuget為所有平台安裝了相同版本

2-對於page.Xaml屬性為:

   Build Action: Embedded Resource
   Copy to output dir: Do not copy
   Custom tool (most common error when created page manually): MSBuild:UpdateDesignTimeXaml

對於page.cs,屬性為:

Build action: Compile
Copy to output dir: Do not copy

3-右鍵單擊項目-卸載項目,然后重新加載項目。

看起來像UWP XAML而不是Xamarin Forms XAML。

您在創造錯誤的新事物嗎?

您應該創建一個新的Xamarin.Forms內容頁面

在此處輸入圖片說明

暫無
暫無

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

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