Exemplo n.º 1
0
        private static void HandleDisplay(FragmentContentViewer viewer, ViewModelFragment oldValue, ViewModelFragment newValue)
        {
            if (oldValue != null)
            {
                oldValue.Dispose();
            }

            if (newValue != null)
            {
                var viewType = FragmentManager.ResolveFragmentView(newValue.GetType());

                if (viewType != null)
                {
                    var view = Activator.CreateInstance(viewType) as FrameworkElement;

                    var newFragment = newValue as ViewModelFragment;

                    view.DataContext = newFragment;
                    newFragment.UI.SetUIElement(view, viewer);

                    viewer.PART_ContentPresenter.Content = view;

                    if (viewer.AutoInvoke)
                    {
                        newFragment.Invoke(viewer.DataContext as ViewModelBase, viewer.InvokeParameter);
                    }
                }
            }
            else
            {
                viewer.PART_ContentPresenter.Content = null;
            }
        }
        public async Task <NepAppUIManagerDialogResult> ShowDialogFragmentAsync <T>(object parameter = null) where T : NepAppUIDialogFragment
        {
            await BeginShowingDialogAsync();

            //used to handle resizing the dialog
            ApplicationView appView = ApplicationView.GetForCurrentView();

            appView.VisibleBoundsChanged += NepAppUIManagerOverlayHandle_VisibleBoundsChanged;
            Window.Current.SizeChanged   += Current_SizeChanged;

            Rect bounds = GetScreenBounds();

            ResizeInlineFrameDialog(bounds.Height, bounds.Width);

            var fragment = Activator.CreateInstance <T>() as NepAppUIDialogFragment;

            var viewType = FragmentManager.ResolveFragmentView <T>();
            var view     = Activator.CreateInstance(viewType) as Control;

            view.DataContext = fragment;

            inlineFrame.Content = view;

            KeyEventHandler escapeHandler         = null;
            bool            escapeHandlerReleased = false;

            escapeHandler = new KeyEventHandler((s, e) =>
            {
                //handles the escape button. On Xbox, the "B" button counts as Escape here.
                if (e.Key == Windows.System.VirtualKey.Escape)
                {
                    e.Handled             = true;
                    view.KeyDown         -= escapeHandler;
                    escapeHandlerReleased = true;
                    fragment.ResultTaskCompletionSource.SetResult(NepAppUIManagerDialogResult.Declined);
                }
            });
            view.KeyDown += escapeHandler;

            var navManager = WindowManager.GetNavigationManagerForCurrentView()
                             .GetNavigationServiceFromFrameLevel(FrameLevel.Two);
            EventHandler <NavigationManagerPreBackRequestedEventArgs> backHandler = null;
            bool backHandlerReleased = false;

            backHandler = new EventHandler <NavigationManagerPreBackRequestedEventArgs>((o, e) =>
            {
                //hack to handle the back button.
                e.Handled = true;
                navManager.PreBackRequested -= backHandler;
                backHandlerReleased          = true;
                fragment.ResultTaskCompletionSource.SetResult(NepAppUIManagerDialogResult.Declined);
            });
            navManager.PreBackRequested += backHandler;

            var result = await fragment.InvokeAsync(parameter);

            if (!backHandlerReleased)
            {
                navManager.PreBackRequested -= backHandler;
            }

            if (!escapeHandlerReleased)
            {
                view.KeyDown -= escapeHandler;
            }

            appView.VisibleBoundsChanged -= NepAppUIManagerOverlayHandle_VisibleBoundsChanged;
            Window.Current.SizeChanged   -= Current_SizeChanged;

            await EndShowingDialogAsync();

            fragment.Dispose();

            return(result);
        }