private void ResetNavigation() { if (BundleRuntime.Instance.State == BundleRuntimeState.Stopping) { return; } Action resetNavigation = () => { // 重新初始化TreeView SkinUserControl = new Settings(); WelcomeControl = new Introduction(); LayoutDockPanel.Children.Clear(); ContentQueue.Clear(); OpenedPagesCache.Clear(); HideSidebar(); SideBars.Clear(); SideBarSettings.Clear(); TreeViewItemSelectionQueue.Clear(); TopTreeViewItemNavigationNodeTuples.Clear(); NavigationTreeView.Items.Clear(); InitializeNavigationTreeView(); if (BundleActivator.PermissionServiceTracker.IsServiceAvailable) { SetCurrentUser(BundleActivator.PermissionServiceTracker.DefaultOrFirstService.CurrentUserName); } ShowContent(WelcomeControl); }; Dispatcher.Invoke(resetNavigation); // 由界面代理线程来执行UI更新。 }
/// <summary> /// 关闭当前用户控件。 /// </summary> public void CloseCurrentContent() { if (ContentQueue.Count < 2) { return; } bool confirm = ConfigurationService.Get(BundleActivator.Bundle, "ConfirmClosingWindow", true); if (confirm && ModernDialog.ShowMessage( "你确实是否要关闭当前窗口?", "关闭窗口", MessageBoxButton.YesNo) != MessageBoxResult.Yes) { return; } HideSidebar(); // 删除当前显示的控件,即最后一个 var content = ContentQueue[ContentQueue.Count - 1]; ContentQueue.Remove(content); // 删除缓存 var key = string.Empty; foreach (var pair in OpenedPagesCache) { if (pair.Value.Equals(content)) { key = pair.Key; } } if (!string.IsNullOrEmpty(key)) { OpenedPagesCache.Remove(key); } // 从容器中删除该控件 LayoutDockPanel.Children.Remove(content); // 显示倒数第二个用户控件 ShowContent(ContentQueue[ContentQueue.Count - 1]); if (TreeViewItemSelectionQueue.Count > 0) // 设置当前节点选中状态为false,并且删除其选中队列 { (TreeViewItemSelectionQueue[TreeViewItemSelectionQueue.Count - 1] as TreeViewItem).IsSelected = false; TreeViewItemSelectionQueue.RemoveAt(TreeViewItemSelectionQueue.Count - 1); } if (TreeViewItemSelectionQueue.Count > 0) // 选中前一个节点 { (TreeViewItemSelectionQueue[TreeViewItemSelectionQueue.Count - 1] as TreeViewItem).IsSelected = true; } }
/// <summary> /// 树点击事件。 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void NavigationTreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs <object> e) { var treeViewItem = e.NewValue; if (treeViewItem == null || !(treeViewItem is TreeViewItem)) { return; } HideSidebar(); // 跟踪树形节点选中队列,保持刚选中的项为最后一项 TreeViewItemSelectionQueue.Remove(treeViewItem); TreeViewItemSelectionQueue.Add(treeViewItem); // 如果是换肤节点,则显示换肤控件 if (treeViewItem == SkinTreeViewItem) { ShowContent(SkinUserControl); return; } var node = (treeViewItem as TreeViewItem).Tag as NavigationNode; if (!string.IsNullOrEmpty(node.Value)) { if (OpenedPagesCache.ContainsKey(node.Id)) { ShowContent(OpenedPagesCache[node.Id]); return; } LoadingTextBlock.Visibility = System.Windows.Visibility.Visible; LayoutDockPanel.Visibility = System.Windows.Visibility.Hidden; ThreadPool.QueueUserWorkItem(state => { Action action = () => { bool cached = false; // 缓存中是否存在当前打开的页面,如果有,直接显示 // 获取该树节点的Tag属性,其值为导航节点 // 加载导航节点定义的用户控件的类型,这个类型由插件提供。 // 因此,必须使用插件来加载类型。 if (!cached) { var contentClass = node.Bundle.LoadClass(node.Value); if (contentClass != null) { try { // 创建实例,并转换成用户控件。 var component = System.Activator.CreateInstance(contentClass); var userControl = component as UserControl; if (userControl == null) { FileLogUtility.Error(string.Format("The type '{0}' in the Bundle '{1}' does not inherit from UIElement.", node.Value, node.Bundle.SymbolicName)); return; } if (userControl.DataContext != null && userControl.DataContext is ListViewModelBase) { userControl.InputBindings.AddRange((userControl.DataContext as ListViewModelBase).InputKeyBindings); } // 添加到缓存 OpenedPagesCache[node.Id] = userControl; // 将动态创建控件添加到显示区域 ShowContent(userControl); } catch (Exception ex) { FileLogUtility.Error(string.Format("Failed to create UIElement for the type '{0}' in the Bundle '{1}'.", node.Value, node.Bundle.SymbolicName)); FileLogUtility.Error(ex); } } else { FileLogUtility.Error(string.Format("Failed to load class '{0}' from the Bundle '{1}'.", node.Value, node.Bundle.SymbolicName)); } } LoadingTextBlock.Visibility = System.Windows.Visibility.Hidden; LayoutDockPanel.Visibility = System.Windows.Visibility.Visible; }; DispatcherOperation op = Dispatcher.BeginInvoke( DispatcherPriority.Background, action); DispatcherOperationStatus status = op.Status; while (status != DispatcherOperationStatus.Completed) { status = op.Wait(TimeSpan.FromMilliseconds(1000)); if (status == DispatcherOperationStatus.Aborted) { // Alert Someone } } }); } }