Skip to content

Commit

Permalink
optimized demo start-up speed
Browse files Browse the repository at this point in the history
  • Loading branch information
NaBian committed May 22, 2020
1 parent 39b1296 commit b57e6e8
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public static object CreateInternalInstance(string className)
{
try
{

var type = Type.GetType($"{NameSpaceStr}.{className}");
return type == null ? null : Activator.CreateInstance(type);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
</Border.Resources>
<DockPanel>
<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}"/>
<TabControl SelectionChanged="TabControl_OnSelectionChanged" ItemContainerStyle="{StaticResource TabItemTransparent}" ItemsSource="{Binding DemoInfoList}" Style="{StaticResource TabControlInLine}" Background="Transparent" SelectedIndex="0">
<TabControl SelectionChanged="TabControl_OnSelectionChanged" ItemContainerStyle="{StaticResource TabItemTransparent}" ItemsSource="{Binding DemoInfoCollection}" Style="{StaticResource TabControlInLine}" Background="Transparent" SelectedIndex="0">
<TabControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{ex:Lang Key={Binding Title}}"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<Border xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:hc="https://handyorg.github.io/handycontrol"
xmlns:avalonedit="http://icsharpcode.net/sharpdevelop/avalonedit"
x:Class="HandyControlDemo.UserControl.MainContent">
<Grid Name="GridMain" MinHeight="300" MinWidth="200" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="16">
<Grid.RowDefinitions>
Expand All @@ -14,19 +13,7 @@
<TextBlock Style="{StaticResource TextBlockDefault}" Foreground="White" Text="{Binding ContentTitle}"/>
<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}"/>
<hc:Drawer Name="DrawerCode" Opened="DrawerCode_OnOpened">
<Border Style="{StaticResource BorderClip}" Margin="32,32,0,32" CornerRadius="10,0,0,10" Background="{DynamicResource RegionBrush}" Width="800">
<TabControl Grid.Row="1" Style="{StaticResource TabControlInLine}">
<TabItem Header="XAML">
<avalonedit:TextEditor Style="{StaticResource TextEditorCustom}" SyntaxHighlighting="XML" Name="EditorXaml"/>
</TabItem>
<TabItem Header="C#">
<avalonedit:TextEditor Style="{StaticResource TextEditorCustom}" SyntaxHighlighting="C#" Name="EditorCs"/>
</TabItem>
<TabItem Header="VM">
<avalonedit:TextEditor Style="{StaticResource TextEditorCustom}" SyntaxHighlighting="C#" Name="EditorVm"/>
</TabItem>
</TabControl>
</Border>
<Border Style="{StaticResource BorderClip}" Margin="32,32,0,32" CornerRadius="10,0,0,10" Background="{DynamicResource RegionBrush}" Width="800" Name="BorderCode"/>
</hc:Drawer>
</hc:SimplePanel>
</Border>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
using System.Windows;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using GalaSoft.MvvmLight.Messaging;
using HandyControl.Tools;
using HandyControl.Tools.Extension;
using HandyControlDemo.Data;
using HandyControlDemo.ViewModel;
using ICSharpCode.AvalonEdit;
using ICSharpCode.AvalonEdit.Highlighting;


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

private string _currentDemoKey;

private bool _drawerCodeUsed;

private Dictionary<string, TextEditor> _textEditor;

public MainContent()
{
InitializeComponent();
Expand Down Expand Up @@ -49,6 +58,53 @@ private void FullSwitch(bool isFull)

private void DrawerCode_OnOpened(object sender, RoutedEventArgs e)
{
if (!_drawerCodeUsed)
{
var textEditorCustomStyle = ResourceHelper.GetResource<Style>("TextEditorCustom");
_textEditor = new Dictionary<string, TextEditor>
{
["XAML"] = new TextEditor
{
Style = textEditorCustomStyle,
SyntaxHighlighting = HighlightingManager.Instance.GetDefinition("XML")
},
["C#"] = new TextEditor
{
Style = textEditorCustomStyle,
SyntaxHighlighting = HighlightingManager.Instance.GetDefinition("C#")
},
["VM"] = new TextEditor
{
Style = textEditorCustomStyle,
SyntaxHighlighting = HighlightingManager.Instance.GetDefinition("C#")
}
};
BorderCode.Child = new TabControl
{
Style = ResourceHelper.GetResource<Style>("TabControlInLine"),
Items =
{
new TabItem
{
Header = "XAML",
Content = _textEditor["XAML"]
},
new TabItem
{
Header = "C#",
Content = _textEditor["C#"]
},
new TabItem
{
Header = "VM",
Content = _textEditor["VM"]
}
}
};

_drawerCodeUsed = true;
}

var typeKey = ViewModelLocator.Instance.Main.DemoInfoCurrent.Key;
var demoKey = ViewModelLocator.Instance.Main.DemoItemCurrent.TargetCtlName;
if (Equals(_currentDemoKey, demoKey)) return;
Expand All @@ -64,9 +120,9 @@ private void DrawerCode_OnOpened(object sender, RoutedEventArgs e)
? $"ViewModel/{dcTypeName}"
: xamlPath;

EditorXaml.Text = DemoHelper.GetCode(xamlPath);
EditorCs.Text = DemoHelper.GetCode($"{xamlPath}.cs");
EditorVm.Text = DemoHelper.GetCode($"{vmPath}.cs");
_textEditor["XAML"].Text = DemoHelper.GetCode(xamlPath);
_textEditor["C#"].Text = DemoHelper.GetCode($"{xamlPath}.cs");
_textEditor["VM"].Text = DemoHelper.GetCode($"{vmPath}.cs");
}
}
}
Expand Down
50 changes: 32 additions & 18 deletions src/Shared/HandyControlDemo_Shared/ViewModel/Main/MainViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using HandyControl.Controls;
Expand All @@ -26,11 +29,6 @@ public class MainViewModel : DemoViewModelBase<DemoDataModel>
/// </summary>
private object _subContent;

/// <summary>
/// demo信息
/// </summary>
private List<DemoInfoModel> _demoInfoList;

#endregion

public MainViewModel(DataService dataService)
Expand All @@ -42,12 +40,12 @@ public MainViewModel(DataService dataService)
disposable.Dispose();
}
SubContent = obj;
});
}, true);

Messenger.Default.Register<object>(this, MessageToken.ClearLeftSelected, obj =>
{
DemoItemCurrent = null;
foreach (var item in DemoInfoList)
foreach (var item in DemoInfoCollection)
{
item.SelectedIndex = -1;
}
Expand All @@ -60,7 +58,31 @@ public MainViewModel(DataService dataService)
});

DataList = dataService.GetDemoDataList();
DemoInfoList = dataService.GetDemoInfo();
DemoInfoCollection = new ObservableCollection<DemoInfoModel>();

#if netle40
Task.Factory.StartNew(() =>
{
foreach (var item in dataService.GetDemoInfo())
{
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
{
DemoInfoCollection.Add(item);
}), DispatcherPriority.ApplicationIdle);
}
});
#else
Task.Run(() =>
{
foreach (var item in dataService.GetDemoInfo())
{
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
{
DemoInfoCollection.Add(item);
}), DispatcherPriority.ApplicationIdle);
}
});
#endif
}

#region 属性
Expand Down Expand Up @@ -101,15 +123,7 @@ public object ContentTitle
/// <summary>
/// demo信息
/// </summary>
public List<DemoInfoModel> DemoInfoList
{
get => _demoInfoList;
#if netle40
set => Set(nameof(DemoInfoList), ref _demoInfoList, value);
#else
set => Set(ref _demoInfoList, value);
#endif
}
public ObservableCollection<DemoInfoModel> DemoInfoCollection { get; set; }

#endregion

Expand Down

0 comments on commit b57e6e8

Please sign in to comment.