我正在关注本教程,我确实看到它基于.net core 2.1 framework
,但适用于.net 5 avalonia.mvvm
模板。考虑到 .net 5 中的avalonia.mvvmProgram.cs
模板实现了ViewLocator.cs
该类,所以我不相信我必须使用Locator
. 然而,当我在Router.NavigationStack.Count
增量时运行应用程序时,视图仍保留其默认内容。这是代码
/// MainWindow.axaml
<Design.DataContext>
<vm:MainWindowViewModel/>
</Design.DataContext>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<rxui:RoutedViewHost Grid.Row="0" Router="{Binding Router}" PageTransition="{x:Null}">
<rxui:RoutedViewHost.DefaultContent>
<TextBlock Text="Main Window"
HorizontalAlignment="Center"
VerticalAlignment="Center" />
</rxui:RoutedViewHost.DefaultContent>
</rxui:RoutedViewHost>
<StackPanel Grid.Row="1" Orientation="Horizontal" Margin="15">
<StackPanel.Styles>
<Style Selector="StackPanel > :is(Control)">
<Setter Property="Margin" Value="2"/>
</Style>
<Style Selector="StackPanel > TextBlock">
<Setter Property="VerticalAlignment" Value="Center"/>
</Style>
</StackPanel.Styles>
<Button Content="Go next" Command="{Binding GoNext}" />
<Button Content="Go back" Command="{Binding GoBack}" />
<TextBlock Text="{Binding Router.NavigationStack.Count}" />
</StackPanel>
</Grid>
//MainWindowViewModel.cs
public class MainWindowViewModel : ViewModelBase, IScreen
{
public RoutingState Router { get; } = new RoutingState();
public ReactiveCommand<Unit, IRoutableViewModel> GoNext { get; }
public ReactiveCommand<Unit, Unit> GoBack => Router.NavigateBack;
public MainWindowViewModel()
{
GoNext = ReactiveCommand.CreateFromObservable(
() => Router.Navigate.Execute(new CommandCenterViewModel(this))
);
}
}
//CommandCenterView.axaml
<UserControl xmlns="https://github.com/avaloniaui"
xmlns:vm="using:GuiClient.ViewModels"
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="800" d:DesignHeight="450"
x:Class="GuiClient.Views.CommandCenterView">
<StackPanel HorizontalAlignment="Center"
VerticalAlignment="Center">
<TextBlock x:Name="PathTextBlock" Text="Hi, I'm the first view!" />
<TextBlock Text="{Binding UrlPathSegment}" />
</StackPanel>
</UserControl>
///CommandCenterViewModel.cs
public class CommandCenterViewModel : ViewModelBase, IRoutableViewModel
{
public string UrlPathSegment { get; } = Guid.NewGuid().ToString().Substring(0, 5);
public IScreen HostScreen { get; }
public CommandCenterViewModel(IScreen screen) => HostScreen = screen;
}
//ViewLocator.cs in case this is relevant
public class ViewLocator : IDataTemplate
{
public IControl Build(object data)
{
var name = data.GetType().FullName!.Replace("ViewModel", "View");
var type = Type.GetType(name);
if (type != null)
{
return (Control)Activator.CreateInstance(type)!;
}
else
{
return new TextBlock { Text = "Not Found: " + name };
}
}
public bool Match(object data)
{
return data is ViewModelBase;
}
}
尝试在网上搜索,找不到遇到同样问题的人。我很感激帮助/被指出正确的方向。谢谢!