Dave
Dave

Reputation: 69

InitializeComponent does not exist in the current context

I am new to Avalonia and now face the issue, that the function InitializeComponent does not exist in the current context. I do not have this error in any other view. I hope you can help me find the problem.

MainWindow.axaml.cs:

using Avalonia.Controls;

namespace MBotSoftware.Views;

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }
}

MainWindow.axaml:

<Window xmlns="https://github.com/avaloniaui"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vm="using:MBotSoftware.ViewModels"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:views="clr-namespace:MBotSoftware"
        mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
        x:Class="MBotSoftware.Views.MainWindow"
        Icon="/Assets/avalonia-logo.ico"
        Title="MBotSoftware">
    <StackPanel>
        <views:MBotLandingView />
    </StackPanel>
</Window>

Upvotes: 0

Views: 1082

Answers (3)

Ian Bailey-Mortimer
Ian Bailey-Mortimer

Reputation: 345

Posting what happened to me here in case it helps others too. I copied a WPF XAML file into my Avalonia project and got this error because I forgot to rename the file .axaml instead of .xaml :)

Upvotes: 0

B0lver
B0lver

Reputation: 41

As far as i know this error indicates that there is no class that can execute InitializeComponent(); method. This is because C# code cannot be associated with markup file. And this is because you might have errors in your markup. So please check errors in your MainWindow.axaml and try to Clear Solution.

Maybe this line is the problem, if you deleted ViewModels directory:

        xmlns:vm="using:MBotSoftware.ViewModels"

This is only my understanding of that issue. But in a wast majority of such cases, careful check for markup errors is often the key.

Upvotes: 0

radoslawik
radoslawik

Reputation: 1202

It could be the mismatch of Avalonia version you use and the version of the template. If this is the case adding this in your axaml file should fix the problem.

using Avalonia.Markup.Xaml;

private void InitializeComponent()
{
   AvaloniaXamlLoader.Load(this);
}

Upvotes: 1

Related Questions