Skip to content

Commit 3ea9f24

Browse files
committed
v1.1 released
1 parent d95d306 commit 3ea9f24

14 files changed

Lines changed: 213 additions & 114 deletions

RoslynSyntaxTool/App.xaml.cs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
using Newtonsoft.Json;
1+
using CommunityToolkit.Mvvm.DependencyInjection;
2+
using CommunityToolkit.Mvvm.Messaging;
3+
using Microsoft.Extensions.DependencyInjection;
4+
using Newtonsoft.Json;
25
using System;
36
using System.Collections.Generic;
47
using System.Configuration;
58
using System.Data;
69
using System.Windows;
710
using System.Windows.Threading;
8-
using GalaSoft.MvvmLight.Messaging;
9-
using GalaSoft.MvvmLight.Threading;
1011
using Workshop.Common;
1112
using Workshop.Helper;
12-
13+
using Workshop.ViewModel;
1314

1415
namespace Workshop
1516
{
@@ -18,9 +19,24 @@ namespace Workshop
1819
/// </summary>
1920
public partial class App : Application
2021
{
22+
private bool _initialized;
23+
2124
public static string Session;
2225
public App()
2326
{
27+
// Register services
28+
if (!_initialized)
29+
{
30+
_initialized = true;
31+
Ioc.Default.ConfigureServices(
32+
new ServiceCollection()
33+
//ViewModels
34+
.AddSingleton<MainViewModel>()
35+
.AddSingleton<IndexPageViewModel>()
36+
.AddSingleton<SettingPageViewModel>()
37+
.BuildServiceProvider());
38+
}
39+
2440
App.Current.Startup += Current_Startup;
2541
App.Current.Exit += Current_Exit;
2642

@@ -41,7 +57,7 @@ private void App_OnDispatcherUnhandledException(object sender, DispatcherUnhandl
4157
{
4258
try
4359
{
44-
Messenger.Default.Send("", MessengerToken.CLOSEPROGRESS);
60+
WeakReferenceMessenger.Default.Send(MessengerToken.CLOSEPROGRESS);
4561

4662
LogHelper.LogError("UI线程全局异常" + e.Exception);
4763
MessageBox.Show("An unhandled exception just occurred: " + e.Exception.Message, "UI线程全局异常", MessageBoxButton.OK, MessageBoxImage.Error);
@@ -63,7 +79,7 @@ private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionE
6379
{
6480
try
6581
{
66-
Messenger.Default.Send("", MessengerToken.CLOSEPROGRESS);
82+
WeakReferenceMessenger.Default.Send(MessengerToken.CLOSEPROGRESS);
6783

6884
var exception = e.ExceptionObject as Exception;
6985
if (exception != null)
@@ -85,7 +101,6 @@ private void Current_Startup(object sender, StartupEventArgs e)
85101
Current.DispatcherUnhandledException += App_OnDispatcherUnhandledException;
86102
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
87103
LogHelper.LogFlag = true;
88-
DispatcherHelper.Initialize();
89104

90105
}
91106
}

RoslynSyntaxTool/Control/ProgressWindow.xaml.cs

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
using GalaSoft.MvvmLight.Messaging;
1+
using CommunityToolkit.Mvvm.Messaging;
22
using MahApps.Metro.Controls;
33
using System;
44
using System.Collections.Generic;
5+
using System.Diagnostics;
56
using System.Linq;
67
using System.Text;
78
using System.Windows;
@@ -21,6 +22,26 @@ namespace Workshop.Control
2122
/// </summary>
2223
public partial class ProgressWindow : MetroWindow
2324
{
25+
public static ProgressWindow Instance;
26+
static ProgressWindow()
27+
{
28+
}
29+
30+
public static void StaticShowDialog(string title)
31+
{
32+
Instance = new ProgressWindow();
33+
Instance.ShowDialog(title);
34+
}
35+
36+
public static void StaticUnShowDialog()
37+
{
38+
if (Instance!=null)
39+
{
40+
Instance.Close();
41+
Instance = null;
42+
}
43+
}
44+
2445
public double CurrentVal { get; private set; }
2546
public double TotalVal { get; private set; }
2647

@@ -29,9 +50,10 @@ public partial class ProgressWindow : MetroWindow
2950
public ProgressWindow()
3051
{
3152
InitializeComponent();
32-
Messenger.Default.Register<string>(this, MessengerToken.UPDATEPROGRESS, HandleMessage);
33-
Messenger.Default.Register<string>(this, MessengerToken.CLOSEPROGRESS, HandleClose);
34-
this.Unloaded += (sender, e) => Messenger.Default.Unregister(this);
53+
WeakReferenceMessenger.Default.Register<string>( MessengerToken.UPDATEPROGRESS, HandleMessage);
54+
WeakReferenceMessenger.Default.Register<string>( MessengerToken.CLOSEPROGRESS, HandleClose);
55+
this.Unloaded += (sender, e) => WeakReferenceMessenger.Default.UnregisterAll(MessengerToken.UPDATEPROGRESS);
56+
this.Unloaded += (sender, e) => WeakReferenceMessenger.Default.UnregisterAll(MessengerToken.CLOSEPROGRESS);
3557

3658
}
3759

@@ -48,12 +70,13 @@ public void ShowDialog(string title)
4870
base.ShowDialog();
4971
}
5072

51-
private void HandleClose(string obj)
73+
private void HandleClose(object recipient, string obj)
5274
{
75+
Debug.WriteLine("ProgressWindow close by" +obj);
5376
this.Close();
5477
}
5578

56-
private void HandleMessage(string obj)
79+
private void HandleMessage(object recipient, string obj)
5780
{
5881
this.MainProgress.IsIndeterminate = false;
5982
this.CancelButton.Visibility = Visibility.Visible;
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using System.Windows;
7+
using Workshop.Control;
8+
9+
namespace Workshop.Helper
10+
{
11+
public class InvokeHelper
12+
{
13+
/// <summary>
14+
/// UI线程调用方法
15+
/// </summary>
16+
/// <param name="title"></param>
17+
/// <param name="action"></param>
18+
/// <param name="thenAction"></param>
19+
/// <returns></returns>
20+
public static Task InvokeOnUi(string title, Action action, Action thenAction=null)
21+
{
22+
var task= Task.Factory.StartNew(() =>
23+
{
24+
25+
Application.Current.Dispatcher.InvokeAsync(() =>
26+
{
27+
if (string.IsNullOrEmpty(title))
28+
{
29+
title = "请稍候..";
30+
}
31+
ProgressWindow.StaticShowDialog(title);
32+
});
33+
action?.Invoke();
34+
35+
Application.Current.Dispatcher.InvokeAsync(() =>
36+
{
37+
38+
ProgressWindow.StaticUnShowDialog();
39+
thenAction?.Invoke();
40+
});
41+
42+
});
43+
return task;
44+
}
45+
46+
/// <summary>
47+
/// 带参UI线程调用方法
48+
/// </summary>
49+
/// <typeparam name="T"></typeparam>
50+
/// <param name="title"></param>
51+
/// <param name="action"></param>
52+
/// <param name="thenAction"></param>
53+
/// <returns></returns>
54+
public static Task<T> InvokeOnUi<T>(string title, Func<T> action, Action<T> thenAction = null)
55+
{
56+
var task = Task.Factory.StartNew(() =>
57+
{
58+
59+
Application.Current.Dispatcher.InvokeAsync(() =>
60+
{
61+
if (string.IsNullOrEmpty(title))
62+
{
63+
title = "请稍候..";
64+
}
65+
ProgressWindow.StaticShowDialog(title);
66+
});
67+
var result= action.Invoke();
68+
69+
Application.Current.Dispatcher.InvokeAsync(() =>
70+
{
71+
72+
ProgressWindow.StaticUnShowDialog();
73+
thenAction?.Invoke(result);
74+
});
75+
return result;
76+
});
77+
return task;
78+
}
79+
}
80+
}

RoslynSyntaxTool/Helper/WebHelper.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
using System.Net;
66
using System.Net.NetworkInformation;
77
using System.Text;
8+
using System.Threading;
89
using System.Windows;
9-
using GalaSoft.MvvmLight.Messaging;
10-
using GalaSoft.MvvmLight.Threading;
1110
using Workshop.Common;
1211

1312
namespace Workshop.Helper
@@ -68,12 +67,16 @@ public string Getdata(string url, Method method = Method.GET, Dictionary<string,
6867
}
6968
catch (Exception e)
7069
{
71-
DispatcherHelper.CheckBeginInvokeOnUI(() =>
70+
71+
InvokeHelper.InvokeOnUi("正在检查网络", () =>
72+
{
73+
Thread.Sleep(2000);
74+
}).ContinueWith((t) =>
7275
{
73-
Messenger.Default.Send("", MessengerToken.CLOSEPROGRESS);
7476

75-
});
76-
MessageBox.Show("未能连接至远程服务器,请检查网络连接", "无网络", MessageBoxButton.OK, MessageBoxImage.Warning);
77+
MessageBox.Show("未能连接至远程服务器,请检查网络连接", "无网络", MessageBoxButton.OK, MessageBoxImage.Warning);
78+
}); ;
79+
7780
}
7881

7982

RoslynSyntaxTool/MainWindow.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@
112112
<!-- Items -->
113113
<mah:HamburgerMenu.ItemsSource>
114114
<mah:HamburgerMenuItemCollection>
115-
<mah:HamburgerMenuIconItem Label="主页" Tag="./View/IndexPage.xaml">
115+
<mah:HamburgerMenuIconItem Label="转换" Tag="./View/IndexPage.xaml">
116116
</mah:HamburgerMenuIconItem>
117117

118118
<mah:HamburgerMenuIconItem Label="设置" Tag="./View/SettingPage.xaml">

RoslynSyntaxTool/MainWindow.xaml.cs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
using System.Windows;
55
using System.Windows.Controls;
66
using System.Windows.Navigation;
7-
using GalaSoft.MvvmLight.Messaging;
8-
using GalaSoft.MvvmLight.Threading;
97
using GeneralServiceHost.View;
108
using Workshop.Common;
119
using Workshop.Control;
@@ -29,14 +27,8 @@ public MainWindow()
2927

3028
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
3129
{
32-
Task.Factory.StartNew(() =>
30+
var task = InvokeHelper.InvokeOnUi("正在初始化", () =>
3331
{
34-
DispatcherHelper.CheckBeginInvokeOnUI(() =>
35-
{
36-
ProgressWindow progressWindow = new ProgressWindow();
37-
progressWindow.ShowDialog("正在初始化");
38-
39-
});
4032
var SettingInfo = LocalDataService.ReadObjectLocal<SettingInfo>();
4133
if (SettingInfo == null)
4234
{
@@ -47,14 +39,6 @@ private void MainWindow_Loaded(object sender, RoutedEventArgs e)
4739
LocalDataService.SaveObjectLocal(SettingInfo);
4840

4941
}
50-
51-
52-
DispatcherHelper.CheckBeginInvokeOnUI(() =>
53-
{
54-
Messenger.Default.Send("", MessengerToken.CLOSEPROGRESS);
55-
56-
});
57-
5842
});
5943
}
6044

RoslynSyntaxTool/Model/SettingInfo.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using GalaSoft.MvvmLight;
1+
using CommunityToolkit.Mvvm.ComponentModel;
22
using System;
33
using System.Collections.Generic;
44
using System.Linq;
@@ -7,7 +7,7 @@
77

88
namespace Workshop.Model
99
{
10-
public class SettingInfo : ViewModelBase
10+
public class SettingInfo : ObservableObject
1111
{
1212

1313

@@ -20,7 +20,7 @@ public NodeKind NodeKind
2020
set
2121
{
2222
_nodeKind = value;
23-
RaisePropertyChanged(nameof(NodeKind));
23+
OnPropertyChanged(nameof(NodeKind));
2424

2525
}
2626
}
@@ -33,7 +33,7 @@ public bool OpenCurlyOnNewLine
3333
set
3434
{
3535
_openCurlyOnNewLine = value;
36-
RaisePropertyChanged(nameof(OpenCurlyOnNewLine));
36+
OnPropertyChanged(nameof(OpenCurlyOnNewLine));
3737

3838
}
3939
}
@@ -46,7 +46,7 @@ public bool CloseCurlyOnNewLine
4646
set
4747
{
4848
_closeCurlyOnNewLine = value;
49-
RaisePropertyChanged(nameof(CloseCurlyOnNewLine));
49+
OnPropertyChanged(nameof(CloseCurlyOnNewLine));
5050

5151
}
5252
}
@@ -59,7 +59,7 @@ public bool PreserveOriginalWhitespace
5959
set
6060
{
6161
_preserveOriginalWhitespace = value;
62-
RaisePropertyChanged(nameof(PreserveOriginalWhitespace));
62+
OnPropertyChanged(nameof(PreserveOriginalWhitespace));
6363

6464
}
6565
}
@@ -72,7 +72,7 @@ public bool KeepRedundantApiCalls
7272
set
7373
{
7474
_keepRedundantApiCalls = value;
75-
RaisePropertyChanged(nameof(KeepRedundantApiCalls));
75+
OnPropertyChanged(nameof(KeepRedundantApiCalls));
7676

7777
}
7878
}
@@ -85,7 +85,7 @@ public bool AvoidUsingStatic
8585
set
8686
{
8787
_avoidUsingStatic = value;
88-
RaisePropertyChanged(nameof(AvoidUsingStatic));
88+
OnPropertyChanged(nameof(AvoidUsingStatic));
8989

9090
}
9191
}

RoslynSyntaxTool/RoslynSyntaxTool.csproj

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>WinExe</OutputType>
5-
<TargetFramework>netcoreapp3.1</TargetFramework>
5+
<TargetFramework>net6.0-windows</TargetFramework>
66
<UseWPF>true</UseWPF>
77
<RootNamespace>Workshop</RootNamespace>
88
<AssemblyName>RoslynSyntaxTool</AssemblyName>
@@ -23,13 +23,13 @@
2323
</ItemGroup>
2424

2525
<ItemGroup>
26-
<PackageReference Include="CommonServiceLocator" Version="2.0.6" />
27-
<PackageReference Include="MahApps.Metro" Version="2.4.4" />
28-
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.9.0" />
29-
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Scripting" Version="3.9.0" />
30-
<PackageReference Include="MvvmLight" Version="5.4.1.1" />
31-
<PackageReference Include="MvvmLightLibs" Version="5.4.1.1" />
32-
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
26+
<PackageReference Include="AvalonEdit" Version="6.1.3.50" />
27+
<PackageReference Include="CommunityToolkit.Mvvm" Version="7.1.2" />
28+
<PackageReference Include="MahApps.Metro" Version="2.4.9" />
29+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.1.0" />
30+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Scripting" Version="4.1.0" />
31+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
32+
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
3333
</ItemGroup>
3434

3535
<ItemGroup>

0 commit comments

Comments
 (0)