Selaa lähdekoodia

TreeNodeViewModel移除Child管理,直接使用treeNodedata的children

tanghai 11 vuotta sitten
vanhempi
commit
251d1a7333

+ 0 - 73
CSharp/App/Modules/Tree/AllTreeData.cs

@@ -7,8 +7,6 @@ namespace Tree
     public class AllTreeData
     {
         private readonly List<TreeNodeData> treeNodeDatas = new List<TreeNodeData>();
-        public int MaxNodeId { get; set; }
-        public int MaxTreeId { get; set; }
 
         [DataMember(Order = 1)]
         public List<TreeNodeData> TreeNodeDatas
@@ -18,76 +16,5 @@ namespace Tree
                 return this.treeNodeDatas;
             }
         }
-
-        private readonly Dictionary<int, TreeNodeData> allTreeNodes =
-                new Dictionary<int, TreeNodeData>();
-
-        /// <summary>
-        /// tree对应的root id
-        /// </summary>
-        private readonly Dictionary<int, int> treeRootId = new Dictionary<int, int>();
-
-        public void Init()
-        {
-            this.MaxNodeId = 0;
-            this.MaxTreeId = 0;
-            foreach (TreeNodeData nodeData in this.treeNodeDatas)
-            {
-                this.allTreeNodes[nodeData.Id] = nodeData;
-                if (nodeData.Id > this.MaxNodeId)
-                {
-                    this.MaxNodeId = nodeData.Id;
-                }
-                if (nodeData.TreeId > this.MaxTreeId)
-                {
-                    this.MaxTreeId = nodeData.TreeId;
-                }
-                if (nodeData.Parent == 0)
-                {
-                    this.treeRootId[nodeData.TreeId] = nodeData.Id;
-                }
-            }
-        }
-
-        public void Save()
-        {
-            treeNodeDatas.Clear();
-            foreach (KeyValuePair<int, TreeNodeData> pair in allTreeNodes)
-            {
-                treeNodeDatas.Add(pair.Value);
-            }
-        }
-
-        /// <summary>
-        /// 删除一棵树的所有节点
-        /// </summary>
-        /// <param name="treeId"></param>
-        public void Remove(int treeId)
-        {
-            var removeList = new List<int>();
-            foreach (int key in this.allTreeNodes.Keys)
-            {
-                TreeNodeData nodeData = allTreeNodes[key];
-                if (nodeData.TreeId != treeId)
-                {
-                    continue;
-                }
-                removeList.Add(key);
-            }
-
-            foreach (int key in removeList)
-            {
-                this.allTreeNodes.Remove(key);
-            }
-            Save();
-        }
-
-        public TreeNodeData this[int id]
-        {
-            get
-            {
-                return this.allTreeNodes[id];
-            }
-        }
     }
 }

+ 2 - 1
CSharp/App/Modules/Tree/AllTreeView.xaml

@@ -13,6 +13,7 @@
 		<MenuItem Header="File">
 			<MenuItem Header="打开" Click="MenuItem_Open" />
 			<MenuItem Header="保存" Click="MenuItem_Save" />
+			<MenuItem Header="新建" Click="MenuItem_New" />
 		</MenuItem>
 	</Menu>
 	<Grid>
@@ -26,7 +27,7 @@
 			<ColumnDefinition />
 		</Grid.ColumnDefinitions>
 		<GroupBox Grid.Row="0" Grid.Column="0" Header="行为树列表:">
-			<ListBox Name="lbTreeId" ></ListBox>
+			<ListBox Name="lbTreeId" ItemsSource="{Binding TreeList}" ></ListBox>
 		</GroupBox>
 		<GroupBox Grid.Row="1" Grid.Column="0" Header="节点编辑:">
 			<Tree:NodeDataEditor x:Name="nodeDataEditor" VerticalAlignment="Top" />

+ 9 - 4
CSharp/App/Modules/Tree/AllTreeView.xaml.cs

@@ -13,13 +13,13 @@ namespace Tree
     {
         private AllTreeViewModel allTreeViewModel;
 
-        [ImportingConstructor]
-        public AllTreeView(IEventAggregator eventAggregator)
+        public AllTreeView()
         {
             this.InitializeComponent();
 
-            this.nodeDataEditor.NodeDataEditorViewModel = new NodeDataEditorViewModel(eventAggregator);
-            this.treeView.TreeViewModel = new TreeViewModel(eventAggregator);
+            this.nodeDataEditor.AllTreeView = this;
+            this.treeView.AllTreeView = this;
+            this.treeView.TreeViewModel = new TreeViewModel();
         }
 
         [Import]
@@ -42,5 +42,10 @@ namespace Tree
         private void MenuItem_Save(object sender, RoutedEventArgs e)
         {
         }
+
+        private void MenuItem_New(object sender, RoutedEventArgs e)
+        {
+            this.treeView.TreeViewModel = new TreeViewModel();
+        }
     }
 }

+ 44 - 3
CSharp/App/Modules/Tree/AllTreeViewModel.cs

@@ -1,5 +1,8 @@
-using System.Collections.ObjectModel;
+using System.Collections.Generic;
+using System.Collections.ObjectModel;
 using System.ComponentModel.Composition;
+using System.IO;
+using Helper;
 
 namespace Tree
 {
@@ -9,7 +12,45 @@ namespace Tree
     {
         private AllTreeData allTreeData;
 
-        private readonly ObservableCollection<TreeInfoViewModel> treeInfos =
-                new ObservableCollection<TreeInfoViewModel>();
+        public int MaxNodeId { get; set; }
+        public int MaxTreeId { get; set; }
+
+        private readonly ObservableCollection<int> treeList =
+                new ObservableCollection<int>();
+
+        public Dictionary<int, ObservableCollection<TreeNodeViewModel>> oneTree = 
+            new Dictionary<int, ObservableCollection<TreeNodeViewModel>>();
+
+        public ObservableCollection<int> TreeList
+        {
+            get
+            {
+                return this.treeList;
+            }
+        }
+
+        public void Load(string file)
+        {
+            this.treeList.Clear();
+            byte[] bytes = File.ReadAllBytes(file);
+            this.allTreeData = ProtobufHelper.FromBytes<AllTreeData>(bytes);
+
+            foreach (TreeNodeData treeNodeData in allTreeData.TreeNodeDatas)
+            {
+                ObservableCollection<TreeNodeViewModel> tree;
+                this.oneTree.TryGetValue(treeNodeData.TreeId, out tree);
+                if (tree == null)
+                {
+                    tree = new ObservableCollection<TreeNodeViewModel>();
+                    oneTree[treeNodeData.TreeId] = tree;
+                }
+                //tree.Add(new TreeNodeViewModel(treeNodeData));
+            }
+        }
+
+        public void Save(string file)
+        {
+            
+        }
     }
 }

+ 0 - 8
CSharp/App/Modules/Tree/IEventNotifyView.cs

@@ -1,8 +0,0 @@
-
-namespace Tree
-{
-    public interface IEventNotifyView
-    {
-        void OnDataContextChange();
-    }
-}

+ 3 - 22
CSharp/App/Modules/Tree/NodeDataEditor.xaml.cs

@@ -8,10 +8,8 @@ namespace Tree
     /// <summary>
     /// NodeDataEditor.xaml 的交互逻辑
     /// </summary>
-    public partial class NodeDataEditor : IEventNotifyView
+    public partial class NodeDataEditor
     {
-        private NodeDataEditorViewModel nodeDataEditorViewModel;
-
         public NodeDataEditor()
         {
             this.InitializeComponent();
@@ -19,20 +17,8 @@ namespace Tree
             string[] nodeTypes = Enum.GetNames(typeof (NodeType));
             this.cbType.ItemsSource = nodeTypes;
         }
-       
-        public NodeDataEditorViewModel NodeDataEditorViewModel
-        {
-            get
-            {
-                return this.nodeDataEditorViewModel;
-            }
-            set
-            {
-                this.nodeDataEditorViewModel = value;
-                this.nodeDataEditorViewModel.EventNotifyView = this;
-                this.TreeNodeViewModel = this.nodeDataEditorViewModel.TreeNodeViewModel;
-            }
-        }
+
+        public AllTreeView AllTreeView { get; set; }
 
         public TreeNodeViewModel TreeNodeViewModel
         {
@@ -46,11 +32,6 @@ namespace Tree
             }
         }
 
-        public void OnDataContextChange()
-        {
-            this.DataContext = this.nodeDataEditorViewModel.TreeNodeViewModel;
-        }
-
         private void OnDataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
         {
             if (this.TreeNodeViewModel == null)

+ 0 - 31
CSharp/App/Modules/Tree/NodeDataEditorViewModel.cs

@@ -1,31 +0,0 @@
-using Microsoft.Practices.Prism.Mvvm;
-using Microsoft.Practices.Prism.PubSubEvents;
-
-namespace Tree
-{
-    public class NodeDataEditorViewModel: BindableBase
-    {
-        private IEventAggregator EventAggregator { get; set; }
-
-        public TreeNodeViewModel TreeNodeViewModel { get; set; }
-
-        public IEventNotifyView EventNotifyView { get; set; }
-
-        public NodeDataEditorViewModel()
-        {
-        }
-
-        public NodeDataEditorViewModel(IEventAggregator eventAggregator)
-        {
-            this.EventAggregator = eventAggregator;
-            this.EventAggregator.GetEvent<SelectNodeChangeEvent>().Subscribe(this.OnSelectNodeChange);
-        }
-
-        private void OnSelectNodeChange(TreeNodeViewModel treeNodeViewModel)
-        {
-            this.TreeNodeViewModel = treeNodeViewModel;
-            // 通知view层datacontext已经发生改变
-            this.EventNotifyView.OnDataContextChange();
-        }
-    }
-}

+ 0 - 2
CSharp/App/Modules/Tree/Tree.csproj

@@ -85,13 +85,11 @@
     <Reference Include="WindowsBase" />
   </ItemGroup>
   <ItemGroup>
-    <Compile Include="IEventNotifyView.cs" />
     <Compile Include="SelectNodeChangeEvent.cs" />
     <Compile Include="TreeInfoViewModel.cs" />
     <Compile Include="AllTreeViewModel.cs" />
     <Compile Include="OneTreeData.cs" />
     <Compile Include="TreeLayout.cs" />
-    <Compile Include="NodeDataEditorViewModel.cs" />
     <Compile Include="TreeView.xaml.cs">
       <DependentUpon>TreeView.xaml</DependentUpon>
     </Compile>

+ 32 - 23
CSharp/App/Modules/Tree/TreeLayout.cs

@@ -1,15 +1,21 @@
 namespace Tree
 {
-    public static class TreeLayout
+    public class TreeLayout
     {
+        private readonly TreeViewModel treeViewModel;
         private const double XGap = 20;
         private const double YGap = 10;
-        private static double rootOrigX;
-        private static double rootOrigY;
-        private static double rootOffsetX;
-        private static double rootOffsetY;
+        private double rootOrigX;
+        private double rootOrigY;
+        private double rootOffsetX;
+        private double rootOffsetY;
 
-        private static TreeNodeViewModel LeftMostOffspring(
+        public TreeLayout(TreeViewModel treeViewModel)
+        {
+            this.treeViewModel = treeViewModel;
+        }
+
+        private TreeNodeViewModel LeftMostOffspring(
                 TreeNodeViewModel treeNode, int currentLevel, int searchLevel)
         {
             if (currentLevel == searchLevel)
@@ -18,7 +24,7 @@
             }
             for (int i = 0; i < treeNode.Children.Count; ++i)
             {
-                var child = treeNode.Children[i];
+                var child = this.treeViewModel.Get(treeNode.Children[i]);
                 child.AncestorModify = treeNode.Modify + treeNode.AncestorModify;
                 var offspring = LeftMostOffspring(child, currentLevel + 1, searchLevel);
                 if (offspring == null)
@@ -30,7 +36,7 @@
             return null;
         }
 
-        private static TreeNodeViewModel RightMostOffspring(
+        private TreeNodeViewModel RightMostOffspring(
                 TreeNodeViewModel treeNode, int currentLevel, int searchLevel)
         {
             if (currentLevel == searchLevel)
@@ -39,7 +45,7 @@
             }
             for (int i = treeNode.Children.Count - 1; i >= 0; --i)
             {
-                var child = treeNode.Children[i];
+                var child = this.treeViewModel.Get(treeNode.Children[i]);
                 child.AncestorModify = treeNode.Modify + treeNode.AncestorModify;
                 var offspring = RightMostOffspring(child, currentLevel + 1, searchLevel);
                 if (offspring == null)
@@ -51,7 +57,7 @@
             return null;
         }
 
-        private static void AjustSubTreeGap(TreeNodeViewModel left, TreeNodeViewModel right)
+        private void AjustSubTreeGap(TreeNodeViewModel left, TreeNodeViewModel right)
         {
             double offset = 0;
             TreeNodeViewModel tLeft = left;
@@ -73,24 +79,25 @@
             right.Prelim += offset;
         }
 
-        private static void AjustTreeGap(TreeNodeViewModel treeNode)
+        private void AjustTreeGap(TreeNodeViewModel treeNode)
         {
             for (int i = 0; i < treeNode.Children.Count - 1; ++i)
             {
                 for (int j = i + 1; j < treeNode.Children.Count; ++j)
                 {
-                    var left = treeNode.Children[i];
-                    var right = treeNode.Children[j];
+                    var left = this.treeViewModel.Get(treeNode.Children[i]);
+                    var right = this.treeViewModel.Get(treeNode.Children[j]);
                     AjustSubTreeGap(left, right);
                 }
             }
         }
 
-        private static void CalculatePrelimAndModify(TreeNodeViewModel treeNode)
+        private void CalculatePrelimAndModify(TreeNodeViewModel treeNode)
         {
-            foreach (TreeNodeViewModel node in treeNode.Children)
+            foreach (int childId in treeNode.Children)
             {
-                CalculatePrelimAndModify(node);
+                TreeNodeViewModel child = this.treeViewModel.Get(childId);
+                CalculatePrelimAndModify(child);
             }
 
             double prelim = 0;
@@ -131,12 +138,13 @@
             // 	treeNode.Modify);
         }
 
-        private static void CalculateRelativeXAndY(
+        private void CalculateRelativeXAndY(
                 TreeNodeViewModel treeNode, int level, double totalModify)
         {
-            foreach (TreeNodeViewModel node in treeNode.Children)
+            foreach (int childId in treeNode.Children)
             {
-                CalculateRelativeXAndY(node, level + 1, treeNode.Modify + totalModify);
+                TreeNodeViewModel child = this.treeViewModel.Get(childId);
+                CalculateRelativeXAndY(child, level + 1, treeNode.Modify + totalModify);
             }
             if (treeNode.IsLeaf)
             {
@@ -149,17 +157,18 @@
             treeNode.Y = level * (TreeNodeViewModel.Height + YGap);
         }
 
-        private static void FixXAndY(TreeNodeViewModel treeNode)
+        private void FixXAndY(TreeNodeViewModel treeNode)
         {
             treeNode.X += rootOffsetX;
             treeNode.Y += rootOffsetY;
-            foreach (var node in treeNode.Children)
+            foreach (var childId in treeNode.Children)
             {
-                FixXAndY(node);
+                TreeNodeViewModel child = this.treeViewModel.Get(childId);
+                FixXAndY(child);
             }
         }
 
-        public static void ExcuteLayout(TreeNodeViewModel root)
+        public void ExcuteLayout(TreeNodeViewModel root)
         {
             if (root == null)
             {

+ 42 - 37
CSharp/App/Modules/Tree/TreeNodeViewModel.cs

@@ -1,6 +1,5 @@
 using System;
 using System.Collections.Generic;
-using System.Collections.ObjectModel;
 using Microsoft.Practices.Prism.Mvvm;
 
 namespace Tree
@@ -10,6 +9,7 @@ namespace Tree
         private static int globalNum;
         private static double width = 80;
         private static double height = 50;
+        private TreeViewModel treeViewModel;
         private double x;
         private double y;
         private readonly TreeNodeData treeNodeData;
@@ -18,43 +18,36 @@ namespace Tree
         private double prelim;
         private double modify;
         private double ancestorModify;
-        private TreeNodeViewModel parent;
         private bool isFolder;
 
-        private ObservableCollection<TreeNodeViewModel> children =
-                new ObservableCollection<TreeNodeViewModel>();
-
-        /// <summary>
-        /// 用于初始化根节点
-        /// </summary>
-        /// <param name="x"></param>
-        /// <param name="y"></param>
-        public TreeNodeViewModel(double x, double y)
+        public TreeNodeViewModel(TreeViewModel treeViewModel, double x, double y)
         {
+            this.treeViewModel = treeViewModel;
             this.x = x;
             this.y = y;
             this.treeNodeData = new TreeNodeData();
             this.treeNodeData.Id = globalNum++;
-            this.parent = this.parent ?? this;
+            this.treeNodeData.Parent = 0;
             this.connectorX2 = 0;
             this.connectorY2 = Height / 2;
         }
 
-        public TreeNodeViewModel(TreeNodeViewModel parent)
+        public TreeNodeViewModel(TreeViewModel treeViewModel, TreeNodeViewModel parent)
         {
+            this.treeViewModel = treeViewModel;
             this.treeNodeData = new TreeNodeData();
             this.treeNodeData.Id = globalNum++;
-            this.parent = parent ?? this;
+            this.Parent = parent;
 
             this.connectorX2 = Width + this.Parent.X - this.X;
             this.connectorY2 = Height / 2 + this.Parent.Y - this.Y;
         }
 
-        public TreeNodeViewModel(TreeNodeData data, TreeNodeViewModel parent)
+        public TreeNodeViewModel(TreeViewModel treeViewModel, TreeNodeData data, TreeNodeViewModel parent)
         {
+            this.treeViewModel = treeViewModel;
             this.treeNodeData = data;
-            this.parent = parent ?? this;
-            if (this.parent == this)
+            if (this.IsRoot)
             {
                 this.x = 200;
                 this.y = 10;
@@ -73,9 +66,9 @@ namespace Tree
             get
             {
                 this.treeNodeData.Children.Clear();
-                foreach (TreeNodeViewModel child in this.children)
+                foreach (int child in this.Children)
                 {
-                    this.treeNodeData.Children.Add(child.Id);
+                    this.treeNodeData.Children.Add(child);
                 }
                 this.treeNodeData.Parent = this.IsRoot? 0 : this.Parent.Id;
                 return this.treeNodeData;
@@ -136,7 +129,7 @@ namespace Tree
         {
             get
             {
-                return this.Parent == this;
+                return this.Parent == null;
             }
         }
 
@@ -179,10 +172,14 @@ namespace Tree
                 this.x = value;
                 this.OnPropertyChanged("X");
 
-                this.ConnectorX2 = Width / 2 + this.Parent.X - this.X;
+                if (this.Parent != null)
+                {
+                    this.ConnectorX2 = Width / 2 + this.Parent.X - this.X;
+                }
 
-                foreach (TreeNodeViewModel child in this.Children)
+                foreach (var childId in this.Children)
                 {
+                    TreeNodeViewModel child = this.treeViewModel.Get(childId);
                     child.ConnectorX2 = Width / 2 + this.X - child.X;
                 }
             }
@@ -204,10 +201,14 @@ namespace Tree
                 this.y = value;
                 this.OnPropertyChanged("Y");
 
-                this.ConnectorY2 = Height + this.Parent.Y - this.Y;
+                if (this.Parent != null)
+                {
+                    this.ConnectorY2 = Height + this.Parent.Y - this.Y;
+                }
 
-                foreach (var child in this.Children)
+                foreach (var childId in this.Children)
                 {
+                    TreeNodeViewModel child = this.treeViewModel.Get(childId);
                     child.ConnectorY2 = Height + this.Y - child.Y;
                 }
             }
@@ -291,12 +292,20 @@ namespace Tree
         {
             get
             {
-                return this.parent;
+                if (this.Id == 0)
+                {
+                    return null;
+                }
+                TreeNodeViewModel parent = this.treeViewModel.Get(this.treeNodeData.Parent);
+                return parent;
             }
             set
             {
-                this.parent = value;
-                this.OnPropertyChanged("ParentNum");
+                if (value == null)
+                {
+                    this.treeNodeData.Parent = 0;
+                }
+                this.treeNodeData.Parent = value.Id;
             }
         }
 
@@ -320,15 +329,11 @@ namespace Tree
             }
         }
 
-        public ObservableCollection<TreeNodeViewModel> Children
+        public List<int> Children
         {
             get
             {
-                return this.children;
-            }
-            set
-            {
-                this.children = value;
+                return this.treeNodeData.Children;
             }
         }
 
@@ -341,8 +346,8 @@ namespace Tree
                     return null;
                 }
 
-                int index = this.Parent.Children.IndexOf(this);
-                return index == 0? null : this.Parent.Children[index - 1];
+                int index = this.Parent.Children.IndexOf(this.Id);
+                return index == 0? null : this.treeViewModel.Get(this.Parent.Children[index - 1]);
             }
         }
 
@@ -356,7 +361,7 @@ namespace Tree
                 }
 
                 int maxIndex = this.Children.Count - 1;
-                return this.Children[maxIndex];
+                return this.treeViewModel.Get(this.Children[maxIndex]);
             }
         }
 
@@ -364,7 +369,7 @@ namespace Tree
         {
             get
             {
-                return this.Children.Count == 0? null : this.Children[0];
+                return this.Children.Count == 0? null : this.treeViewModel.Get(this.Children[0]);
             }
         }
 

+ 5 - 4
CSharp/App/Modules/Tree/TreeView.xaml.cs

@@ -1,5 +1,4 @@
 using System;
-using System.ComponentModel.Composition;
 using System.Windows;
 using System.Windows.Input;
 using Logger;
@@ -17,6 +16,8 @@ namespace Tree
         private Point origMouseDownPoint;
         private TreeNodeViewModel moveFromNode;
 
+        public AllTreeView AllTreeView { get; set; }
+
         public TreeView()
         {
             this.InitializeComponent();
@@ -134,7 +135,7 @@ namespace Tree
             this.listBox.SelectedItem = treeNodeViewModel;
             this.moveFromNode = treeNodeViewModel;
 
-            this.TreeViewModel.SelectNodeChange(treeNodeViewModel);
+            this.AllTreeView.nodeDataEditor.DataContext = treeNodeViewModel;
         }
 
         private void ListBoxItem_PreviewMouseLeftButtonUp(object sender, MouseEventArgs e)
@@ -165,7 +166,7 @@ namespace Tree
             // one root node
             if (this.TreeViewModel.TreeNodes.Count == 0)
             {
-                var addTreeNode = new TreeNodeViewModel(point.X, point.Y)
+                var addTreeNode = new TreeNodeViewModel(this.TreeViewModel, point.X, point.Y)
                 {
                     Type = (int)NodeType.Selector
                 };
@@ -176,7 +177,7 @@ namespace Tree
                 if (this.listBox.SelectedItem != null)
                 {
                     var parentNode = this.listBox.SelectedItem as TreeNodeViewModel;
-                    var addTreeNode = new TreeNodeViewModel(parentNode)
+                    var addTreeNode = new TreeNodeViewModel(this.TreeViewModel, parentNode)
                     {
                         Type = (int)NodeType.Selector
                     };

+ 44 - 59
CSharp/App/Modules/Tree/TreeViewModel.cs

@@ -1,26 +1,17 @@
-using System.Collections.ObjectModel;
+using System.Collections.Generic;
+using System.Collections.ObjectModel;
 using System.ComponentModel.Composition;
-using System.IO;
-using Helper;
 using Microsoft.Practices.Prism.Mvvm;
-using Microsoft.Practices.Prism.PubSubEvents;
 
 namespace Tree
 {
     [Export(typeof (TreeViewModel)), PartCreationPolicy(CreationPolicy.NonShared)]
     public class TreeViewModel: BindableBase
     {
-        public IEventAggregator EventAggregator { get; set; }
-
-        private AllTreeData allTreeData;
-
         private readonly ObservableCollection<TreeNodeViewModel> treeNodes =
                 new ObservableCollection<TreeNodeViewModel>();
 
-        public TreeViewModel(IEventAggregator eventAggregator)
-        {
-            this.EventAggregator = eventAggregator;
-        }
+        private readonly Dictionary<int, TreeNodeViewModel> treeNodeDict = new Dictionary<int, TreeNodeViewModel>(); 
 
         public ObservableCollection<TreeNodeViewModel> TreeNodes
         {
@@ -38,9 +29,11 @@ namespace Tree
             }
         }
 
-        public void SelectNodeChange(TreeNodeViewModel treeNodeViewModel)
+        public TreeNodeViewModel Get(int id)
         {
-            this.EventAggregator.GetEvent<SelectNodeChangeEvent>().Publish(treeNodeViewModel);
+            TreeNodeViewModel node;
+            this.treeNodeDict.TryGetValue(id, out node);
+            return node;
         }
 
         public void Add(TreeNodeViewModel treeNode, TreeNodeViewModel parent)
@@ -50,28 +43,39 @@ namespace Tree
             {
                 this.UnFold(parent);
             }
+
             this.treeNodes.Add(treeNode);
+            this.treeNodeDict[treeNode.Id] = treeNode;
+
             if (parent != null)
             {
-                parent.Children.Add(treeNode);
+                parent.Children.Add(treeNode.Id);
             }
-            TreeLayout.ExcuteLayout(this.Root);
+
+            var treeLayout = new TreeLayout(this);
+            treeLayout.ExcuteLayout(this.Root);
         }
 
         private void RecursionRemove(TreeNodeViewModel treeNodeViewModel)
         {
             for (int i = 0; i < treeNodeViewModel.Children.Count; ++i)
             {
-                this.RecursionRemove(treeNodeViewModel.Children[i]);
+                TreeNodeViewModel child = this.Get(treeNodeViewModel.Children[i]);
+                this.RecursionRemove(child);
             }
+            this.treeNodeDict.Remove(treeNodeViewModel.Id);
             this.treeNodes.Remove(treeNodeViewModel);
         }
 
         public void Remove(TreeNodeViewModel treeNodeViewModel)
         {
             this.RecursionRemove(treeNodeViewModel);
-            treeNodeViewModel.Parent.Children.Remove(treeNodeViewModel);
-            TreeLayout.ExcuteLayout(this.Root);
+            if (treeNodeViewModel.Parent != null)
+            {
+                treeNodeViewModel.Parent.Children.Remove(treeNodeViewModel.Id);
+            }
+            var treeLayout = new TreeLayout(this);
+            treeLayout.ExcuteLayout(this.Root);
         }
 
         private void RecursionMove(
@@ -79,9 +83,10 @@ namespace Tree
         {
             treeNodeViewModel.X += offsetX;
             treeNodeViewModel.Y += offsetY;
-            foreach (var node in treeNodeViewModel.Children)
+            foreach (var childId in treeNodeViewModel.Children)
             {
-                this.RecursionMove(node, offsetX, offsetY);
+                TreeNodeViewModel child = this.Get(childId);
+                this.RecursionMove(child, offsetX, offsetY);
             }
         }
 
@@ -116,10 +121,11 @@ namespace Tree
             {
                 this.UnFold(to);
             }
-            from.Parent.Children.Remove(from);
-            to.Children.Add(from);
+            from.Parent.Children.Remove(from.Id);
+            to.Children.Add(from.Id);
             from.Parent = to;
-            TreeLayout.ExcuteLayout(this.Root);
+            var treeLayout = new TreeLayout(this);
+            treeLayout.ExcuteLayout(this.Root);
         }
 
         /// <summary>
@@ -128,12 +134,14 @@ namespace Tree
         /// <param name="treeNodeViewModel"></param>
         public void Fold(TreeNodeViewModel treeNodeViewModel)
         {
-            foreach (var node in treeNodeViewModel.Children)
+            foreach (var childId in treeNodeViewModel.Children)
             {
-                this.RecursionRemove(node);
+                TreeNodeViewModel child = this.Get(childId);
+                this.RecursionRemove(child);
             }
             treeNodeViewModel.IsFolder = true;
-            TreeLayout.ExcuteLayout(this.Root);
+            var treeLayout = new TreeLayout(this);
+            treeLayout.ExcuteLayout(this.Root);
         }
 
         /// <summary>
@@ -142,12 +150,14 @@ namespace Tree
         /// <param name="unFoldNode"></param>
         public void UnFold(TreeNodeViewModel unFoldNode)
         {
-            foreach (var tn in unFoldNode.Children)
+            foreach (var childId in unFoldNode.Children)
             {
-                this.RecursionAdd(tn);
+                TreeNodeViewModel child = this.Get(childId);
+                this.RecursionAdd(child);
             }
             unFoldNode.IsFolder = false;
-            TreeLayout.ExcuteLayout(this.Root);
+            var treeLayout = new TreeLayout(this);
+            treeLayout.ExcuteLayout(this.Root);
         }
 
         private void RecursionAdd(TreeNodeViewModel treeNodeViewModel)
@@ -156,15 +166,16 @@ namespace Tree
             {
                 this.treeNodes.Add(treeNodeViewModel);
             }
-            ObservableCollection<TreeNodeViewModel> children = treeNodeViewModel.Children;
+            List<int> children = treeNodeViewModel.Children;
 
             if (treeNodeViewModel.IsFolder)
             {
                 return;
             }
-            foreach (var tn in children)
+            foreach (var childId in children)
             {
-                this.RecursionAdd(tn);
+                TreeNodeViewModel child = this.Get(childId);
+                this.RecursionAdd(child);
             }
         }
 
@@ -193,31 +204,5 @@ namespace Tree
             //    this.RecursionSave(allTreeData, childNode);
             //}
         }
-
-        /// <summary>
-        /// 从配置中加载
-        /// </summary>
-        /// <param name="filePath"></param>
-        public void Load(string filePath)
-        {
-            this.TreeNodes.Clear();
-            byte[] bytes = File.ReadAllBytes(filePath);
-            this.allTreeData = ProtobufHelper.FromBytes<AllTreeData>(bytes);
-            allTreeData.Init();
-        }
-
-        private void RecursionLoad(
-                AllTreeData allTreeData, TreeNodeData treeNodeData,
-                TreeNodeViewModel parentNode)
-        {
-            var node = new TreeNodeViewModel(treeNodeData, parentNode);
-            this.Add(node, parentNode);
-            foreach (int id in treeNodeData.Children)
-            {
-                TreeNodeData childNodeData = allTreeData[id];
-                this.RecursionLoad(allTreeData, childNodeData, node);
-            }
-            TreeLayout.ExcuteLayout(this.Root);
-        }
     }
 }

+ 0 - 15
CSharp/CSharp.sln

@@ -26,8 +26,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Infrastructure", "App\Infra
 EndProject
 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tools", "Tools", "{FD5F443E-CBEE-443E-821D-C47C86E09534}"
 EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProtobufTool", "Tools\ProtobufTool\ProtobufTool.csproj", "{87537C92-B2C7-4E46-A6FB-02B73215C100}"
-EndProject
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Helper", "Platform\Helper\Helper.csproj", "{24233CD5-A5DF-484B-A482-B79CB7A0D9CB}"
 EndProject
 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ENetCpp", "Platform\ENetCpp\ENetCpp.vcxproj", "{C9992B7C-313E-4C9F-A954-640D01EDFB58}"
@@ -134,18 +132,6 @@ Global
 		{48A2E149-0DAC-41B4-BB54-DFBCCD6D42B3}.Release|Mixed Platforms.Build.0 = Release|Any CPU
 		{48A2E149-0DAC-41B4-BB54-DFBCCD6D42B3}.Release|Win32.ActiveCfg = Release|Any CPU
 		{48A2E149-0DAC-41B4-BB54-DFBCCD6D42B3}.Release|x86.ActiveCfg = Release|Any CPU
-		{87537C92-B2C7-4E46-A6FB-02B73215C100}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
-		{87537C92-B2C7-4E46-A6FB-02B73215C100}.Debug|Any CPU.Build.0 = Debug|Any CPU
-		{87537C92-B2C7-4E46-A6FB-02B73215C100}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
-		{87537C92-B2C7-4E46-A6FB-02B73215C100}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
-		{87537C92-B2C7-4E46-A6FB-02B73215C100}.Debug|Win32.ActiveCfg = Debug|Any CPU
-		{87537C92-B2C7-4E46-A6FB-02B73215C100}.Debug|x86.ActiveCfg = Debug|Any CPU
-		{87537C92-B2C7-4E46-A6FB-02B73215C100}.Release|Any CPU.ActiveCfg = Release|Any CPU
-		{87537C92-B2C7-4E46-A6FB-02B73215C100}.Release|Any CPU.Build.0 = Release|Any CPU
-		{87537C92-B2C7-4E46-A6FB-02B73215C100}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
-		{87537C92-B2C7-4E46-A6FB-02B73215C100}.Release|Mixed Platforms.Build.0 = Release|Any CPU
-		{87537C92-B2C7-4E46-A6FB-02B73215C100}.Release|Win32.ActiveCfg = Release|Any CPU
-		{87537C92-B2C7-4E46-A6FB-02B73215C100}.Release|x86.ActiveCfg = Release|Any CPU
 		{24233CD5-A5DF-484B-A482-B79CB7A0D9CB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
 		{24233CD5-A5DF-484B-A482-B79CB7A0D9CB}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{24233CD5-A5DF-484B-A482-B79CB7A0D9CB}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
@@ -398,7 +384,6 @@ Global
 		{5D6ECBCD-BE14-4DCB-BAEC-57089748B164} = {C4C64188-4FAE-4CC3-A9E6-D9D4AF7429B6}
 		{3A98B35C-DEA8-489C-9203-263FFB6B065D} = {ADBF5F67-B480-4A93-9D50-C81856FC61A9}
 		{48A2E149-0DAC-41B4-BB54-DFBCCD6D42B3} = {6E9D97F0-4243-452E-B832-1A855B8118EB}
-		{87537C92-B2C7-4E46-A6FB-02B73215C100} = {FD5F443E-CBEE-443E-821D-C47C86E09534}
 		{24233CD5-A5DF-484B-A482-B79CB7A0D9CB} = {ADBF5F67-B480-4A93-9D50-C81856FC61A9}
 		{C9992B7C-313E-4C9F-A954-640D01EDFB58} = {ADBF5F67-B480-4A93-9D50-C81856FC61A9}
 		{D0B4CFAC-A368-4742-9863-68776CFA9938} = {ADBF5F67-B480-4A93-9D50-C81856FC61A9}

+ 2 - 0
CSharp/CSharp.sln.DotSettings

@@ -14,8 +14,10 @@
 	<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=PossibleNullReferenceException/@EntryIndexedValue">DO_NOT_SHOW</s:String>
 	<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=RedundantAssignment/@EntryIndexedValue">DO_NOT_SHOW</s:String>
 	<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=SuggestUseVarKeywordEverywhere/@EntryIndexedValue">DO_NOT_SHOW</s:String>
+	<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=SuggestUseVarKeywordEvident/@EntryIndexedValue">DO_NOT_SHOW</s:String>
 	<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=UnusedAutoPropertyAccessor_002EGlobal/@EntryIndexedValue">DO_NOT_SHOW</s:String>
 	<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=UnusedAutoPropertyAccessor_002ELocal/@EntryIndexedValue">DO_NOT_SHOW</s:String>
+	<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=UseObjectOrCollectionInitializer/@EntryIndexedValue">DO_NOT_SHOW</s:String>
 	<s:String x:Key="/Default/CodeStyle/CodeCleanup/Profiles/=tanghai/@EntryIndexedValue">&lt;?xml version="1.0" encoding="utf-16"?&gt;&lt;Profile name="tanghai"&gt;&lt;CSArrangeThisQualifier&gt;True&lt;/CSArrangeThisQualifier&gt;&lt;CSRemoveCodeRedundancies&gt;True&lt;/CSRemoveCodeRedundancies&gt;&lt;CSMakeFieldReadonly&gt;True&lt;/CSMakeFieldReadonly&gt;&lt;CSUseVar&gt;&lt;BehavourStyle&gt;DISABLED&lt;/BehavourStyle&gt;&lt;LocalVariableStyle&gt;IMPLICIT_WHEN_INITIALIZER_HAS_TYPE&lt;/LocalVariableStyle&gt;&lt;ForeachVariableStyle&gt;IMPLICIT_EXCEPT_SIMPLE_TYPES&lt;/ForeachVariableStyle&gt;&lt;/CSUseVar&gt;&lt;CSOptimizeUsings&gt;&lt;OptimizeUsings&gt;True&lt;/OptimizeUsings&gt;&lt;EmbraceInRegion&gt;False&lt;/EmbraceInRegion&gt;&lt;RegionName&gt;&lt;/RegionName&gt;&lt;/CSOptimizeUsings&gt;&lt;CSShortenReferences&gt;True&lt;/CSShortenReferences&gt;&lt;CSReformatCode&gt;True&lt;/CSReformatCode&gt;&lt;/Profile&gt;</s:String>
 	<s:String x:Key="/Default/CodeStyle/CodeCleanup/RecentlyUsedProfile/@EntryValue">tanghai</s:String>
 	<s:String x:Key="/Default/CodeStyle/CodeCleanup/SilentCleanupProfile/@EntryValue">tanghai</s:String>

+ 18 - 3
CSharp/Platform/Helper/ProtobufHelper.cs

@@ -1,4 +1,5 @@
-using System.IO;
+using System.ComponentModel;
+using System.IO;
 using ProtoBuf;
 
 namespace Helper
@@ -15,13 +16,27 @@ namespace Helper
         public static T FromBytes<T>(byte[] bytes)
         {
             var ms = new MemoryStream(bytes, 0, bytes.Length);
-            return Serializer.Deserialize<T>(ms);
+            T t = Serializer.Deserialize<T>(ms);
+            var iSupportInitialize = t as ISupportInitialize;
+            if (iSupportInitialize == null)
+            {
+                return t;
+            }
+            iSupportInitialize.EndInit();
+            return t;
         }
 
         public static T FromBytes<T>(byte[] bytes, int index, int length)
         {
             var ms = new MemoryStream(bytes, index, length);
-            return Serializer.Deserialize<T>(ms);
+            T t = Serializer.Deserialize<T>(ms);
+            var iSupportInitialize = t as ISupportInitialize;
+            if (iSupportInitialize == null)
+            {
+                return t;
+            }
+            iSupportInitialize.EndInit();
+            return t;
         }
     }
 }