Xamarin.Forms - InitializeComponent doesn't exist when creating a new page

前端 未结 30 2289
不知归路
不知归路 2021-01-30 12:36

I\'m using Visual Studio to try out Xamarin.Forms. I\'m trying to follow the guide: http://developer.xamarin.com/guides/cross-platform/xamarin-forms/xaml-for-xamarin-forms/getti

相关标签:
30条回答
  • 2021-01-30 12:47

    UPDATE: This error doesn't usually appear in VS 2015, if it does, here's my original answer:

    Found the solution! Right click on the .XAML file, select Properties.

    You will see a Property called Custom Tool. Change its value from MSBuild:Compile to MSBuild:UpdateDesignTimeXaml

    This will solve the problem. Dont know about the downvote, but here's my screenshot:

    UPDATE:

    It reappears rarely. If it does, just open the Xaml and code behind files and save them. I know, its not the best solution, but it gets the job done.

    0 讨论(0)
  • 2021-01-30 12:47

    This is probably not your case but I had similar problem and mine was xaml and code behind name missmatching. for example according to your sample, if code behind namespace is XamaTest(name of your app in most cases) and class is called MyXamlPage, your xaml class name must be XamaTest.MyXamlPage ([namespace].[classname])

    I was silly after creating an empty xaml with code behind, I changed name of the class in xaml and i was getting this error.

    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 x:Class="XamaTest.MyXamlPage">
        <Label Text="{Binding MainText}" VerticalOptions="Center" HorizontalOptions="Center" />
    </ContentPage>
    

    Code-behind:

    public partial class MyXamlPage : ContentPage
    
    0 讨论(0)
  • 2021-01-30 12:48

    Check the version of Xamarin.Forms package referenced in your project.

    0 讨论(0)
  • 2021-01-30 12:51

    In Xaml Page Properties only set
    Build Action = Embedded resource
    It works in visual studio 2017.

    0 讨论(0)
  • 2021-01-30 12:52
    • I had similar problem in Visual Studio 2013 update 4 environment and I tried all recommendations what I found on the web. Nothing solved my problem.
    • Then I tried workaround approach. I installed Visual Studio 2015 preview and create new blank app with xamarin forms project.
    • When I added new Xaml file everything was OK and issue with InitialComponent method disappeared.
    • I don t know where exactly is the reason of the problem but it seems to be issue around configuration settings.
    0 讨论(0)
  • 2021-01-30 12:56

    I came across this error when;

    1. I removed a ContentPage-cs+xaml and the App-cs+xaml from the project without actually deleting it.
    2. Re-added these to the project in another folder.

    This was fixed by;

    1. Select the .xaml file of the class in which the issue is present.
    2. Right click and select properties.
    3. In Build action select "Embedded Resource"
    4. In Custom Tool type "MSBuild:UpdateDesignTimeXaml"
    5. Clean and Build and it was gone.
    0 讨论(0)
提交回复
热议问题