C
C#6mo ago
abdul

Avalonia UI: Issue with code generation

Hello everyone. I am new to C# and am attempting to learn Avalonia. I tried to follow the guide on Avalonia's website for the To Do List app, but I got stuck on making a new view. I am using VS 2022 and I am getting the following error when creating the new view ToDoListView.axaml.cs: CS0103: The Name 'Initialize Component' does not exist in the current context However, the default MainView provided with the solution template has identical code-behind with the obvious exception of differing class names. I suspect this is an issue with code generation, but I'm not sure how I would determine this. Has anyone encountered this before?
4 Replies
SinFluxx
SinFluxx6mo ago
Probably need to share the code of your ToDoListView.axaml and ToDoListview.axaml.cs
abdul
abdul6mo ago
Of course. Here is ToDoListView.axaml:
<UserControl xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="250" d:DesignHeight="450"
x:Class="M2ASCII.ToDoListView">
<DockPanel>
<Button DockPanel.Dock="Bottom"
HorizontalAlignment="Stretch"
HorizontalContentAlignment="Center">
Add Item
</Button>
<StackPanel>
<CheckBox Margin="4">Walk the dog</CheckBox>
<CheckBox Margin="4">Buy some milk</CheckBox>
</StackPanel>
</DockPanel>
</UserControl>
<UserControl xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="250" d:DesignHeight="450"
x:Class="M2ASCII.ToDoListView">
<DockPanel>
<Button DockPanel.Dock="Bottom"
HorizontalAlignment="Stretch"
HorizontalContentAlignment="Center">
Add Item
</Button>
<StackPanel>
<CheckBox Margin="4">Walk the dog</CheckBox>
<CheckBox Margin="4">Buy some milk</CheckBox>
</StackPanel>
</DockPanel>
</UserControl>
And here is ToDoListView.axaml.cs:
using Avalonia.Controls;

namespace M2ASCII.Views;

public partial class ToDoListView : UserControl
{
public ToDoListView()
{
InitializeComponent();
}
}
using Avalonia.Controls;

namespace M2ASCII.Views;

public partial class ToDoListView : UserControl
{
public ToDoListView()
{
InitializeComponent();
}
}
Locked down my error. It was the 6th line in ToDoListView.axaml. It was refererring to a type in the wrong namespace. It should have been M2ASCII.Views.ToDoListView instead of M2ASCII.ToDoListView
SinFluxx
SinFluxx6mo ago
Ah weird, did you manually type that in rather than letting it autogenerate then?
canton7
canton76mo ago
Yeah, quite often refactor-rename on the codebehind / namespace doesn't update the XAML files