嘿,我正在用MVVM做一个Windows应用程序和Avalia. 我有一个包含当前视图的导航视图模型.它如下所示:
public class NavigationVM : ViewModelBase
{
public NavigationVM()
{
//first time created assigns the currentvm to loginvm
CurrentVM = new LoginVM(this);
}
private ViewModelBase? currentvm;
public ViewModelBase CurrentVM
{
get => currentvm!;
set => this.RaiseAndSetIfChanged(ref currentvm, value);
}
}
在MainWindow.xaml中,我将其内容绑定到CurrentVM(是的,我将文本设置为导航视图模型).
MainWindow.xaml:个
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="using:MusicPlayer.ViewModels"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:views="clr-namespace:MusicPlayer.Views"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="MusicPlayer.Views.MainWindow"
Icon="/Assets/avalonia-logo.ico"
Title="MusicPlayer"
x:CompileBindings="True"
x:DataType="vm:NavigationVM"
Content="{Binding CurrentVM}"
>
<Window.DataTemplates>
<DataTemplate DataType="{x:Type vm:LoginVM}">
<views:LoginView/>
</DataTemplate>
</Window.DataTemplates>
</Window>
我知道视图模型不能直观地显示,所以我try 使用数据模板.LoginView只是一个带有标签的视图.但它向我展示了这个错误:
提前谢谢!