ViewModelViewHost在远程桌面连接时请求视图

时间:2017-05-17 06:00:58

标签: c# wpf reactiveui

我有一个实现RoutedViewHost的现有项目,一旦引导它导航到ViewModel,并使用splat来解析View。这是有效的,但是我注意到当我将桌面远程连接到运行应用程序实例的计算机时,在连接时再次解析View,然后再次通过本地登录来断开连接。

我只使用ViewModelViewHost(无需路由器)就可以在一个简单的例子中重现这一点。为什么在远程桌面连接时会重新创建视图(或者更确切地说,为什么视图会被多次解析),以及如何更好地管理它?请注意,只需锁定/解锁计算机时,症状就不会出现。

以下简单示例。

MainWindow.xaml:

<Window x:Class="ReactiveUiRemoteDesktopIssue.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        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:reactiveUi="http://reactiveui.net"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <reactiveUi:ViewModelViewHost ViewModel="{Binding DefaultViewModel}" />
    </Grid>
</Window>

MainWindow.xaml.cs:

using ReactiveUI;
using ReactiveUI.Fody.Helpers;
using Splat;

namespace ReactiveUiRemoteDesktopIssue
{
    public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;

            Locator.CurrentMutable.Register<ExamplePageViewModel>(() => new ExamplePageViewModel());
            Locator.CurrentMutable.Register<IViewFor<ExamplePageViewModel>>(() => new ExamplePageView());

            DefaultViewModel = Locator.Current.GetService<ExamplePageViewModel>();
        }

        [Reactive]
        public object DefaultViewModel { get; set; }
    }
}

ExamplePageViewModel.cs:

namespace ReactiveUiRemoteDesktopIssue
{
    public class ExamplePageViewModel
    {
    }
}

ExamplePageView.xaml:

<UserControl x:Class="ReactiveUiRemoteDesktopIssue.ExamplePageView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>

    </Grid>
</UserControl>

ExamplePageView.xaml.cs:

using System.Windows;
using ReactiveUI;

namespace ReactiveUiRemoteDesktopIssue
{
    public partial class ExamplePageView : IViewFor<ExamplePageViewModel>
    {
        public ExamplePageView()
        {
            InitializeComponent();
        }

        public static readonly DependencyProperty ViewModelProperty =
           DependencyProperty.Register("ViewModel", typeof(ExamplePageViewModel), typeof(ExamplePageView),
               new FrameworkPropertyMetadata(null));

        public ExamplePageViewModel ViewModel
        {
            get { return (ExamplePageViewModel)GetValue(ViewModelProperty); }
            set { SetValue(ViewModelProperty, value); }
        }

        object IViewFor.ViewModel
        {
            get { return ViewModel; }
            set { ViewModel = (ExamplePageViewModel)value; }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

也许您应该将已解析的部分放在app.xaml.cs的OnStartup中,类似于以下内容 代码,这样,viewModelHost将找到绑定到ViewModel的视图并将其解析出来:

public partial class App : Application
{
    public App()
    {

    }

    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
        var builder = new ContainerBuilder();
        //register module(include view and viewmodel)
        builder.RegisterModule<MainModule>();
        var container = builder.Build();
        using (var scope=container.BeginLifetimeScope())
        {
            Locator.CurrentMutable.InitializeSplat();
            Locator.CurrentMutable.InitializeReactiveUI();
            Locator.CurrentMutable.UseSerilogFullLogger();
            Locator.CurrentMutable.Register<IViewFor<TestViewModel>>(() => new TestView());
            scope.Resolve<RegisterView>().Show();
        }
    }
}