Example #1
0
        public void AddFlyout(string name, Type viewType, Position position, UnloadBehavior unloadBehavior = UnloadBehavior.SaveAndCloseViewModel, FlyoutTheme flyoutTheme = FlyoutTheme.Adapt)
        {
            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;
            flyout.Position = position;

            var flyoutInfo = new FlyoutInfo(flyout, content);

            // See https://github.com/WildGums/Orchestra/issues/278, we cannot bind this, use workaround for now, see workaround below as well!!!
            //flyout.SetBinding(Flyout.HeaderProperty, new Binding("ViewModel.Title") { Source = content });

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

            // ViewModelChanged handler (Workaround for https://github.com/WildGums/Orchestra/issues/278)
            var vmContainer = content as IViewModelContainer;
            EventHandler <EventArgs> viewModelChangedHandler = null;

            viewModelChangedHandler = (sender, e) =>
            {
                var title = vmContainer?.ViewModel?.Title;
                if (!string.IsNullOrWhiteSpace(title))
                {
                    flyout.Dispatcher.BeginInvoke(() =>
                    {
                        flyout.SetCurrentValue(Flyout.HeaderProperty, title);
                    });
                }
            };

            vmContainer.ViewModelChanged += viewModelChangedHandler;

            // IsOpenChanged handler
            RoutedEventHandler isOpenHandler = null;

#pragma warning disable AvoidAsyncVoid
            isOpenHandler = async(sender, e) =>
#pragma warning restore AvoidAsyncVoid
            {
                var vmContainer = flyout.Content as IViewModelContainer;
                var vm          = vmContainer?.ViewModel;

                if (!flyout.IsOpen)
                {
                    if (vm != null)
                    {
                        switch (unloadBehavior)
                        {
                        case UnloadBehavior.CloseViewModel:
                            await vm.CloseViewModelAsync(null);

                            break;

                        case UnloadBehavior.SaveAndCloseViewModel:
                            await vm.SaveAndCloseViewModelAsync();

                            break;

                        case UnloadBehavior.CancelAndCloseViewModel:
                            await vm.CancelAndCloseViewModelAsync();

                            break;

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

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

                    flyout.IsOpenChanged         -= isOpenHandler;
                    vmContainer.ViewModelChanged -= viewModelChangedHandler;
                }
            };

            flyout.IsOpenChanged += isOpenHandler;

            _flyouts[name] = flyoutInfo;
        }