Avalonia ReactiveUI RoutedViewHost 没有创建嵌入适当的视图
Avalonia ReactiveUI RoutedViewHost not creating embedding appropriate view
我在关注 this tutorial,我确实看到它基于 .net core 2.1 framework
,但适用于 .net 5 avalonia.mvvm
模板。考虑到 .net 5 中的 avalonia.mvvm 模板实现了 ViewLocator.cs
class 所以我没有遵循 Program.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;
}
}
尝试在网上四处搜索,但找不到 运行 遇到相同问题的人。我很感激 help/being 指出了正确的方向。谢谢!
显然答案只是当前 avalonia.mvvm 模板的 ViewLocator.cs
class 与来自 RoutedViewHost
的 IViewLocator
无关=14=] 需要才能工作。只需要将此行添加到 Program.cs
Locator.CurrentMutable.Register(() => new CommandCenterView(), typeof(IViewFor<CommandCenterViewModel>));
我在关注 this tutorial,我确实看到它基于 .net core 2.1 framework
,但适用于 .net 5 avalonia.mvvm
模板。考虑到 .net 5 中的 avalonia.mvvm 模板实现了 ViewLocator.cs
class 所以我没有遵循 Program.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;
}
}
尝试在网上四处搜索,但找不到 运行 遇到相同问题的人。我很感激 help/being 指出了正确的方向。谢谢!
显然答案只是当前 avalonia.mvvm 模板的 ViewLocator.cs
class 与来自 RoutedViewHost
的 IViewLocator
无关=14=] 需要才能工作。只需要将此行添加到 Program.cs
Locator.CurrentMutable.Register(() => new CommandCenterView(), typeof(IViewFor<CommandCenterViewModel>));