Esempio n. 1
0
        public override void WindowDidLoad()
        {
            base.WindowDidLoad();
            this.BindCommand(ViewModel, x => x.DoIt, x => x.doIt);

            realViewModelHost           = new ViewModelViewHost(viewModelHost);
            realViewModelHost.ViewModel = new TestViewModel();
        }
        public void ShouldSetDefaultContentWhenViewModelIsNull()
        {
            var viewLocator = new FakeViewLocator {
                LocatorFunc = t => new FakeWinformsView()
            };
            var defaultContent = new Control();
            var target         = new ViewModelViewHost {
                DefaultContent = defaultContent, ViewLocator = viewLocator
            };

            Assert.Null(target.CurrentView);
            Assert.True(target.Controls.Contains(defaultContent));
        }
        public void SettingViewModelShouldAddTheViewtoItsControls()
        {
            var viewLocator = new FakeViewLocator {
                LocatorFunc = t => new FakeWinformsView()
            };
            var target = new ViewModelViewHost();

            target.ViewLocator = viewLocator;

            target.ViewModel = new FakeWinformViewModel();

            Assert.IsType <FakeWinformsView>(target.CurrentView);
            Assert.Equal(1, target.Controls.OfType <FakeWinformsView>().Count());
        }
        public void ViewModelViewHost_View_Should_Stay_In_Sync_With_ViewModel()
        {
            var defaultContent = new TextBlock();
            var host           = new ViewModelViewHost
            {
                DefaultContent = defaultContent,
                PageTransition = null
            };

            var root = new TestRoot
            {
                Child = host
            };

            Dispatcher.UIThread.RunJobs(DispatcherPriority.Loaded);
            Assert.NotNull(host.Content);
            Assert.Equal(typeof(TextBlock), host.Content.GetType());
            Assert.Equal(defaultContent, host.Content);

            var first = new FirstViewModel();

            host.ViewModel = first;
            Assert.NotNull(host.Content);
            Assert.Equal(typeof(FirstView), host.Content.GetType());
            Assert.Equal(first, ((FirstView)host.Content).DataContext);
            Assert.Equal(first, ((FirstView)host.Content).ViewModel);

            var second = new SecondViewModel();

            host.ViewModel = second;
            Assert.NotNull(host.Content);
            Assert.Equal(typeof(SecondView), host.Content.GetType());
            Assert.Equal(second, ((SecondView)host.Content).DataContext);
            Assert.Equal(second, ((SecondView)host.Content).ViewModel);

            host.ViewModel = null;
            Assert.NotNull(host.Content);
            Assert.Equal(typeof(TextBlock), host.Content.GetType());
            Assert.Equal(defaultContent, host.Content);

            host.ViewModel = first;
            Assert.NotNull(host.Content);
            Assert.Equal(typeof(FirstView), host.Content.GetType());
            Assert.Equal(first, ((FirstView)host.Content).DataContext);
            Assert.Equal(first, ((FirstView)host.Content).ViewModel);
        }
        public override void AwakeFromNib()
        {
            base.AwakeFromNib();

            ViewModel = new MainViewModel();


            this.WhenActivated(d =>
            {
                d(this.BindCommand(ViewModel, vm => vm.MyMenuCommand, c => c.MyMenuItem));

                d(this.BindCommand(ViewModel, vm => vm.MyCommand, c => c.ToggleButton));
                d(this.WhenAnyValue(c => c.ViewModel.SubViewModel).Subscribe(vm => _subViewHost.ViewModel = vm));

                d(this.BindCommand(ViewModel, vm => vm.ShowPanelCommand, controller => controller.PanelButton));

                d(ViewModel.ShowPanelCommand.Subscribe(_ =>
                {
                    var controller = new DummyPanelController {
                        ViewModel = ViewModel.PanelViewModel
                    };
                    Window.BeginSheet(controller.Window, __ =>
                    {
                        controller.Dispose();
                        controller = null;
                    });
                }));

//					d(ViewModel.ShowPanelCommand.Subscribe(_ =>
//						{
//							var controller = new DummyPanelController { ViewModel = ViewModel.PanelViewModel };
//							NSApplication.SharedApplication.BeginSheet(controller.Window, Window, () =>
//								{
//									controller.Dispose();
//									controller = null;
//								});
//						}));

                d(Disposable.Create(() => Console.WriteLine("MainWindowController disposed.")));
            });

            _subViewHost           = new ViewModelViewHost(CustomView);
            _subViewHost.ViewModel = ViewModel.SubViewModel;
        }
        public void ShouldDisposePreviousView()
        {
            var viewLocator = new FakeViewLocator {
                LocatorFunc = t => new FakeWinformsView()
            };
            var target = new ViewModelViewHost();

            target.ViewLocator = viewLocator;

            target.ViewModel = new FakeWinformViewModel();

            Control currentView = target.CurrentView;
            bool    isDisposed  = false;

            currentView.Disposed += (o, e) => isDisposed = true;

            // switch the viewmodel
            target.ViewModel = new FakeWinformViewModel();

            Assert.True(isDisposed);
        }
Esempio n. 7
0
        public void ViewModelViewHostDefaultContentNotNull()
        {
            var uc = new ViewModelViewHost
            {
                DefaultContent = new System.Windows.Controls.Label()
            };
            var window = new WpfTestWindow();

            window.RootGrid.Children.Add(uc);

            var activation = new ActivationForViewFetcher();

            activation.GetActivationForView(window)
            .ToObservableChangeSet(scheduler: ImmediateScheduler.Instance)
            .Bind(out var windowActivated)
            .Subscribe();

            activation.GetActivationForView(uc)
            .ToObservableChangeSet(scheduler: ImmediateScheduler.Instance)
            .Bind(out var controlActivated)
            .Subscribe();

            var loaded = new RoutedEventArgs
            {
                RoutedEvent = FrameworkElement.LoadedEvent
            };

            window.RaiseEvent(loaded);
            uc.RaiseEvent(loaded);

            new[] { true }.AssertAreEqual(windowActivated);
            new[] { true }.AssertAreEqual(controlActivated);

            Assert.NotNull(uc.Content);

            window.Dispatcher.InvokeShutdown();
        }
Esempio n. 8
0
        public void ViewModelViewHostContentNotNullWithViewModelAndActivated()
        {
            Locator.CurrentMutable.Register <TestViewModel>(() => new());
            Locator.CurrentMutable.Register <IViewFor <TestViewModel> >(() => new TestView());

            var uc = new ViewModelViewHost
            {
                DefaultContent = new System.Windows.Controls.Label(),
                ViewModel      = Locator.Current.GetService <TestViewModel>()
            };
            var window = new WpfTestWindow();

            window.RootGrid.Children.Add(uc);

            var activation = new ActivationForViewFetcher();

            activation.GetActivationForView(window).ToObservableChangeSet(scheduler: ImmediateScheduler.Instance).Bind(out var windowActivated).Subscribe();

            activation.GetActivationForView(uc).ToObservableChangeSet(scheduler: ImmediateScheduler.Instance).Bind(out var controlActivated).Subscribe();

            var loaded = new RoutedEventArgs
            {
                RoutedEvent = FrameworkElement.LoadedEvent
            };

            window.RaiseEvent(loaded);
            uc.RaiseEvent(loaded);

            new[] { true }.AssertAreEqual(windowActivated);
            new[] { true }.AssertAreEqual(controlActivated);

            // Test IViewFor<ViewModel> after activated
            Assert.IsType <TestView>(uc.Content);

            window.Dispatcher.InvokeShutdown();
        }
Esempio n. 9
0
        public void ViewModelViewHost_View_Should_Stay_In_Sync_With_ViewModel_And_Contract()
        {
            var defaultContent = new TextBlock();
            var host           = new ViewModelViewHost
            {
                DefaultContent = defaultContent,
                PageTransition = null
            };

            var root = new TestRoot
            {
                Child = host
            };

            Assert.NotNull(host.Content);
            Assert.Equal(typeof(TextBlock), host.Content.GetType());
            Assert.Equal(defaultContent, host.Content);

            var first = new FirstViewModel();

            host.ViewModel = first;

            host.ViewContract = null;
            Assert.NotNull(host.Content);
            Assert.Equal(typeof(FirstView), host.Content.GetType());
            Assert.Equal(first, ((FirstView)host.Content).DataContext);
            Assert.Equal(first, ((FirstView)host.Content).ViewModel);

            host.ViewContract = AlternativeViewContract;
            Assert.NotNull(host.Content);
            Assert.Equal(typeof(AlternativeFirstView), host.Content.GetType());
            Assert.Equal(first, ((AlternativeFirstView)host.Content).DataContext);
            Assert.Equal(first, ((AlternativeFirstView)host.Content).ViewModel);

            var second = new SecondViewModel();

            host.ViewModel = second;

            host.ViewContract = null;
            Assert.NotNull(host.Content);
            Assert.Equal(typeof(SecondView), host.Content.GetType());
            Assert.Equal(second, ((SecondView)host.Content).DataContext);
            Assert.Equal(second, ((SecondView)host.Content).ViewModel);

            host.ViewContract = AlternativeViewContract;
            Assert.NotNull(host.Content);
            Assert.Equal(typeof(AlternativeSecondView), host.Content.GetType());
            Assert.Equal(second, ((AlternativeSecondView)host.Content).DataContext);
            Assert.Equal(second, ((AlternativeSecondView)host.Content).ViewModel);

            host.ViewModel = null;

            host.ViewContract = null;
            Assert.NotNull(host.Content);
            Assert.Equal(typeof(TextBlock), host.Content.GetType());
            Assert.Equal(defaultContent, host.Content);

            host.ViewContract = AlternativeViewContract;
            Assert.NotNull(host.Content);
            Assert.Equal(typeof(TextBlock), host.Content.GetType());
            Assert.Equal(defaultContent, host.Content);
        }