Esempio n. 1
0
        private void SecondaryViewCoreLayout_Loaded(object sender, RoutedEventArgs e)
        {
            var appearanceSettings = Microsoft.Toolkit.Mvvm.DependencyInjection.Ioc.Default.GetService <AppearanceSettings>();

            _disposables = new CompositeDisposable(new[]
            {
                appearanceSettings.ObserveProperty(x => x.ApplicationTheme)
                .Subscribe(theme =>
                {
                    ThemeChanged(theme);
                })
            });

            WeakReferenceMessenger.Default.Register <LiteNotificationMessage>(this, (r, m) =>
            {
                if (_CurrentActiveWindowUIContextService.UIContext != UIContext)
                {
                    return;
                }

                TimeSpan duration = m.Value.Duration ?? m.Value.DisplayDuration switch
                {
                    DisplayDuration.Default => TimeSpan.FromSeconds(0.75),
                    DisplayDuration.MoreAttention => TimeSpan.FromSeconds(3),
                    _ => TimeSpan.FromSeconds(0.75),
                };

                LiteInAppNotification.Show(m.Value, duration);
            });
        public PrimaryWindowCoreLayout(
            PrimaryWindowCoreLayoutViewModel viewModel,
            Services.CurrentActiveWindowUIContextService currentActiveWindowUIContextService,
            ILoggerFactory loggerFactory
            )
        {
            DataContext = _viewModel = viewModel;

            this.InitializeComponent();
            _dispatcherQueue = DispatcherQueue.GetForCurrentThread();
            _currentActiveWindowUIContextService = currentActiveWindowUIContextService;
            _logger = loggerFactory.CreateLogger <PrimaryWindowCoreLayout>();


            ContentFrame.NavigationFailed += (_, e) =>
            {
                _logger.ZLogError(e.Exception, "Page navigation failed!!");
            };

            // Resolve Page Title
            ContentFrame.Navigated += (_, e) =>
            {
                _navigationDisposable?.Dispose();
                _navigationDisposable = new CompositeDisposable();

                PageTitle = string.Empty;
                Action <Page, NavigationEventArgs> UpdateOptionalTitleAction = (page, args) =>
                {
                    if (page.DataContext is ITitleUpdatablePage pageVM)
                    {
                        pageVM.GetTitleObservable()
                        .Subscribe(title =>
                        {
                            PageTitle = title;
                            if (pageVM is HohoemaPageViewModelBase vm)
                            {
                                vm.Title = title;
                            }
                        })
                        .AddTo(_navigationDisposable);
                    }
                    else if (page.DataContext is HohoemaPageViewModelBase vm)
                    {
                        var pageNameRaw = e.SourcePageType.FullName.Split('.').LastOrDefault();
                        var pageName    = pageNameRaw.Split('_').FirstOrDefault();
                        if (Enum.TryParse(pageName.Substring(0, pageName.Length - 4), out HohoemaPageType pageType))
                        {
                            PageTitle = vm.Title = pageType.Translate();
                        }
                        else
                        {
                            PageTitle = vm.Title = pageType.ToString();
                        }
                    }
                };

                var page = e.Content as Page;
                if (page.DataContext == null)
                {
                    page.ObserveDependencyProperty(DataContextProperty)
                    .Subscribe(_ =>
                    {
                        UpdateOptionalTitleAction(page, e);
                    })
                    .AddTo(_navigationDisposable);
                }
                else
                {
                    UpdateOptionalTitleAction(page, e);
                }
            };

            ContentFrame.Navigating += ContentFrame_Navigating;

            ContentFrame.Navigated += ContentFrame_Navigated;

            WeakReferenceMessenger.Default.Register <PageNavigationEvent>(this, (r, m) =>
            {
                ContentFrameNavigation(m.Value);
            });

            // Back Navigation Handling
            SystemNavigationManager.GetForCurrentView().BackRequested += App_BackRequested;
            Window.Current.CoreWindow.KeyDown        += CoreWindow_KeyDown;
            Window.Current.CoreWindow.PointerPressed += CoreWindow_PointerPressed;


            if (DeviceTypeHelper.IsDesktop)
            {
                Window.Current.SetTitleBar(DraggableContent as UIElement);
            }

            ContentFrame.Navigated += TVModeContentFrame_Navigated;
            this.GettingFocus      += PrimaryWindowCoreLayout_GettingFocus;


            _viewModel.AppearanceSettings.ObserveProperty(x => x.ApplicationTheme)
            .Subscribe(theme =>
            {
                if (theme == ElementTheme.Default)
                {
                    var appTheme = Helpers.SystemThemeHelper.GetSystemTheme();
                    if (appTheme == ApplicationTheme.Dark)
                    {
                        theme = ElementTheme.Dark;
                    }
                    else
                    {
                        theme = ElementTheme.Light;
                    }
                }

                this.RequestedTheme = theme;
            });

            CoreNavigationView.ObserveDependencyProperty(Microsoft.UI.Xaml.Controls.NavigationView.IsPaneOpenProperty)
            .Subscribe(_ =>
            {
                if (CoreNavigationView.IsPaneOpen)
                {
                    var pinsNVItem = CoreNavigationView.ContainerFromMenuItem(viewModel._pinsMenuSubItemViewModel);
                    if (pinsNVItem is Microsoft.UI.Xaml.Controls.NavigationViewItem nvi)
                    {
                        nvi.IsExpanded = true;
                    }
                }
            });

            WeakReferenceMessenger.Default.Register <LiteNotificationMessage>(this, (r, m) =>
            {
                var payload = m.Value;
                if (_currentActiveWindowUIContextService.UIContext != UIContext)
                {
                    return;
                }

                TimeSpan duration = payload.Duration ?? payload.DisplayDuration switch
                {
                    DisplayDuration.Default => TimeSpan.FromSeconds(1.25),
                    DisplayDuration.MoreAttention => TimeSpan.FromSeconds(1.25 * 3),
                    _ => TimeSpan.FromSeconds(1.25),
                };

                LiteInAppNotification.Show(payload, duration);
            });