I'm looking for the right approach to declare design-time ViewModel for an Avalonia window.
Some samples suggest
d:DataContext="{d:DesignInstance viewModels:LoginViewModel, IsDesignTimeCreatable=True}"
This throws
XamlParseException at 5:5: Unable to resolve type DesignInstance from namespace http://schemas.microsoft.com/expression/blend/2008
Default Avalonia MVVM template suggests
<Design.DataContext>
<vm:MainWindowViewModel/>
</Design.DataContext>
If the ViewModel takes parameters, it throws
XamlLoadException at 16:10: Unable to find public constructor for type Demo.CloseNonModalDialog:Demo.CloseNonModalDialog.CurrentTimeDialogViewModel()
I guess adding a default parameter-less constructor is an option.
With MvvmLight/WPF, I used to reference the ViewLocator as a static resource
DataContext="{Binding Source={StaticResource Locator}, Path=MainWindow}"
That's an option, although I haven't yet found the right way to declare and reference the resource.
What is the recommended approach here? If I want to show design-time data, I'd say only the 3rd option would work. Which is not the option shown in samples.
CodePudding user response:
Unable to find public constructor for type Demo.CloseNonModalDialog:Demo.CloseNonModalDialog.CurrentTimeDialogViewModel()
You can specify arguments via x:Arguments
XAML directive, see https://docs.microsoft.com/en-us/dotnet/desktop/xaml-services/xarguments-directive
That's an option, although I haven't yet found the right way to declare and reference the resource.
I'd suggest to declare DesignData
class and use x:Static
, it will give you way more flexibility. e. g.
class DesignData
{
public MyViewModel MyViewModel => new MyViewModel(...);
}
d:DataContext="{x:Static local:DesignData.MyViewModel}"
View model creation would also not happen during the normal app execution unlike the StaticResource approach.