Beispiel #1
0
        public void AddFlyout(string name, Type viewType, Position position, UnloadBehavior unloadBehavior = UnloadBehavior.SaveAndCloseViewModel)
        {
            Argument.IsNotNullOrWhitespace(() => name);
            Argument.IsNotNull(() => viewType);

            Log.Info("Adding flyout '{0}' with view type '{1}'", name, viewType.FullName);

            var content = (UIElement)_typeFactory.CreateInstance(viewType);

            var flyout = new Flyout();

            flyout.Theme    = FlyoutTheme.Adapt;
            flyout.Position = position;

            var flyoutInfo = new FlyoutInfo(flyout, content);

            flyout.SetBinding(Flyout.HeaderProperty, new Binding("ViewModel.Title")
            {
                Source = content
            });

            ((ICompositeCommand)_commandManager.GetCommand("Close")).RegisterAction(() => { flyout.IsOpen = false; });

            flyout.IsOpenChanged += (sender, e) =>
            {
                if (!flyout.IsOpen)
                {
                    var vmContainer = flyout.Content as IViewModelContainer;
                    if (vmContainer != null)
                    {
                        var vm = vmContainer.ViewModel;
                        if (vm != null)
                        {
                            switch (unloadBehavior)
                            {
                            case UnloadBehavior.CloseViewModel:
                                vm.CloseViewModel(null);
                                break;

                            case UnloadBehavior.SaveAndCloseViewModel:
                                vm.SaveAndCloseViewModel();
                                break;

                            case UnloadBehavior.CancelAndCloseViewModel:
                                vm.CancelAndCloseViewModel();
                                break;

                            default:
                                throw new ArgumentOutOfRangeException("unloadBehavior");
                            }
                        }
                    }

                    flyout.Content     = null;
                    flyout.DataContext = null;
                }
            };

            _flyouts[name] = flyoutInfo;
        }