Skip to content

Commit ae94bb7

Browse files
committed
New Function: Window Dialog.
1 parent 38b2e6b commit ae94bb7

13 files changed

Lines changed: 375 additions & 68 deletions

.vscode/launch.json

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
// 使用 IntelliSense 了解相关属性。
3+
// 悬停以查看现有属性的描述。
4+
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": ".NET Core Launch (console)",
9+
"type": "coreclr",
10+
"request": "launch",
11+
"警告01": "*********************************************************************************",
12+
"警告02": "The C# extension was unable to automatically decode projects in the current",
13+
"警告03": "workspace to create a runnable launch.json file. A template launch.json file has",
14+
"警告04": "been created as a placeholder.",
15+
"警告05": "",
16+
"警告06": "If the server is currently unable to load your project, you can attempt to",
17+
"警告07": "resolve this by restoring any missing project dependencies (example: run 'dotnet",
18+
"警告08": "restore') and by fixing any reported errors from building the projects in your",
19+
"警告09": "workspace.",
20+
"警告10": "If this allows the server to now load your project then --",
21+
"警告11": " * Delete this file",
22+
"警告12": " * Open the Visual Studio Code command palette (View->Command Palette)",
23+
"警告13": " * run the command: '.NET: Generate Assets for Build and Debug'.",
24+
"警告14": "",
25+
"警告15": "If your project requires a more complex launch configuration, you may wish to",
26+
"警告16": "delete this configuration and pick a different template using the 'Add",
27+
"警告17": "Configuration...' button at the bottom of this file.",
28+
"警告18": "*********************************************************************************",
29+
"preLaunchTask": "build",
30+
"program": "${workspaceFolder}/bin/Debug/<insert-target-framework-here>/<insert-project-name-here>.dll",
31+
"args": [],
32+
"cwd": "${workspaceFolder}",
33+
"console": "internalConsole",
34+
"stopAtEntry": false
35+
},
36+
{
37+
"name": ".NET Core Attach",
38+
"type": "coreclr",
39+
"request": "attach"
40+
}
41+
]
42+
}

.vscode/tasks.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
// See https://go.microsoft.com/fwlink/?LinkId=733558
3+
// for the documentation about the tasks.json format
4+
"version": "2.0.0",
5+
"tasks": [
6+
{
7+
"label": "build",
8+
"command": "dotnet",
9+
"type": "shell",
10+
"args": [
11+
"build",
12+
// Ask dotnet build to generate full paths for file names.
13+
"/property:GenerateFullPaths=true",
14+
// Do not generate summary otherwise it leads to duplicate errors in Problems panel
15+
"/consoleloggerparameters:NoSummary"
16+
],
17+
"group": "build",
18+
"presentation": {
19+
"reveal": "silent"
20+
},
21+
"problemMatcher": "$msCompile"
22+
}
23+
]
24+
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ namespace SimpleNavigation.Common
55
/// <summary>
66
/// 导航配置,可配置导航的行为,如是否允许Region重复打开同一个Page、是否保留导航历史等。
77
/// </summary>
8-
public class NavigationOptions
8+
public class DialogOptions
99
{
10-
public enum PageMode
10+
public enum DialogMode
1111
{
1212
Singleton,
1313
Transient
@@ -20,11 +20,11 @@ public enum KeepHistory
2020
}
2121

2222
/// <summary>
23-
/// 是否允许Region重复打开同一个Page,与<see cref="PageMode"/>相关联。
23+
/// 是否允许Region重复打开同一个Page,与<see cref="DialogMode"/>相关联。
2424
/// Singleton:Region仅允许导航一次;
2525
/// Transient:Region允许导航该Page多次。
2626
/// </summary>
27-
public PageMode AllowMulti = PageMode.Transient;
27+
public DialogMode AllowMulti = DialogMode.Transient;
2828

2929
/// <summary>
3030
/// 是否允许保留导航历史,影响<see cref="NavigationService.Goback(string)"/>功能。
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,23 @@
33
/// <summary>
44
/// 导航参数对象
55
/// </summary>
6-
public class NavigationParameter
6+
public class DialogParameters
77
{
88
public Dictionary<string, object> Parameters { get; } = [];
99

10-
public NavigationParameter(string key, object value)
10+
public DialogParameters(string key, object value)
1111
{
1212
if (key != null && value != null)
1313
Parameters[key] = value;
1414
}
1515

16-
public NavigationParameter(Dictionary<string, object> keyValues)
16+
public DialogParameters(Dictionary<string, object> keyValues)
1717
{
1818
if (keyValues != null)
1919
Parameters = keyValues;
2020
}
2121

22-
public NavigationParameter(params object[] values)
22+
public DialogParameters(params object[] values)
2323
{
2424
for (int i = 0; i < values.Length -1; i++)
2525
{

Common/NavigationRoute.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace SimpleNavigation.Common
66
/// 路由实例对象
77
/// </summary>
88
/// <param name="pageType">当前路由实例对应的Page类型,在判断是否允许重复导航时使用</param>
9-
/// <param name="factory">路由实例的配置,详情见:<see cref="NavigationOptions"/></param>
9+
/// <param name="factory">路由实例的配置,详情见:<see cref="DialogOptions"/></param>
1010
/// <param name="options">路由实例的构造方式,推荐使用<see cref="DependencyInjection"/>,以支持更复杂的页面构造需求。</param>
1111
internal class NavigationRoute
1212
{
@@ -15,7 +15,7 @@ internal class NavigationRoute
1515
/// </summary>
1616
public Guid Id { get; }
1717

18-
public NavigationRoute(Type pageType, Func<Page> factory, NavigationOptions options)
18+
public NavigationRoute(Type pageType, Func<Page> factory, DialogOptions options)
1919
{
2020
Id = new Guid();
2121
this.factory = factory;
@@ -29,9 +29,9 @@ public NavigationRoute(Type pageType, Func<Page> factory, NavigationOptions opti
2929
private readonly Func<Page> factory;
3030

3131
/// <summary>
32-
/// 路由实例的配置,详情见:<see cref="NavigationOptions"/>
32+
/// 路由实例的配置,详情见:<see cref="DialogOptions"/>
3333
/// </summary>
34-
public NavigationOptions Options { get; }
34+
public DialogOptions Options { get; }
3535

3636
/// <summary>
3737
/// 当前路由实例对应的Page类型,在判断是否允许重复导航时使用
@@ -50,7 +50,7 @@ public NavigationRoute(Type pageType, Func<Page> factory, NavigationOptions opti
5050
/// <returns>路由实例</returns>
5151
public Page GetPage()
5252
{
53-
if (Options.AllowMulti == NavigationOptions.PageMode.Singleton)
53+
if (Options.AllowMulti == DialogOptions.DialogMode.Singleton)
5454
return cachePage ??= factory();
5555

5656
return factory();

Common/Serilogger.cs

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1+
using System.Windows;
2+
using SimpleNavigation.Interface;
3+
14
namespace SimpleNavigation.Common
25
{
36
/// <summary>
4-
/// Discarded class.
7+
/// Serilog日志记录器,用于全局日志记录。
58
/// </summary>
69
public sealed class Serilogger : IDisposable
710
{
811
private bool disposed = false;
912
private static readonly object loggerLock = new();
1013
private static int _instanceSet = 0;
1114

12-
private static volatile ISerilog _instance;
15+
private static volatile ISerilog? _instance;
1316
public static ISerilog Instance
1417
{
1518
get
@@ -18,43 +21,39 @@ public static ISerilog Instance
1821
throw new ArgumentNullException($"{nameof(_instance)} is null.");
1922
return _instance;
2023
}
24+
private set => _instance = value;
2125
}
2226

2327
private Serilogger() { }
2428

2529
public static void SetInstance(ISerilog logger)
2630
{
27-
if (logger == null) throw new ArgumentNullException($"{nameof(logger)} is null.");
28-
29-
if (Interlocked.CompareExchange(ref _instanceSet, 1, 0) != 0)
30-
{
31-
throw InvalidaOperationException($"{nameof(Instance)} has been initialized!");
32-
}
31+
ArgumentNullException.ThrowIfNull(logger);
3332

3433
lock(loggerLock)
3534
{
3635
if (Instance != null)
3736
{
3837
Interlocked.Exchange(ref _instanceSet, 0);
39-
throw InvalidaOperationException($"{nameof(Instance)} has been initialized!");
38+
throw new InvalidOperationException($"{nameof(Instance)} has been initialized!");
4039
}
4140

42-
this.Instance = logger;
41+
Instance = logger;
4342
}
4443
}
4544

46-
private void Dispose()
45+
public void Dispose()
4746
{
4847
if (disposed)
49-
return;
50-
51-
// Dispose(true);
52-
GC.SuppressFinalize(this);
53-
Instance.Dispose();
54-
55-
disposed = true;
56-
_instance = null;
57-
Interlocked.Exchange(ref _instanceSet, 0);
48+
return;
49+
50+
51+
GC.SuppressFinalize(this);
52+
Instance.Dispose();
53+
54+
disposed = true;
55+
_instance = null;
56+
Interlocked.Exchange(ref _instanceSet, 0);
5857
}
5958
}
6059
}

Common/WindowRegistration.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System.Windows;
2+
3+
namespace SimpleNavigation.Common
4+
{
5+
/// <summary>
6+
/// 窗口注册类
7+
/// </summary>
8+
public class WindowRegistration
9+
{
10+
private readonly Func<Window> factory;
11+
private Window? cacheWindow;
12+
13+
/// <summary>
14+
/// 唯一标识
15+
/// </summary>
16+
public Guid Id { get; } = new Guid();
17+
18+
/// <summary>
19+
/// 窗口类型
20+
/// </summary>
21+
public Type WindowType { get; set; } = null!;
22+
23+
public DialogOptions Options { get; set; } = null!;
24+
25+
public WindowRegistration(Type windowType, DialogOptions options, Func<Window> factory)
26+
{
27+
WindowType = windowType;
28+
Options = options;
29+
this.factory = factory;
30+
}
31+
32+
public Window GetWindow()
33+
{
34+
if (Options.AllowMulti == DialogOptions.DialogMode.Singleton)
35+
return cacheWindow ??= factory();
36+
37+
return factory();
38+
}
39+
}
40+
41+
}

Interface/INavigationAware.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ public interface INavigationAware
1111
/// 导航即将发生时的回调方法
1212
/// </summary>
1313
/// <param name="parameters">导航携带的参数</param>
14-
void OnNavigating(NavigationParameter? parameters);
14+
void OnNavigating(DialogParameters? parameters);
1515

1616
/// <summary>
1717
/// 导航完成后的回调方法
1818
/// </summary>
1919
/// <param name="parameters"></param>
20-
void OnNavigated(NavigationParameter? parameters);
20+
void OnNavigated(DialogParameters? parameters);
2121
}
2222
}

Interface/INavigationService.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@ public interface INavigationService
1616
/// </summary>
1717
/// <param name="region">导航的目标区域</param>
1818
/// <param name="route">导航的目标路由</param>
19-
/// <param name="parameters">导航时所携带的参数,详情见:<see cref="NavigationParameter"/></param>
20-
void Navigate(string region, string route, NavigationParameter? parameters = null);
19+
/// <param name="parameters">导航时所携带的参数,详情见:<see cref="DialogParameters"/></param>
20+
void Navigate(string region, string route, DialogParameters? parameters = null);
2121

2222
/// <summary>
2323
/// 导航功能,根据提供的路由Key和区域Key导航到目标路由(Page),并且可以传递参数。导航过程中会触发页面的<see cref="INavigationAware"/>接口方法,允许在导航前后执行特定逻辑。
2424
/// </summary>
2525
/// <typeparam name="TPage">导航的目标路由。通过TPage.FullName来获取指定的路由Key</typeparam>
2626
/// <param name="region">导航的目标区域</param>
27-
/// <param name="parameter">导航时所携带的参数,详情见:<see cref="NavigationParameter"/></param>
28-
void Navigate<TPage>(string region, NavigationParameter? parameters = null);
27+
/// <param name="parameters">导航时所携带的参数,详情见:<see cref="DialogParameters"/></param>
28+
void Navigate<TPage>(string region, DialogParameters? parameters = null);
2929

3030
/// <summary>
3131
/// 注册区域,区域是导航的目标容器,允许在不同的区域导航到不同的页面,甚至在同一区域导航到同一个页面但传递不同的参数。
@@ -39,41 +39,41 @@ public interface INavigationService
3939
/// </summary>
4040
/// <param name="route">路由Key,<see cref="Routes"/></param>
4141
/// <param name="factory">路由对象的构造方式</param>
42-
/// <param name="options">详情查看<see cref="NavigationOptions"/></param>
42+
/// <param name="options">详情查看<see cref="DialogOptions"/></param>
4343
/// <param name="pageType">记录Page类型,用于判断是否能重复导航,<see cref="NavigationRoute.PageType"/></param>
44-
void RegisterRoute(string route, Func<Page> factory, NavigationOptions? options = null, Type? pageType = null);
44+
void RegisterRoute(string route, Func<Page> factory, DialogOptions? options = null, Type? pageType = null);
4545

4646
/// <summary>
4747
/// 注册路由,默认使用类型名称作为路由Key,并且要求Page具有无参构造函数。
4848
/// </summary>
4949
/// <typeparam name="TPage">记录路由的Page类型,同时作为路由Key,<see cref="NavigationRoute.PageType"/></typeparam>
50-
/// <param name="options">详情查看<see cref="NavigationOptions"/></param>
51-
void RegisterRoute<TPage>(NavigationOptions? options = null) where TPage : Page, new();
50+
/// <param name="options">详情查看<see cref="DialogOptions"/></param>
51+
void RegisterRoute<TPage>(DialogOptions? options = null) where TPage : Page, new();
5252

5353
/// <summary>
5454
/// 注册路由,要求提供Page的构造方式,推荐使用<see cref="DependencyInjection"/>
5555
/// </summary>
5656
/// <typeparam name="TPage">记录路由的Page类型,<see cref="NavigationRoute.PageType"/></typeparam>
5757
/// <param name="factory">路由对象的构造方式</param>
58-
/// <param name="options">详情查看<see cref="NavigationOptions"/></param>
59-
void RegisterRoute<TPage>(Func<Page> factory, NavigationOptions? options = null);
58+
/// <param name="options">详情查看<see cref="DialogOptions"/></param>
59+
void RegisterRoute<TPage>(Func<Page> factory, DialogOptions? options = null);
6060

6161
/// <summary>
6262
/// 注册路由,要求Page具有无参构造函数。
6363
/// </summary>
6464
/// <typeparam name="TPage">记录路由的Page类型,<see cref="NavigationRoute.PageType"/></typeparam>
6565
/// <param name="route">路由Key,<see cref="Routes"/></param>
66-
/// <param name="options">详情查看<see cref="NavigationOptions"/></param>
67-
void RegisterRoute<TPage>(string route, NavigationOptions? options = null) where TPage : Page, new();
66+
/// <param name="options">详情查看<see cref="DialogOptions"/></param>
67+
void RegisterRoute<TPage>(string route, DialogOptions? options = null) where TPage : Page, new();
6868

6969
/// <summary>
7070
/// 注册路由,要求提供Page的构造方式,推荐使用<see cref="DependencyInjection"/>
7171
/// </summary>
7272
/// <typeparam name="TPage">记录路由的Page类型,<see cref="NavigationRoute.PageType"/></typeparam>
7373
/// <param name="route">路由Key,<see cref="Routes"/></param>
7474
/// <param name="factory">路由对象的构造方式</param>
75-
/// <param name="options">详情查看<see cref="NavigationOptions"/></param>
76-
void RegisterRoute<TPage>(string route, Func<Page> factory, NavigationOptions? options = null) where TPage : Page;
75+
/// <param name="options">详情查看<see cref="DialogOptions"/></param>
76+
void RegisterRoute<TPage>(string route, Func<Page> factory, DialogOptions? options = null) where TPage : Page;
7777

7878
/// <summary>
7979
/// 取消注册区域,根据提供的区域Key从区域表中移除对应的区域记录,取消注册后将无法通过该区域进行导航。

0 commit comments

Comments
 (0)