❔ AvaloniaUI InvalidCastException
I started playing around with AvaloniaUI and I get a the (
InvalidCastException
s.
the MainWindow
's XAML:
<Window 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"
xmlns:main="clr-namespace:cap5lut.net.Growbox.Client.Views.Main"
xmlns:controls="clr-namespace:cap5lut.net.Growbox.Client.Controls"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="cap5lut.net.Growbox.Client.Views.Main.MainWindow"
x:DataType="main:MainWindowViewModel"
x:CompileBindings="True"
Title="{Binding Title}">
<controls:ServerAddForm
DataContext="{Binding ServerAddForm}"/>
</Window>
<Window 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"
xmlns:main="clr-namespace:cap5lut.net.Growbox.Client.Views.Main"
xmlns:controls="clr-namespace:cap5lut.net.Growbox.Client.Controls"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="cap5lut.net.Growbox.Client.Views.Main.MainWindow"
x:DataType="main:MainWindowViewModel"
x:CompileBindings="True"
Title="{Binding Title}">
<controls:ServerAddForm
DataContext="{Binding ServerAddForm}"/>
</Window>
MainWindowViewModel
:
public class MainWindowViewModel : Model
{
private string _title;
private ServerAddFormModel _serverAddForm;
public MainWindowViewModel()
{
_title = $"Servers - Growbox - v{Assembly.GetExecutingAssembly().GetName().Version} - cap5lut.net";
_serverAddForm = new ServerAddFormModel();
}
public string Title
{
get => _title;
set => SetField(ref _title, value);
}
public ServerAddFormModel ServerAddForm
{
get => _serverAddForm;
set => SetField(ref _serverAddForm, value);
}
}
public class MainWindowViewModel : Model
{
private string _title;
private ServerAddFormModel _serverAddForm;
public MainWindowViewModel()
{
_title = $"Servers - Growbox - v{Assembly.GetExecutingAssembly().GetName().Version} - cap5lut.net";
_serverAddForm = new ServerAddFormModel();
}
public string Title
{
get => _title;
set => SetField(ref _title, value);
}
public ServerAddFormModel ServerAddForm
{
get => _serverAddForm;
set => SetField(ref _serverAddForm, value);
}
}
Model
is just an INotifyPropertyChanged
implementation)4 Replies
the
and here the
ServerAddForm
's XAML:
<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"
xmlns:controls="clr-namespace:cap5lut.net.Growbox.Client.Controls"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="cap5lut.net.Growbox.Client.Controls.ServerAddForm"
x:DataType="controls:ServerAddFormModel"
x:CompileBindings="True">
<StackPanel>
<Label>URI:</Label>
<TextBox Text="{Binding ServerUri}"/>
<Button>Add</Button>
</StackPanel>
</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"
xmlns:controls="clr-namespace:cap5lut.net.Growbox.Client.Controls"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="cap5lut.net.Growbox.Client.Controls.ServerAddForm"
x:DataType="controls:ServerAddFormModel"
x:CompileBindings="True">
<StackPanel>
<Label>URI:</Label>
<TextBox Text="{Binding ServerUri}"/>
<Button>Add</Button>
</StackPanel>
</UserControl>
ServerAddFormModel
:
public class ServerAddFormModel : Model
{
private string? _serverUri;
public string? ServerUri
{
get => _serverUri;
set => SetField(ref _serverUri, value);
}
}
public class ServerAddFormModel : Model
{
private string? _serverUri;
public string? ServerUri
{
get => _serverUri;
set => SetField(ref _serverUri, value);
}
}
and here is the actual error:
the exception is silent when i do not run with a debugger and it works afterwards, but with a debugger im getting it.
what am i doing wrong?
just in case its important, here is the
Model
implementation:
public abstract class Model : INotifyPropertyChanged
{
public event PropertyChangedEventHandler? PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
protected bool SetField<T>(ref T field, T value, [CallerMemberName] string? propertyName = null)
{
if (EqualityComparer<T>.Default.Equals(field, value)) return false;
field = value;
OnPropertyChanged(propertyName);
return true;
}
}
public abstract class Model : INotifyPropertyChanged
{
public event PropertyChangedEventHandler? PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
protected bool SetField<T>(ref T field, T value, [CallerMemberName] string? propertyName = null)
{
if (EqualityComparer<T>.Default.Equals(field, value)) return false;
field = value;
OnPropertyChanged(propertyName);
return true;
}
}
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.