Skip to content

Commit b57e6e8

Browse files
committed
optimized demo start-up speed
1 parent 39b1296 commit b57e6e8

File tree

5 files changed

+94
-38
lines changed

5 files changed

+94
-38
lines changed

src/Shared/HandyControlDemo_Shared/Tools/Helper/AssemblyHelper.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ public static object CreateInternalInstance(string className)
2525
{
2626
try
2727
{
28-
2928
var type = Type.GetType($"{NameSpaceStr}.{className}");
3029
return type == null ? null : Activator.CreateInstance(type);
3130
}

src/Shared/HandyControlDemo_Shared/UserControl/Main/LeftMainContent.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
</Border.Resources>
2828
<DockPanel>
2929
<Button Command="{Binding OpenPracticalDemoCmd}" DockPanel.Dock="Top" HorizontalAlignment="Stretch" Margin="10,10,10,4" Content="{ex:Lang Key={x:Static langs:LangKeys.PracticalDemos}}" BorderThickness="0" Background="{DynamicResource SecondaryRegionBrush}"/>
30-
<TabControl SelectionChanged="TabControl_OnSelectionChanged" ItemContainerStyle="{StaticResource TabItemTransparent}" ItemsSource="{Binding DemoInfoList}" Style="{StaticResource TabControlInLine}" Background="Transparent" SelectedIndex="0">
30+
<TabControl SelectionChanged="TabControl_OnSelectionChanged" ItemContainerStyle="{StaticResource TabItemTransparent}" ItemsSource="{Binding DemoInfoCollection}" Style="{StaticResource TabControlInLine}" Background="Transparent" SelectedIndex="0">
3131
<TabControl.ItemTemplate>
3232
<DataTemplate>
3333
<TextBlock Text="{ex:Lang Key={Binding Title}}"/>

src/Shared/HandyControlDemo_Shared/UserControl/Main/MainContent.xaml

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<Border xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
22
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
33
xmlns:hc="https://handyorg.github.io/handycontrol"
4-
xmlns:avalonedit="http://icsharpcode.net/sharpdevelop/avalonedit"
54
x:Class="HandyControlDemo.UserControl.MainContent">
65
<Grid Name="GridMain" MinHeight="300" MinWidth="200" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="16">
76
<Grid.RowDefinitions>
@@ -14,19 +13,7 @@
1413
<TextBlock Style="{StaticResource TextBlockDefault}" Foreground="White" Text="{Binding ContentTitle}"/>
1514
<ToggleButton Margin="0,0,6,0" IsChecked="{Binding IsOpen,ElementName=DrawerCode}" Padding="5" Foreground="{DynamicResource TextIconBrush}" Style="{StaticResource ToggleButtonIconTransparent}" HorizontalAlignment="Right" hc:IconElement.Geometry="{StaticResource CodeGeometry}"/>
1615
<hc:Drawer Name="DrawerCode" Opened="DrawerCode_OnOpened">
17-
<Border Style="{StaticResource BorderClip}" Margin="32,32,0,32" CornerRadius="10,0,0,10" Background="{DynamicResource RegionBrush}" Width="800">
18-
<TabControl Grid.Row="1" Style="{StaticResource TabControlInLine}">
19-
<TabItem Header="XAML">
20-
<avalonedit:TextEditor Style="{StaticResource TextEditorCustom}" SyntaxHighlighting="XML" Name="EditorXaml"/>
21-
</TabItem>
22-
<TabItem Header="C#">
23-
<avalonedit:TextEditor Style="{StaticResource TextEditorCustom}" SyntaxHighlighting="C#" Name="EditorCs"/>
24-
</TabItem>
25-
<TabItem Header="VM">
26-
<avalonedit:TextEditor Style="{StaticResource TextEditorCustom}" SyntaxHighlighting="C#" Name="EditorVm"/>
27-
</TabItem>
28-
</TabControl>
29-
</Border>
16+
<Border Style="{StaticResource BorderClip}" Margin="32,32,0,32" CornerRadius="10,0,0,10" Background="{DynamicResource RegionBrush}" Width="800" Name="BorderCode"/>
3017
</hc:Drawer>
3118
</hc:SimplePanel>
3219
</Border>

src/Shared/HandyControlDemo_Shared/UserControl/Main/MainContent.xaml.cs

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
using System.Windows;
1+
using System.Collections.Generic;
2+
using System.Windows;
3+
using System.Windows.Controls;
24
using GalaSoft.MvvmLight.Messaging;
5+
using HandyControl.Tools;
36
using HandyControl.Tools.Extension;
47
using HandyControlDemo.Data;
58
using HandyControlDemo.ViewModel;
9+
using ICSharpCode.AvalonEdit;
10+
using ICSharpCode.AvalonEdit.Highlighting;
611

712

813
namespace HandyControlDemo.UserControl
@@ -16,6 +21,10 @@ public partial class MainContent
1621

1722
private string _currentDemoKey;
1823

24+
private bool _drawerCodeUsed;
25+
26+
private Dictionary<string, TextEditor> _textEditor;
27+
1928
public MainContent()
2029
{
2130
InitializeComponent();
@@ -49,6 +58,53 @@ private void FullSwitch(bool isFull)
4958

5059
private void DrawerCode_OnOpened(object sender, RoutedEventArgs e)
5160
{
61+
if (!_drawerCodeUsed)
62+
{
63+
var textEditorCustomStyle = ResourceHelper.GetResource<Style>("TextEditorCustom");
64+
_textEditor = new Dictionary<string, TextEditor>
65+
{
66+
["XAML"] = new TextEditor
67+
{
68+
Style = textEditorCustomStyle,
69+
SyntaxHighlighting = HighlightingManager.Instance.GetDefinition("XML")
70+
},
71+
["C#"] = new TextEditor
72+
{
73+
Style = textEditorCustomStyle,
74+
SyntaxHighlighting = HighlightingManager.Instance.GetDefinition("C#")
75+
},
76+
["VM"] = new TextEditor
77+
{
78+
Style = textEditorCustomStyle,
79+
SyntaxHighlighting = HighlightingManager.Instance.GetDefinition("C#")
80+
}
81+
};
82+
BorderCode.Child = new TabControl
83+
{
84+
Style = ResourceHelper.GetResource<Style>("TabControlInLine"),
85+
Items =
86+
{
87+
new TabItem
88+
{
89+
Header = "XAML",
90+
Content = _textEditor["XAML"]
91+
},
92+
new TabItem
93+
{
94+
Header = "C#",
95+
Content = _textEditor["C#"]
96+
},
97+
new TabItem
98+
{
99+
Header = "VM",
100+
Content = _textEditor["VM"]
101+
}
102+
}
103+
};
104+
105+
_drawerCodeUsed = true;
106+
}
107+
52108
var typeKey = ViewModelLocator.Instance.Main.DemoInfoCurrent.Key;
53109
var demoKey = ViewModelLocator.Instance.Main.DemoItemCurrent.TargetCtlName;
54110
if (Equals(_currentDemoKey, demoKey)) return;
@@ -64,9 +120,9 @@ private void DrawerCode_OnOpened(object sender, RoutedEventArgs e)
64120
? $"ViewModel/{dcTypeName}"
65121
: xamlPath;
66122

67-
EditorXaml.Text = DemoHelper.GetCode(xamlPath);
68-
EditorCs.Text = DemoHelper.GetCode($"{xamlPath}.cs");
69-
EditorVm.Text = DemoHelper.GetCode($"{vmPath}.cs");
123+
_textEditor["XAML"].Text = DemoHelper.GetCode(xamlPath);
124+
_textEditor["C#"].Text = DemoHelper.GetCode($"{xamlPath}.cs");
125+
_textEditor["VM"].Text = DemoHelper.GetCode($"{vmPath}.cs");
70126
}
71127
}
72128
}

src/Shared/HandyControlDemo_Shared/ViewModel/Main/MainViewModel.cs

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
using System;
2-
using System.Collections.Generic;
2+
using System.Collections.ObjectModel;
3+
using System.Threading.Tasks;
4+
using System.Windows;
35
using System.Windows.Controls;
6+
using System.Windows.Threading;
47
using GalaSoft.MvvmLight.Command;
58
using GalaSoft.MvvmLight.Messaging;
69
using HandyControl.Controls;
@@ -26,11 +29,6 @@ public class MainViewModel : DemoViewModelBase<DemoDataModel>
2629
/// </summary>
2730
private object _subContent;
2831

29-
/// <summary>
30-
/// demo信息
31-
/// </summary>
32-
private List<DemoInfoModel> _demoInfoList;
33-
3432
#endregion
3533

3634
public MainViewModel(DataService dataService)
@@ -42,12 +40,12 @@ public MainViewModel(DataService dataService)
4240
disposable.Dispose();
4341
}
4442
SubContent = obj;
45-
});
43+
}, true);
4644

4745
Messenger.Default.Register<object>(this, MessageToken.ClearLeftSelected, obj =>
4846
{
4947
DemoItemCurrent = null;
50-
foreach (var item in DemoInfoList)
48+
foreach (var item in DemoInfoCollection)
5149
{
5250
item.SelectedIndex = -1;
5351
}
@@ -60,7 +58,31 @@ public MainViewModel(DataService dataService)
6058
});
6159

6260
DataList = dataService.GetDemoDataList();
63-
DemoInfoList = dataService.GetDemoInfo();
61+
DemoInfoCollection = new ObservableCollection<DemoInfoModel>();
62+
63+
#if netle40
64+
Task.Factory.StartNew(() =>
65+
{
66+
foreach (var item in dataService.GetDemoInfo())
67+
{
68+
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
69+
{
70+
DemoInfoCollection.Add(item);
71+
}), DispatcherPriority.ApplicationIdle);
72+
}
73+
});
74+
#else
75+
Task.Run(() =>
76+
{
77+
foreach (var item in dataService.GetDemoInfo())
78+
{
79+
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
80+
{
81+
DemoInfoCollection.Add(item);
82+
}), DispatcherPriority.ApplicationIdle);
83+
}
84+
});
85+
#endif
6486
}
6587

6688
#region 属性
@@ -101,15 +123,7 @@ public object ContentTitle
101123
/// <summary>
102124
/// demo信息
103125
/// </summary>
104-
public List<DemoInfoModel> DemoInfoList
105-
{
106-
get => _demoInfoList;
107-
#if netle40
108-
set => Set(nameof(DemoInfoList), ref _demoInfoList, value);
109-
#else
110-
set => Set(ref _demoInfoList, value);
111-
#endif
112-
}
126+
public ObservableCollection<DemoInfoModel> DemoInfoCollection { get; set; }
113127

114128
#endregion
115129

0 commit comments

Comments
 (0)