From 08af4faf59e6c488f6744ae278607e5e63145ede Mon Sep 17 00:00:00 2001 From: Leo Yang Date: Wed, 1 Dec 2021 17:57:29 +0800 Subject: [PATCH 1/8] Update README.md Add contact information. --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 38873d8..5ee5428 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,12 @@ wif 一个基础设施框架,可帮助您快速开发WPF应用程序。 +## 联系方式 + +邮箱:leolion610@foxmail.com + + + ## Licence 该项目根据[MIT许可证授权](https://github.com/LeoYang-Chuese/wif/blob/master/LICENSE)。 From 8fdde1161e65988c0d48888b56dbda71fd00decb Mon Sep 17 00:00:00 2001 From: Leo Yang Date: Thu, 16 Dec 2021 10:40:52 +0800 Subject: [PATCH 2/8] .NET 4.6.2 is supported. --- .../Wif.Demo.Common/Wif.Demo.Common.csproj | 42 ++++---- Source/Demos/Wif.Demo/Wif.Demo.csproj | 80 ++++++++-------- Source/Wif.Core/Wif.Core.csproj | 90 ++++++++--------- .../Wif.Infrastructure.csproj | 96 +++++++++---------- Source/Wif.Utilities/Wif.Utilities.csproj | 88 ++++++++--------- 5 files changed, 198 insertions(+), 198 deletions(-) diff --git a/Source/Demos/Wif.Demo.Common/Wif.Demo.Common.csproj b/Source/Demos/Wif.Demo.Common/Wif.Demo.Common.csproj index f0e2213..f172bb1 100644 --- a/Source/Demos/Wif.Demo.Common/Wif.Demo.Common.csproj +++ b/Source/Demos/Wif.Demo.Common/Wif.Demo.Common.csproj @@ -1,25 +1,25 @@ - - net472;netcoreapp3.1;net5.0-windows - true - default - - - ..\Output\Debug\ - - - ..\Output\Release\ - - - - - - - - - - - + + net462;net472;netcoreapp3.1;net5.0-windows + true + default + + + ..\Output\Debug\ + + + ..\Output\Release\ + + + + + + + + + + + diff --git a/Source/Demos/Wif.Demo/Wif.Demo.csproj b/Source/Demos/Wif.Demo/Wif.Demo.csproj index 0829ffa..3a5f8c3 100644 --- a/Source/Demos/Wif.Demo/Wif.Demo.csproj +++ b/Source/Demos/Wif.Demo/Wif.Demo.csproj @@ -1,44 +1,44 @@  - - WinExe - net472;netcoreapp3.1;net5.0-windows - true - default - - - ..\Output\Debug\ - - - ..\Output\Release\ - - - - - - - - - - - - - 4.0 - - - - - - - - - - - - - - - - + + WinExe + net462;net472;netcoreapp3.1;net5.0-windows + true + default + + + ..\Output\Debug\ + + + ..\Output\Release\ + + + + + + + + + + + + + 4.0 + + + + + + + + + + + + + + + + diff --git a/Source/Wif.Core/Wif.Core.csproj b/Source/Wif.Core/Wif.Core.csproj index 6a78f7b..5ef1a09 100644 --- a/Source/Wif.Core/Wif.Core.csproj +++ b/Source/Wif.Core/Wif.Core.csproj @@ -1,47 +1,47 @@  - - net472;netcoreapp3.1;net5.0-windows - true - Frontier.Wif.Core - 0.3.0.21286 - LeoYang-Chuese - WPF Infrastructure Framework(WIF)is an infrastructure framework that helps you develop WPF applications quickly. - Copyright © 2021 LeoYang-Chuese. All rights reserved. - MIT - https://github.com/LeoYang-Chuese/wif - wif_logo.png - https://github.com/LeoYang-Chuese/wif.git - git - WPF;UI;Infrastructure;Utilities;Modern;XAML;Toolkit;Library;.NET;OpenSource - https://github.com/LeoYang-Chuese/wif/releases - default - - - ..\Output\Debug\ - ..\Output\Debug\Wif.Core.xml - - - ..\Output\Release\ - ..\Output\Release\Wif.Core.xml - - - - - - - - - - - - True - - - - - - all - runtime; build; native; contentfiles; analyzers; buildtransitive - - + + net462;net472;netcoreapp3.1;net5.0-windows + true + Frontier.Wif.Core + 0.3.0.21349 + LeoYang-Chuese + WPF Infrastructure Framework(WIF)is an infrastructure framework that helps you develop WPF applications quickly. + Copyright © 2021 LeoYang-Chuese. All rights reserved. + MIT + https://github.com/LeoYang-Chuese/wif + wif_logo.png + https://github.com/LeoYang-Chuese/wif.git + git + WPF;UI;Infrastructure;Utilities;Modern;XAML;Toolkit;Library;.NET;OpenSource + https://github.com/LeoYang-Chuese/wif/releases + default + + + ..\Output\Debug\ + ..\Output\Debug\Wif.Core.xml + + + ..\Output\Release\ + ..\Output\Release\Wif.Core.xml + + + + + + + + + + + + True + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + \ No newline at end of file diff --git a/Source/Wif.Infrastructure/Wif.Infrastructure.csproj b/Source/Wif.Infrastructure/Wif.Infrastructure.csproj index a5a69a8..da16de4 100644 --- a/Source/Wif.Infrastructure/Wif.Infrastructure.csproj +++ b/Source/Wif.Infrastructure/Wif.Infrastructure.csproj @@ -1,50 +1,50 @@  - - net472;netcoreapp3.1;net5.0-windows - true - Frontier.Wif.Infrastructure - 0.3.0.21286 - WPF Infrastructure Framework(WIF)is an infrastructure framework that helps you develop WPF applications quickly. - LeoYang-Chuese - Copyright © 2021 LeoYang-Chuese. All rights reserved. - MIT - https://github.com/LeoYang-Chuese/wif - https://github.com/LeoYang-Chuese/wif.git - wif_logo.png - WPF;UI;Infrastructure;Utilities;Modern;XAML;Toolkit;Library;.NET;OpenSource - git - https://github.com/LeoYang-Chuese/wif/releases - a46f13f6-c033-4863-be99-bcc4634651c6 - false - - - ..\Output\Debug\ - ..\Output\Debug\Wif.Infrastructure.xml - - - ..\Output\Release\ - ..\Output\Release\Wif.Infrastructure.xml - - - - - all - runtime; build; native; contentfiles; analyzers; buildtransitive - - - - - - - - - - - - - - True - - - + + net462;net472;netcoreapp3.1;net5.0-windows + true + Frontier.Wif.Infrastructure + 0.3.0.21349 + WPF Infrastructure Framework(WIF)is an infrastructure framework that helps you develop WPF applications quickly. + LeoYang-Chuese + Copyright © 2021 LeoYang-Chuese. All rights reserved. + MIT + https://github.com/LeoYang-Chuese/wif + https://github.com/LeoYang-Chuese/wif.git + wif_logo.png + WPF;UI;Infrastructure;Utilities;Modern;XAML;Toolkit;Library;.NET;OpenSource + git + https://github.com/LeoYang-Chuese/wif/releases + a46f13f6-c033-4863-be99-bcc4634651c6 + false + + + ..\Output\Debug\ + ..\Output\Debug\Wif.Infrastructure.xml + + + ..\Output\Release\ + ..\Output\Release\Wif.Infrastructure.xml + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + + + + + + True + + + \ No newline at end of file diff --git a/Source/Wif.Utilities/Wif.Utilities.csproj b/Source/Wif.Utilities/Wif.Utilities.csproj index 98e7941..1ec63fe 100644 --- a/Source/Wif.Utilities/Wif.Utilities.csproj +++ b/Source/Wif.Utilities/Wif.Utilities.csproj @@ -1,46 +1,46 @@  - - net472;netcoreapp3.1;net5.0-windows - true - true - Frontier.Wif.Utilities - 0.3.0.21286 - LeoYang-Chuese - WPF Infrastructure Framework(WIF)is an infrastructure framework that helps you develop WPF applications quickly. - Copyright © 2021 LeoYang-Chuese. All rights reserved. - MIT - https://github.com/LeoYang-Chuese/wif - WPF;UI;Infrastructure;Utilities;Modern;XAML;Toolkit;Library;.NET;OpenSource - https://github.com/LeoYang-Chuese/wif/releases - wif_logo.png - https://github.com/LeoYang-Chuese/wif.git - git - default - - - ..\Output\Debug\ - ..\Output\Debug\Wif.Utilities.xml - - - ..\Output\Release\ - ..\Output\Release\Wif.Utilities.xml - - - - all - runtime; build; native; contentfiles; analyzers; buildtransitive - - - - - - - - - - - True - - - + + net462;net472;netcoreapp3.1;net5.0-windows + true + true + Frontier.Wif.Utilities + 0.3.0.21349 + LeoYang-Chuese + WPF Infrastructure Framework(WIF)is an infrastructure framework that helps you develop WPF applications quickly. + Copyright © 2021 LeoYang-Chuese. All rights reserved. + MIT + https://github.com/LeoYang-Chuese/wif + WPF;UI;Infrastructure;Utilities;Modern;XAML;Toolkit;Library;.NET;OpenSource + https://github.com/LeoYang-Chuese/wif/releases + wif_logo.png + https://github.com/LeoYang-Chuese/wif.git + git + default + + + ..\Output\Debug\ + ..\Output\Debug\Wif.Utilities.xml + + + ..\Output\Release\ + ..\Output\Release\Wif.Utilities.xml + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + + + True + + + \ No newline at end of file From 300bf3e49f3f2fd8543acb012e883f3839101cf3 Mon Sep 17 00:00:00 2001 From: Leo Yang Date: Fri, 8 Apr 2022 18:58:24 +0800 Subject: [PATCH 3/8] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5ee5428..1ab191a 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ wif 一个基础设施框架,可帮助您快速开发WPF应用程序。 支持 .NET Framework (4.7.2+) 和 .NET Core (3.1 and 5.0) -[![Nuget](https://img.shields.io/nuget/dt/Wif.Infrastructure.svg)](https://www.nuget.org/packages/Wif.Infrastructure/) [![nuget-version](https://img.shields.io/nuget/v/Wif.Infrastructure.svg)](https://www.nuget.org/packages/Wif.Infrastructure) ![dotnet-version](https://img.shields.io/badge/.NET%20Framework-%3E%3D4.7.2-blue.svg) ![dotnetcore version](https://img.shields.io/badge/.NET%20Core-%3E%3D3.1-blue.svg) ![csharp-version](https://img.shields.io/badge/C%23-8.0-blue.svg) ![IDE-version](https://img.shields.io/badge/IDE-vs2019-blue.svg) [![博客园](https://img.shields.io/badge/%E5%8D%9A%E5%AE%A2%E5%9B%AD-%E6%A5%9A%E4%BA%BALeo-brightgreen.svg)](https://www.cnblogs.com/leolion/) +[![Nuget](https://img.shields.io/nuget/dt/Wif.Infrastructure.svg)](https://www.nuget.org/packages/Wif.Utilities/) [![nuget-version](https://img.shields.io/nuget/v/Wif.Infrastructure.svg)](https://www.nuget.org/packages/Wif.Infrastructure) ![dotnet-version](https://img.shields.io/badge/.NET%20Framework-%3E%3D4.7.2-blue.svg) ![dotnetcore version](https://img.shields.io/badge/.NET%20Core-%3E%3D3.1-blue.svg) ![csharp-version](https://img.shields.io/badge/C%23-8.0-blue.svg) ![IDE-version](https://img.shields.io/badge/IDE-vs2019-blue.svg) [![博客园](https://img.shields.io/badge/%E5%8D%9A%E5%AE%A2%E5%9B%AD-%E6%A5%9A%E4%BA%BALeo-brightgreen.svg)](https://www.cnblogs.com/leolion/) From be0e1fc97fffa3805d3411343ab57d91f900bde1 Mon Sep 17 00:00:00 2001 From: Leo Yang Date: Fri, 8 Apr 2022 19:02:19 +0800 Subject: [PATCH 4/8] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1ab191a..602ada1 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ wif 一个基础设施框架,可帮助您快速开发WPF应用程序。 支持 .NET Framework (4.7.2+) 和 .NET Core (3.1 and 5.0) -[![Nuget](https://img.shields.io/nuget/dt/Wif.Infrastructure.svg)](https://www.nuget.org/packages/Wif.Utilities/) [![nuget-version](https://img.shields.io/nuget/v/Wif.Infrastructure.svg)](https://www.nuget.org/packages/Wif.Infrastructure) ![dotnet-version](https://img.shields.io/badge/.NET%20Framework-%3E%3D4.7.2-blue.svg) ![dotnetcore version](https://img.shields.io/badge/.NET%20Core-%3E%3D3.1-blue.svg) ![csharp-version](https://img.shields.io/badge/C%23-8.0-blue.svg) ![IDE-version](https://img.shields.io/badge/IDE-vs2019-blue.svg) [![博客园](https://img.shields.io/badge/%E5%8D%9A%E5%AE%A2%E5%9B%AD-%E6%A5%9A%E4%BA%BALeo-brightgreen.svg)](https://www.cnblogs.com/leolion/) +[![Nuget](https://img.shields.io/nuget/dt/Wif.Utilities.svg)](https://www.nuget.org/packages/Wif.Utilities/) [![nuget-version](https://img.shields.io/nuget/v/Wif.Infrastructure.svg)](https://www.nuget.org/packages/Wif.Infrastructure) ![dotnet-version](https://img.shields.io/badge/.NET%20Framework-%3E%3D4.7.2-blue.svg) ![dotnetcore version](https://img.shields.io/badge/.NET%20Core-%3E%3D3.1-blue.svg) ![csharp-version](https://img.shields.io/badge/C%23-8.0-blue.svg) ![IDE-version](https://img.shields.io/badge/IDE-vs2019-blue.svg) [![博客园](https://img.shields.io/badge/%E5%8D%9A%E5%AE%A2%E5%9B%AD-%E6%A5%9A%E4%BA%BALeo-brightgreen.svg)](https://www.cnblogs.com/leolion/) From 461735eae52ca7293c481ecf85ecd64a43236ef4 Mon Sep 17 00:00:00 2001 From: yangzheng06 Date: Fri, 8 Mar 2024 19:47:08 +0800 Subject: [PATCH 5/8] Update framework supports the latest version. --- .../Wif.Demo.Common/Wif.Demo.Common.csproj | 2 +- Source/Demos/Wif.Demo/Wif.Demo.csproj | 2 +- Source/Wif.Core/Wif.Core.csproj | 8 +++++++- .../Wif.Infrastructure.csproj | 8 +++++++- .../BinarySerializationExtensions.cs | 4 ++++ .../Helpers/SerializationHelper.cs | 4 ++++ Source/Wif.Utilities/Wif.Utilities.csproj | 20 ++++++++++++++++++- Source/Wif.sln.DotSettings | 3 ++- 8 files changed, 45 insertions(+), 6 deletions(-) diff --git a/Source/Demos/Wif.Demo.Common/Wif.Demo.Common.csproj b/Source/Demos/Wif.Demo.Common/Wif.Demo.Common.csproj index f172bb1..19dad82 100644 --- a/Source/Demos/Wif.Demo.Common/Wif.Demo.Common.csproj +++ b/Source/Demos/Wif.Demo.Common/Wif.Demo.Common.csproj @@ -1,7 +1,7 @@ - net462;net472;netcoreapp3.1;net5.0-windows + net462;net472;net6.0-windows;net8.0-windows true default diff --git a/Source/Demos/Wif.Demo/Wif.Demo.csproj b/Source/Demos/Wif.Demo/Wif.Demo.csproj index 3a5f8c3..8cfd6f0 100644 --- a/Source/Demos/Wif.Demo/Wif.Demo.csproj +++ b/Source/Demos/Wif.Demo/Wif.Demo.csproj @@ -2,7 +2,7 @@ WinExe - net462;net472;netcoreapp3.1;net5.0-windows + net462;net472;net6.0-windows;net8.0-windows true default diff --git a/Source/Wif.Core/Wif.Core.csproj b/Source/Wif.Core/Wif.Core.csproj index 5ef1a09..0ea626b 100644 --- a/Source/Wif.Core/Wif.Core.csproj +++ b/Source/Wif.Core/Wif.Core.csproj @@ -1,6 +1,6 @@  - net462;net472;netcoreapp3.1;net5.0-windows + net462;net472;net6.0-windows;net8.0-windows true Frontier.Wif.Core 0.3.0.21349 @@ -24,6 +24,12 @@ ..\Output\Release\ ..\Output\Release\Wif.Core.xml + + 7 + + + 7 + diff --git a/Source/Wif.Infrastructure/Wif.Infrastructure.csproj b/Source/Wif.Infrastructure/Wif.Infrastructure.csproj index da16de4..19ff108 100644 --- a/Source/Wif.Infrastructure/Wif.Infrastructure.csproj +++ b/Source/Wif.Infrastructure/Wif.Infrastructure.csproj @@ -1,6 +1,6 @@  - net462;net472;netcoreapp3.1;net5.0-windows + net462;net472;net6.0-windows;net8.0-windows true Frontier.Wif.Infrastructure 0.3.0.21349 @@ -25,6 +25,12 @@ ..\Output\Release\ ..\Output\Release\Wif.Infrastructure.xml + + 7 + + + 7 + diff --git a/Source/Wif.Utilities/Extensions/BinarySerializationExtensions.cs b/Source/Wif.Utilities/Extensions/BinarySerializationExtensions.cs index 434d38d..35a997d 100644 --- a/Source/Wif.Utilities/Extensions/BinarySerializationExtensions.cs +++ b/Source/Wif.Utilities/Extensions/BinarySerializationExtensions.cs @@ -28,9 +28,11 @@ public static class BinarySerializationExtensions /// public static T Deserialize(this Stream stream) { +#pragma warning disable SYSLIB0011 var formatter = new BinaryFormatter(); stream.Position = 0; return (T) formatter.Deserialize(stream); +#pragma warning disable SYSLIB0011 } /// @@ -40,10 +42,12 @@ public static T Deserialize(this Stream stream) /// public static Stream Serialize(this object source) { +#pragma warning disable SYSLIB0011 var formatter = new BinaryFormatter(); var stream = new MemoryStream(); formatter.Serialize(stream, source); return stream; +#pragma warning disable SYSLIB0011 } #endregion diff --git a/Source/Wif.Utilities/Helpers/SerializationHelper.cs b/Source/Wif.Utilities/Helpers/SerializationHelper.cs index 2b543fe..56da7d0 100644 --- a/Source/Wif.Utilities/Helpers/SerializationHelper.cs +++ b/Source/Wif.Utilities/Helpers/SerializationHelper.cs @@ -188,6 +188,7 @@ public static void SerializeObjectToXmlFile(object sourceObject, string targetFi /// private static T DeserializeFromBytes(byte[] source) { +#pragma warning disable SYSLIB0011 var obj = default(T); IFormatter formatter = new BinaryFormatter(); T result; @@ -198,6 +199,7 @@ private static T DeserializeFromBytes(byte[] source) } return result; +#pragma warning disable SYSLIB0011 } /// @@ -208,6 +210,7 @@ private static T DeserializeFromBytes(byte[] source) /// private static byte[] SerializeToBytes(T obj) { +#pragma warning disable SYSLIB0011 IFormatter formatter = new BinaryFormatter(); byte[] result; using (var memoryStream = new MemoryStream()) @@ -218,6 +221,7 @@ private static byte[] SerializeToBytes(T obj) } return result; +#pragma warning disable SYSLIB0011 } #endregion diff --git a/Source/Wif.Utilities/Wif.Utilities.csproj b/Source/Wif.Utilities/Wif.Utilities.csproj index 1ec63fe..5ab2831 100644 --- a/Source/Wif.Utilities/Wif.Utilities.csproj +++ b/Source/Wif.Utilities/Wif.Utilities.csproj @@ -1,6 +1,6 @@  - net462;net472;netcoreapp3.1;net5.0-windows + net462;net472;net6.0-windows;net8.0-windows true true Frontier.Wif.Utilities @@ -25,6 +25,24 @@ ..\Output\Release\ ..\Output\Release\Wif.Utilities.xml + + 6 + + + 6 + + + 4 + + + 4 + + + 7 + + + 7 + all diff --git a/Source/Wif.sln.DotSettings b/Source/Wif.sln.DotSettings index 6c7ad0b..3b512b3 100644 --- a/Source/Wif.sln.DotSettings +++ b/Source/Wif.sln.DotSettings @@ -321,6 +321,7 @@ 12 2 0 + True True True True @@ -343,7 +344,7 @@ True True True - anonymous + C:\Users\leoli\AppData\Local\Temp\JetBrains\58a2b74a-22b9-439d-a5b4-772306c6323a True True From a1a8021990d8ac573acd1b8420cb6db02656018e Mon Sep 17 00:00:00 2001 From: yangzheng06 Date: Fri, 8 Mar 2024 20:05:46 +0800 Subject: [PATCH 6/8] =?UTF-8?q?Added=20ByteExtension=E3=80=81CharEncodingC?= =?UTF-8?q?onverter=20class,update=20VisualTreeHelperExtensions=20class.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Wif.Utilities/Extensions/ByteExtension.cs | 82 +++ .../Extensions/CharEncodingConverter.cs | 84 ++++ .../Extensions/VisualTreeHelperExtensions.cs | 472 +++++++++++------- 3 files changed, 470 insertions(+), 168 deletions(-) create mode 100644 Source/Wif.Utilities/Extensions/ByteExtension.cs create mode 100644 Source/Wif.Utilities/Extensions/CharEncodingConverter.cs diff --git a/Source/Wif.Utilities/Extensions/ByteExtension.cs b/Source/Wif.Utilities/Extensions/ByteExtension.cs new file mode 100644 index 0000000..c0da329 --- /dev/null +++ b/Source/Wif.Utilities/Extensions/ByteExtension.cs @@ -0,0 +1,82 @@ +/************************************************************************** + * File Name:SerializationHelper.cs + * Description:SerializationHelper.cs class description... + * Copyright:Copyright © 2020 LeoYang-Chuese. All rights reserved. + * Creator:Leo Yang + * Create Time:2024/03/08 + *Project Address:https://github.com/LeoYang-Chuese/wif + **************************************************************************/ + + +using System; +using System.Collections; +using System.Linq; + +namespace Frontier.Wif.Utilities.Extensions +{ + /// + /// 表示bit、byte和数据类型转换的扩展方法。 + /// + public static class ByteExtension + { + #region BitArray Extension + + /// + /// Convert bits to bytes. + /// + /// + /// + public static byte[] ToBytes(this BitArray bitArray) + { + int length = bitArray.Length / 8 + (bitArray.Length % 8 == 0 ? 0 : 1); + var array = new byte[length]; + bitArray.CopyTo(array, 0); + return array; + } + + /// + /// Convert bits to short. + /// + /// + /// + public static short ToShort(this BitArray bitArray) + { + byte[] bytes = bitArray.ToBytes(); + return bytes.ToShort(); + } + + #endregion BitArray Extension + + #region ValueType Extension + + /// + /// Convert short to bits. + /// + /// + /// + public static BitArray ToBitArray(this short value) + { + byte[] bytes = BitConverter.GetBytes(value); + var bits = new BitArray(bytes); + return bits; + } + + /// + /// Convert bytes to bits. + /// + /// + /// + public static short ToShort(this byte[] bytes) + { + if (BitConverter.IsLittleEndian) + { + return BitConverter.ToInt16(bytes, 0); + } + + byte[] revValue = bytes.Reverse().ToArray(); + return BitConverter.ToInt16(revValue, 0); + } + + #endregion ValueType Extension + } +} \ No newline at end of file diff --git a/Source/Wif.Utilities/Extensions/CharEncodingConverter.cs b/Source/Wif.Utilities/Extensions/CharEncodingConverter.cs new file mode 100644 index 0000000..02183ce --- /dev/null +++ b/Source/Wif.Utilities/Extensions/CharEncodingConverter.cs @@ -0,0 +1,84 @@ +/************************************************************************** + * File Name:SerializationHelper.cs + * Description:SerializationHelper.cs class description... + * Copyright:Copyright © 2020 LeoYang-Chuese. All rights reserved. + * Creator:Leo Yang + * Create Time:2024/03/08 + *Project Address:https://github.com/LeoYang-Chuese/wif + **************************************************************************/ + + +using System; +using System.Text; + +namespace Frontier.Wif.Utilities.Extensions +{ + /// + /// 表示字符编码转换器。 + /// + public class CharEncodingConverter + { + #region 字符串转16进制编解码 + + /// + /// 字符串转十六进制编码。 + /// + /// + /// + public static string StringToHex(string str) + { + var sb = new StringBuilder(); + foreach (char c in str) + { + sb.Append(((int)c).ToString("X4")); + } + + return sb.ToString(); + } + + /// + /// 十六进制编码转字符串。 + /// + /// + /// 出现不符合十六进制字符则返回空字符。 + /// true:转换成功;false:转换失败 + public static bool HexToString(string hex, out string str) + { + str = string.Empty; + var sb = new StringBuilder(); + for (var i = 0; i < hex.Length; i += 4) + { + string hexChar = hex.Substring(i, 4); + if (!IsHex(hexChar)) + { + return false; + } + + sb.Append((char)Convert.ToInt32(hexChar, 16)); + } + + str = sb.ToString(); + return true; + } + + /// + /// 判断字符串是否符合十六进制数。 + /// + /// + /// + public static bool IsHex(string str) + { + foreach (char c in str) + { + if (!Uri.IsHexDigit(c)) + { + return false; + } + } + + return true; + } + + #endregion + } +} \ No newline at end of file diff --git a/Source/Wif.Utilities/Extensions/VisualTreeHelperExtensions.cs b/Source/Wif.Utilities/Extensions/VisualTreeHelperExtensions.cs index 6cb0060..b6d13b8 100644 --- a/Source/Wif.Utilities/Extensions/VisualTreeHelperExtensions.cs +++ b/Source/Wif.Utilities/Extensions/VisualTreeHelperExtensions.cs @@ -1,73 +1,126 @@ /************************************************************************** -* File Name:VisualTreeHelperExtensions.cs -* Description:VisualTreeHelperExtensions.cs class description... -* Copyright:Copyright © 2020 LeoYang-Chuese. All rights reserved. -* Creator:Leo Yang -* Create Time:2020/12/15 -*Project Address:https://github.com/LeoYang-Chuese/wif -**************************************************************************/ - + * File Name:VisualTreeHelperExtensions.cs + * Description:VisualTreeHelperExtensions.cs class description... + * Copyright:Copyright © 2020 LeoYang-Chuese. All rights reserved. + * Creator:Leo Yang + * Create Time:2020/12/15 + *Project Address:https://github.com/LeoYang-Chuese/wif + **************************************************************************/ using System; using System.Collections.Generic; +using System.Linq; using System.Windows; using System.Windows.Controls.Primitives; using System.Windows.Media; +using System.Windows.Media.Media3D; namespace Frontier.Wif.Utilities.Extensions { /// - /// 查找控件扩展方法 - /// http://www.hardcodet.net/uploads/2009/06/UIHelper.cs + /// 查找控件扩展方法 http://www.hardcodet.net/uploads/2009/06/UIHelper.cs /// public static class VisualTreeHelperExtensions { #region Methods + /// + /// The GetParent + /// + /// The + /// The + /// The + private static DependencyObject GetParent(DependencyObject element, bool recurseIntoPopup) + { + if (recurseIntoPopup) + { + // Case 126732 : To correctly detect parent of a popup we must do that exception case + + if (element is Popup popup && popup.PlacementTarget != null) + { + return popup.PlacementTarget; + } + } + + DependencyObject parent = element is not Visual visual ? null : VisualTreeHelper.GetParent(visual); + + if (parent == null) + { + // No Visual parent. Check in the logical tree. + + if (element is FrameworkElement fe) + { + parent = fe.Parent; + + if (parent == null) + { + parent = fe.TemplatedParent; + } + } + else + { + if (element is FrameworkContentElement fce) + { + parent = fce.Parent; + + if (parent == null) + { + parent = fce.TemplatedParent; + } + } + } + } + + return parent; + } + /// /// Finds a Child of a given item in the visual tree. /// /// The type of the queried item. /// A direct parent of the queried item. - /// x:Name or Name of child. - /// The - public static T FindChild(DependencyObject parent, string childName) - where T : DependencyObject + /// x:Name or Name of child. + /// The + public static T FindChild(DependencyObject parent, string childName) where T : DependencyObject { - // Confirm parent and childName are valid. - if (parent == null) return null; + // Confirm parent and childName are valid. + if (parent == null) + { + return null; + } T foundChild = null; - var childrenCount = VisualTreeHelper.GetChildrenCount(parent); + int childrenCount = VisualTreeHelper.GetChildrenCount(parent); for (var i = 0; i < childrenCount; i++) { - var child = VisualTreeHelper.GetChild(parent, i); + DependencyObject child = VisualTreeHelper.GetChild(parent, i); // If the child is not of the request child type child - var childType = child as T; - if (childType == null) + if (child is not T childType) { // recursively drill down the tree foundChild = FindChild(child, childName); - // If the child is found, break so we do not overwrite the found child. - if (foundChild != null) break; + // If the child is found, break so we do not overwrite the found child. + if (foundChild != null) + { + break; + } } else if (!string.IsNullOrEmpty(childName)) { - var frameworkElement = child as FrameworkElement; // If the child's name is set for search - if (frameworkElement != null && frameworkElement.Name == childName) + if (child is FrameworkElement frameworkElement && frameworkElement.Name == childName) { // if the child's name is of the request name - foundChild = (T) child; + foundChild = (T)child; break; } } else { // child element found. - foundChild = (T) child; + foundChild = (T)child; break; } } @@ -77,23 +130,25 @@ public static T FindChild(DependencyObject parent, string childName) /// /// This will search for a child of the specified type. The search is performed - /// hierarchically, breadth first (as opposed to depth first). + /// hierarchically, breadth first (as opposed to depth first). /// /// The type of the element to find - /// The root of the tree to search for. This element itself is not checked. - /// The additionalCheck + /// + /// The root of the tree to search for. This element itself is not checked. + /// + /// The additionalCheck /// Returns the found element. Null if nothing is found. - public static T FindChild(this DependencyObject parent, Func additionalCheck = null) - where T : DependencyObject + public static T FindChild(this DependencyObject parent, Func additionalCheck = null) where T : DependencyObject { - var childrenCount = VisualTreeHelper.GetChildrenCount(parent); - T child; + int childrenCount = VisualTreeHelper.GetChildrenCount(parent); + T child; for (var index = 0; index < childrenCount; index++) { child = VisualTreeHelper.GetChild(parent, index) as T; if (child != null) + { if (additionalCheck == null) { return child; @@ -101,8 +156,11 @@ public static T FindChild(this DependencyObject parent, Func additio else { if (additionalCheck(child)) + { return child; + } } + } } for (var index = 0; index < childrenCount; index++) @@ -110,86 +168,99 @@ public static T FindChild(this DependencyObject parent, Func additio child = FindChild(VisualTreeHelper.GetChild(parent, index), additionalCheck); if (child != null) + { return child; + } } return null; } /// - /// 根据控件名称,查找子控件 - /// elementName为空时,查找指定类型的子控件 + /// 根据控件名称,查找子控件 elementName为空时,查找指定类型的子控件 /// /// - /// The obj - /// The elementName - /// The - public static T FindChildByName(this DependencyObject obj, string elementName) - where T : FrameworkElement + /// The obj + /// The elementName + /// The + public static T FindChildByName(this DependencyObject obj, string elementName) where T : FrameworkElement { DependencyObject child = null; for (var i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++) { child = VisualTreeHelper.GetChild(obj, i); - if (child is T && ((T) child).Name == elementName || string.IsNullOrEmpty(elementName)) - return (T) child; + if ((child is T && ((T)child).Name == elementName) || string.IsNullOrEmpty(elementName)) + { + return (T)child; + } var grandChild = FindChildByName(child, elementName); - if (grandChild != null) return grandChild; + if (grandChild != null) + { + return grandChild; + } } return null; } /// - /// Analyzes both visual and logical tree in order to find all elements of a given - /// type that are descendants of the item. + /// Analyzes both visual and logical tree in order to find all elements of a given type that + /// are descendants of the item. /// /// The type of the queried items. - /// The source - /// Sometimes it's better to search in the VisualTree (e.g. in tests) + /// The source + /// + /// Sometimes it's better to search in the VisualTree (e.g. in tests) + /// /// All descendants of that match the requested type. - public static IEnumerable FindChildren(DependencyObject source, - bool forceUsingTheVisualTreeHelper = false) where T : DependencyObject + public static IEnumerable FindChildren(DependencyObject source, bool forceUsingTheVisualTreeHelper = false) where T : DependencyObject { if (source != null) { var childs = GetChildObjects(source, forceUsingTheVisualTreeHelper); - foreach (var child in childs) + foreach (DependencyObject child in childs) { //analyze if children match the requested type - if (child != null && child is T) yield return (T) child; + if (child != null && child is T) + { + yield return (T)child; + } //recurse tree - foreach (var descendant in FindChildren(child)) yield return descendant; + foreach (T descendant in FindChildren(child)) + { + yield return descendant; + } } } } /// - /// 根据控件名称,查找子控件集合 - /// elementName为空时,查找指定类型的所有子控件 + /// 根据控件名称,查找子控件集合 elementName为空时,查找指定类型的所有子控件 /// /// - /// The obj - /// The elementName - /// The - public static List FindChildsByName(this DependencyObject obj, string elementName) - where T : FrameworkElement + /// The obj + /// The elementName + /// The + public static List FindChildsByName(this DependencyObject obj, string elementName) where T : FrameworkElement { - DependencyObject child = null; - var childList = new List(); + DependencyObject child = null; + var childList = new List(); for (var i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++) { child = VisualTreeHelper.GetChild(obj, i); - if (child is T && ((T) child).Name == elementName || string.IsNullOrEmpty(elementName)) + if ((child is T && ((T)child).Name == elementName) || string.IsNullOrEmpty(elementName)) { - childList.Add((T) child); + childList.Add((T)child); } else { var grandChildList = FindChildsByName(child, elementName); - if (grandChildList != null) childList.AddRange(grandChildList); + if (grandChildList != null) + { + childList.AddRange(grandChildList); + } } } @@ -200,7 +271,9 @@ public static List FindChildsByName(this DependencyObject obj, string elem /// This will search for a parent of the specified type. /// /// The type of the element to find - /// The node where the search begins. This element is not checked. + /// + /// The node where the search begins. This element is not checked. + /// /// Returns the found element. Null if nothing is found. public static T FindParent(DependencyObject startingObject) where T : DependencyObject { @@ -214,8 +287,7 @@ public static T FindParent(DependencyObject startingObject) where T : Depende /// The node where the search begins. /// Should the specified startingObject be checked first. /// Returns the found element. Null if nothing is found. - public static T FindParent(DependencyObject startingObject, bool checkStartingObject) - where T : DependencyObject + public static T FindParent(DependencyObject startingObject, bool checkStartingObject) where T : DependencyObject { return FindParent(startingObject, checkStartingObject, null); } @@ -226,19 +298,19 @@ public static T FindParent(DependencyObject startingObject, bool checkStartin /// The type of the element to find /// The node where the search begins. /// Should the specified startingObject be checked first. - /// The additionalCheck + /// The additionalCheck /// Returns the found element. Null if nothing is found. - public static T FindParent(DependencyObject startingObject, bool checkStartingObject, - Func additionalCheck) where T : DependencyObject + public static T FindParent(DependencyObject startingObject, bool checkStartingObject, Func additionalCheck) where T : DependencyObject { - T foundElement; - var parent = checkStartingObject ? startingObject : GetParent(startingObject, true); + T foundElement; + DependencyObject parent = checkStartingObject ? startingObject : GetParent(startingObject, true); while (parent != null) { foundElement = parent as T; if (foundElement != null) + { if (additionalCheck == null) { return foundElement; @@ -246,8 +318,11 @@ public static T FindParent(DependencyObject startingObject, bool checkStartin else { if (additionalCheck(foundElement)) + { return foundElement; + } } + } parent = GetParent(parent, true); } @@ -256,21 +331,22 @@ public static T FindParent(DependencyObject startingObject, bool checkStartin } /// - /// 根据控件名称,查找父控件 - /// elementName为空时,查找指定类型的父控件 + /// 根据控件名称,查找父控件 elementName为空时,查找指定类型的父控件 /// /// - /// The obj - /// The elementName - /// The - public static T FindParentByName(this DependencyObject obj, string elementName) - where T : FrameworkElement + /// The obj + /// The elementName + /// The + public static T FindParentByName(this DependencyObject obj, string elementName) where T : FrameworkElement { - var parent = VisualTreeHelper.GetParent(obj); + DependencyObject parent = VisualTreeHelper.GetParent(obj); while (parent != null) { - if (parent is T && (((T) parent).Name == elementName || string.IsNullOrEmpty(elementName))) - return (T) parent; + if (parent is T && (((T)parent).Name == elementName || string.IsNullOrEmpty(elementName))) + { + return (T)parent; + } + parent = VisualTreeHelper.GetParent(parent); } @@ -286,15 +362,22 @@ public static T FindParentByName(this DependencyObject obj, string elementNam public static T FindVisualChildItem(this DependencyObject obj) where T : DependencyObject { if (null != obj) + { for (var i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++) { - var child = VisualTreeHelper.GetChild(obj, i); - if (child != null && child is T) return (T) child; + DependencyObject child = VisualTreeHelper.GetChild(obj, i); + if (child != null && child is T) + { + return (T)child; + } var childOfChild = FindVisualChildItem(child); if (childOfChild != null) + { return childOfChild; + } } + } return null; } @@ -309,91 +392,125 @@ public static T FindVisualChildItem(this DependencyObject obj) where T : Depe public static T FindVisualChildItem(this DependencyObject obj, string name) where T : FrameworkElement { if (null != obj) + { for (var i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++) { - var child = VisualTreeHelper.GetChild(obj, i); - if (child != null && child is T && (child as T).Name.Equals(name)) return (T) child; + DependencyObject child = VisualTreeHelper.GetChild(obj, i); + if (child != null && child is T && (child as T).Name.Equals(name)) + { + return (T)child; + } var childOfChild = FindVisualChildItem(child, name); if (childOfChild != null && childOfChild is T && childOfChild.Name.Equals(name)) + { return childOfChild; + } } + } return null; } /// - /// This method is an alternative to WPF's - /// method, which also - /// supports content elements. Keep in mind that for content elements, - /// this method falls back to the logical tree of the element. + /// This method is an alternative to WPF's method, + /// which also supports content elements. Keep in mind that for content elements, this + /// method falls back to the logical tree of the element. /// /// The item to be processed. - /// Sometimes it's better to search in the VisualTree (e.g. in tests) + /// + /// Sometimes it's better to search in the VisualTree (e.g. in tests) + /// /// The submitted item's child elements, if available. - public static IEnumerable GetChildObjects(DependencyObject parent, - bool forceUsingTheVisualTreeHelper = false) + public static IEnumerable GetChildObjects(DependencyObject parent, bool forceUsingTheVisualTreeHelper = false) { - if (parent == null) yield break; + if (parent == null) + { + yield break; + } if (!forceUsingTheVisualTreeHelper && (parent is ContentElement || parent is FrameworkElement)) { //use the logical tree for content / framework elements - foreach (var obj in LogicalTreeHelper.GetChildren(parent)) + foreach (object obj in LogicalTreeHelper.GetChildren(parent)) { - var depObj = obj as DependencyObject; - if (depObj != null) yield return (DependencyObject) obj; + if (obj is DependencyObject depObj) + { + yield return (DependencyObject)obj; + } } } else { //use the visual tree per default - var count = VisualTreeHelper.GetChildrenCount(parent); - for (var i = 0; i < count; i++) yield return VisualTreeHelper.GetChild(parent, i); + int count = VisualTreeHelper.GetChildrenCount(parent); + for (var i = 0; i < count; i++) + { + yield return VisualTreeHelper.GetChild(parent, i); + } + } + } + + /// + /// 返回从叶节点开始的完整逻辑祖先链。 + /// + /// 叶节点 + /// 返回一个包含从叶节点开始的所有逻辑祖先的迭代器 + public static IEnumerable GetLogicalAncestry(this DependencyObject leaf) + { + while (leaf != null) + { + yield return leaf; + leaf = LogicalTreeHelper.GetParent(leaf); } } /// - /// Tries its best to return the specified element's parent. It will - /// try to find, in this order, the VisualParent, LogicalParent, LogicalTemplatedParent. - /// It only works for Visual, FrameworkElement or FrameworkContentElement. + /// Tries its best to return the specified element's parent. It will try to find, in this + /// order, the VisualParent, LogicalParent, LogicalTemplatedParent. It only works for + /// Visual, FrameworkElement or FrameworkContentElement. /// - /// The element - /// The + /// The element + /// The public static DependencyObject GetParent(DependencyObject element) { return GetParent(element, true); } /// - /// This method is an alternative to WPF's - /// method, which also - /// supports content elements. Keep in mind that for content element, - /// this method falls back to the logical tree of the element! + /// This method is an alternative to WPF's method, + /// which also supports content elements. Keep in mind that for content element, this method + /// falls back to the logical tree of the element! /// /// The item to be processed. - /// The + /// The public static DependencyObject GetParentObject(this DependencyObject child) { - if (child == null) return null; + if (child == null) + { + return null; + } //handle content elements separately - var contentElement = child as ContentElement; - if (contentElement != null) + if (child is ContentElement contentElement) { - var parent = ContentOperations.GetParent(contentElement); - if (parent != null) return parent; + DependencyObject parent = ContentOperations.GetParent(contentElement); + if (parent != null) + { + return parent; + } - var fce = contentElement as FrameworkContentElement; - return fce != null ? fce.Parent : null; + return contentElement is FrameworkContentElement fce ? fce.Parent : null; } //also try searching for parent in framework elements (such as DockPanel, etc) - var frameworkElement = child as FrameworkElement; - if (frameworkElement != null) + if (child is FrameworkElement frameworkElement) { - var parent = frameworkElement.Parent; - if (parent != null) return parent; + DependencyObject parent = frameworkElement.Parent; + if (parent != null) + { + return parent; + } } //if it's not a ContentElement/FrameworkElement, rely on VisualTreeHelper @@ -401,31 +518,48 @@ public static DependencyObject GetParentObject(this DependencyObject child) } /// - /// Returns true if the specified element is a child of parent somewhere in the visual - /// tree. This method will work for Visual, FrameworkElement and FrameworkContentElement. + /// 返回从叶节点开始的完整视觉祖先链。 + /// 如果元素不是 , 则使用逻辑祖先链。 + /// + /// 叶节点 + /// 返回一个包含从叶节点开始的所有祖先的迭代器 + public static IEnumerable GetVisualAncestry(this DependencyObject leaf) + { + while (leaf != null) + { + yield return leaf; + leaf = leaf is Visual || leaf is Visual3D ? VisualTreeHelper.GetParent(leaf) : LogicalTreeHelper.GetParent(leaf); + } + } + + /// + /// Returns true if the specified element is a child of parent somewhere in the visual tree. + /// This method will work for Visual, FrameworkElement and FrameworkContentElement. /// /// The element that is potentially a child of the specified parent. /// The element that is potentially a parent of the specified element. - /// The + /// The public static bool IsDescendantOf(DependencyObject element, DependencyObject parent) { return IsDescendantOf(element, parent, true); } /// - /// Returns true if the specified element is a child of parent somewhere in the visual - /// tree. This method will work for Visual, FrameworkElement and FrameworkContentElement. + /// Returns true if the specified element is a child of parent somewhere in the visual tree. + /// This method will work for Visual, FrameworkElement and FrameworkContentElement. /// /// The element that is potentially a child of the specified parent. /// The element that is potentially a parent of the specified element. - /// The recurseIntoPopup - /// The + /// The recurseIntoPopup + /// The public static bool IsDescendantOf(DependencyObject element, DependencyObject parent, bool recurseIntoPopup) { while (element != null) { if (element == parent) + { return true; + } element = GetParent(element, recurseIntoPopup); } @@ -433,20 +567,44 @@ public static bool IsDescendantOf(DependencyObject element, DependencyObject par return false; } + /// + /// 判断一个DependencyObject是否是另一个DependencyObject的后代。 + /// + /// 可能是后代的DependencyObject + /// 可能是祖先的DependencyObject + /// 如果leaf是ancestor的后代,则返回true,否则返回false + public static bool IsDescendantOfDependencyObject(this DependencyObject leaf, DependencyObject ancestor) + { + DependencyObject parent = null; + foreach (DependencyObject node in leaf.GetVisualAncestry()) + { + if (Equals(node, ancestor)) + { + return true; + } + + parent = node; + } + + return parent?.GetLogicalAncestry().Contains(ancestor) == true; + } + /// /// Finds a parent of a given item on the visual tree. /// /// The type of the queried item. - /// The child - /// The - public static T TryFindParent(this DependencyObject child) - where T : DependencyObject + /// The child + /// The + public static T TryFindParent(this DependencyObject child) where T : DependencyObject { //get parent item - var parentObject = GetParentObject(child); + DependencyObject parentObject = GetParentObject(child); //we've reached the end of the tree - if (parentObject == null) return null; + if (parentObject == null) + { + return null; + } //check if the parent matches the type we're looking for var parent = parentObject as T; @@ -454,52 +612,30 @@ public static T TryFindParent(this DependencyObject child) } /// - /// The GetParent + /// 提供了一种以深度优先的方式遍历DependencyObject的视觉树的方法。 /// - /// The - /// The - /// The - private static DependencyObject GetParent(DependencyObject element, bool recurseIntoPopup) + /// 需要遍历的DependencyObject节点。 + /// 返回一个包含视觉树中所有DependencyObject的迭代器。 + /// 当提供的DependencyObject为null时,抛出此异常。 + public static IEnumerable VisualDepthFirstTraversal(this DependencyObject node) { - if (recurseIntoPopup) + if (node == null) { - // Case 126732 : To correctly detect parent of a popup we must do that exception case - var popup = element as Popup; - - if (popup != null && popup.PlacementTarget != null) - return popup.PlacementTarget; + throw new ArgumentNullException(nameof(node)); } - var visual = element as Visual; - var parent = visual == null ? null : VisualTreeHelper.GetParent(visual); + yield return node; - if (parent == null) + for (var i = 0; i < VisualTreeHelper.GetChildrenCount(node); i++) { - // No Visual parent. Check in the logical tree. - var fe = element as FrameworkElement; - - if (fe != null) + DependencyObject child = VisualTreeHelper.GetChild(node, i); + foreach (DependencyObject descendant in child.VisualDepthFirstTraversal()) { - parent = fe.Parent; - - if (parent == null) parent = fe.TemplatedParent; - } - else - { - var fce = element as FrameworkContentElement; - - if (fce != null) - { - parent = fce.Parent; - - if (parent == null) parent = fce.TemplatedParent; - } + yield return descendant; } } - - return parent; } - #endregion + #endregion Methods } } \ No newline at end of file From 0b99ceb91036392fb84a67e53f42f15aa541c2b2 Mon Sep 17 00:00:00 2001 From: yangzheng06 Date: Fri, 8 Mar 2024 20:22:03 +0800 Subject: [PATCH 7/8] =?UTF-8?q?Added=20EnumDescriptionTypeConverter,update?= =?UTF-8?q?=20EnumBindingSourceExtension=E3=80=81EnumExtensions=20and=20Ta?= =?UTF-8?q?skExtensions.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../EnumBindingSourceExtension.cs | 122 ++++++++++----- .../EnumDescriptionTypeConverter.cs | 59 +++++++ .../Extensions/EnumExtensions.cs | 25 +-- .../Extensions/TaskExtensions.cs | 146 ++++++++++-------- 4 files changed, 235 insertions(+), 117 deletions(-) create mode 100644 Source/Wif.Infrastructure/MarkupExtensions/EnumDescriptionTypeConverter.cs diff --git a/Source/Wif.Infrastructure/MarkupExtensions/EnumBindingSourceExtension.cs b/Source/Wif.Infrastructure/MarkupExtensions/EnumBindingSourceExtension.cs index 473ae7f..24075cb 100644 --- a/Source/Wif.Infrastructure/MarkupExtensions/EnumBindingSourceExtension.cs +++ b/Source/Wif.Infrastructure/MarkupExtensions/EnumBindingSourceExtension.cs @@ -9,90 +9,136 @@ using System; +using System.Linq; +using System.Reflection; +using System.Runtime.CompilerServices; using System.Windows.Markup; namespace Frontier.Wif.Infrastructure.MarkupExtensions { /// - /// Markup extension for Enum values. - /// https://brianlagunas.com/a-better-way-to-data-bind-enums-in-wpf/ + /// 枚举值绑定源的标记扩展。参考 https://brianlagunas.com/a-better-way-to-data-bind-enums-in-wpf/ /// - public class EnumBindingSourceExtension - : MarkupExtension + public class EnumBindingSourceExtension : MarkupExtension { - #region Fields - /// - /// Defines the _enumType + /// 定义枚举类型字段。 /// private Type _enumType; - #endregion + /// + /// Initializes a new instance of the class. + /// + public EnumBindingSourceExtension() + { + } + + /// + /// Initializes a new instance of the class. + /// + /// 枚举类型。 + public EnumBindingSourceExtension(Type enumType) + { + EnumType = enumType; + } - #region Properties + /// + /// Initializes a new instance of the class. + /// + /// 枚举类型。 + /// 枚举成员是否排序。 + public EnumBindingSourceExtension(Type enumType, bool isSorted) + { + EnumType = enumType; + IsSorted = isSorted; + } /// - /// Gets or sets the type of the Enum. + /// 获取或设置枚举类型。 /// public Type EnumType { - get => _enumType; + get { return _enumType; } set { if (value != EnumType) { if (!Equals(value, null) && !(Nullable.GetUnderlyingType(value) ?? value).IsEnum) + { throw new ArgumentException("Type must be an Enum."); + } _enumType = value; } } } - #endregion - - #region Constructors - /// - /// Initializes a new instance of the class. + /// 获取或设置是否排序,开启后获取EnumOrderAttribute值,若Attribute未设置,按照成员定义顺序排序。 /// - public EnumBindingSourceExtension() - { - } + public bool IsSorted { get; set; } /// - /// Initializes a new instance of the class. + /// 返回用作此标记扩展的目标属性值的对象。 /// - /// The type of the Enum. - public EnumBindingSourceExtension(Type enumType) - { - EnumType = enumType; - } - - #endregion - - #region Methods - - /// - /// The ProvideValue - /// - /// Object that can provide services for the markup extension. - /// The values of the Enum. + /// 为标记扩展提供服务的对象。 + /// 枚举的所有值。 public override object ProvideValue(IServiceProvider serviceProvider) { if (Equals(EnumType, null)) + { throw new InvalidOperationException("The type of the Enum is undefined."); + } + + Type underlyingEnumType = Nullable.GetUnderlyingType(EnumType) ?? EnumType; + + Array enumValues; + if (IsSorted) + { + enumValues = underlyingEnumType.GetFields().Where(field => field.IsStatic).Select(field => new + { + field, + attribute = field.GetCustomAttribute() + }).Select(fieldInfo => new + { + value = fieldInfo.field.GetValue(fieldInfo), + order = fieldInfo.attribute?.Order ?? 0 + }).OrderBy(field => field.order).Select(field => field.value).ToArray(); + } + else + { + enumValues = Enum.GetValues(underlyingEnumType); + } - var underlyingEnumType = Nullable.GetUnderlyingType(EnumType) ?? EnumType; - var enumValues = Enum.GetValues(underlyingEnumType); if (underlyingEnumType == EnumType) + { return enumValues; + } var nullableEnumValues = Array.CreateInstance(underlyingEnumType, enumValues.Length); enumValues.CopyTo(nullableEnumValues, 1); return nullableEnumValues; } + } - #endregion + /// + /// 表示在EnumBindingExtension中定义枚举成员顺序的特性。 + /// + [AttributeUsage(AttributeTargets.All, Inherited = false)] + public sealed class EnumOrderAttribute : Attribute + { + /// + /// 有参构造函数。 + /// + /// 枚举成员序号。 + public EnumOrderAttribute([CallerLineNumber] int order = 0) + { + Order = order; + } + + /// + /// 获取枚举成员序号。 + /// + public int Order { get; } } } \ No newline at end of file diff --git a/Source/Wif.Infrastructure/MarkupExtensions/EnumDescriptionTypeConverter.cs b/Source/Wif.Infrastructure/MarkupExtensions/EnumDescriptionTypeConverter.cs new file mode 100644 index 0000000..dd03541 --- /dev/null +++ b/Source/Wif.Infrastructure/MarkupExtensions/EnumDescriptionTypeConverter.cs @@ -0,0 +1,59 @@ +/************************************************************************** +* File Name:EnumBindingSourceExtension.cs +* Description:EnumBindingSourceExtension.cs class description... +* Copyright:Copyright © 2020 LeoYang-Chuese. All rights reserved. +* Creator:Leo Yang +* Create Time:2024/03/08 +*Project Address:https://github.com/LeoYang-Chuese/wif +**************************************************************************/ + + +using System; +using System.ComponentModel; +using System.Globalization; +using System.Reflection; + +namespace Frontier.Wif.Infrastructure.MarkupExtensions +{ + /// + /// 表示枚举值转描述值的类型转换器。 + /// + public class EnumDescriptionTypeConverter : EnumConverter + { + /// + /// + /// + /// + public EnumDescriptionTypeConverter(Type type) : base(type) + { + } + + /// + /// + /// + /// + /// + /// + /// + /// + public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) + { + if (destinationType == typeof(string)) + { + if (value != null) + { + FieldInfo fi = value.GetType().GetField(value.ToString()); + if (fi != null) + { + var attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false); + return attributes.Length > 0 && !string.IsNullOrEmpty(attributes[0].Description) ? attributes[0].Description : value.ToString(); + } + } + + return string.Empty; + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } +} \ No newline at end of file diff --git a/Source/Wif.Utilities/Extensions/EnumExtensions.cs b/Source/Wif.Utilities/Extensions/EnumExtensions.cs index 08156c3..dcc60da 100644 --- a/Source/Wif.Utilities/Extensions/EnumExtensions.cs +++ b/Source/Wif.Utilities/Extensions/EnumExtensions.cs @@ -42,21 +42,20 @@ public static string GetDescription(this Enum enumSubitem) { try { - var strValue = enumSubitem.ToString(); - var fieldInfo = enumSubitem.GetType().GetField(strValue); - var attribute = fieldInfo.GetCustomAttribute(false); - if (attribute != null) + if (enumSubitem == null) { - var da = attribute; - return da.Description; + return string.Empty; } + + var strValue = enumSubitem.ToString(); + FieldInfo fieldInfo = enumSubitem.GetType().GetField(strValue); + var attribute = fieldInfo.GetCustomAttribute(false); + return attribute != null ? attribute.Description : enumSubitem.ToString(); } catch (Exception ex) { - throw new Exception("分解对象值出错", ex); + throw new Exception($"分解对象{enumSubitem}值出错:{ex.Message}", ex); } - - return "未知属性名称"; } /// @@ -171,10 +170,14 @@ public static T ToEnum(this object value) where T : struct, IConvertible public static T ToEnum(this string value) where T : struct, IConvertible { if (!Enum.TryParse(value, true, out T result)) - return default; + { + return default(T); + } if (!Enum.IsDefined(typeof(T), result)) - return default; + { + return default(T); + } return result; } diff --git a/Source/Wif.Utilities/Extensions/TaskExtensions.cs b/Source/Wif.Utilities/Extensions/TaskExtensions.cs index 5dac196..dcf4645 100644 --- a/Source/Wif.Utilities/Extensions/TaskExtensions.cs +++ b/Source/Wif.Utilities/Extensions/TaskExtensions.cs @@ -1,12 +1,11 @@ /************************************************************************** -* File Name:TaskExtensions.cs -* Description:TaskExtensions.cs class description... -* Copyright:Copyright © 2020 LeoYang-Chuese. All rights reserved. -* Creator:Leo Yang -* Create Time:2020/12/15 -*Project Address:https://github.com/LeoYang-Chuese/wif -**************************************************************************/ - + * File Name:TaskExtensions.cs + * Description:TaskExtensions.cs class description... + * Copyright:Copyright © 2020 LeoYang-Chuese. All rights reserved. + * Creator:Leo Yang + * Create Time:2020/12/15 + *Project Address:https://github.com/LeoYang-Chuese/wif + **************************************************************************/ using System; using System.Threading.Tasks; @@ -18,7 +17,33 @@ namespace Frontier.Wif.Utilities.Extensions /// public static class TaskExtensions { - #region Methods + /// + /// 支持工作任务和同步执行完成委托的扩展。 + /// + /// 工作任务。 + /// 工作任务结束后的完成方法。 + /// 完成方法是否在UI线程执行,默认在UI线程执行。 + /// The + public static Task ContinueWithRun(this Task workTask, Action completedAction, bool runCompletedActionInUIThread = true) + { + TaskScheduler taskScheduler = runCompletedActionInUIThread ? TaskScheduler.FromCurrentSynchronizationContext() : TaskScheduler.Current; + + return workTask.ContinueWith(unusedTask => completedAction(), taskScheduler); + } + + /// + /// 支持工作任务和同步执行完成委托的扩展。 + /// + /// 工作任务。 + /// 工作任务结束后的完成方法。 + /// 完成方法是否在UI线程执行,默认在UI线程执行。 + /// The + public static Task ContinueWithRun(this Task workTask, Action completedAction, bool runCompletedActionInUIThread = true) + { + TaskScheduler taskScheduler = runCompletedActionInUIThread ? TaskScheduler.FromCurrentSynchronizationContext() : TaskScheduler.Current; + + return workTask.ContinueWith(unusedTask => completedAction(unusedTask.Result), taskScheduler); + } /// /// 延时异步执行方法。 @@ -33,16 +58,13 @@ public static void DelayRun(this Action action, int delayTime) /// /// 支持异步执行工作委托和同步执行完成委托的扩展。 /// - /// The action - /// The completedAction - /// The runCompletedActionInUIThread - /// The - public static Task RunAsync(this Action workAction, Action completedAction, - bool runCompletedActionInUIThread = true) + /// The action + /// The completedAction + /// The runCompletedActionInUIThread + /// The + public static Task RunAsync(this Action workAction, Action completedAction, bool runCompletedActionInUIThread = true) { - var taskScheduler = runCompletedActionInUIThread - ? TaskScheduler.FromCurrentSynchronizationContext() - : TaskScheduler.Current; + TaskScheduler taskScheduler = runCompletedActionInUIThread ? TaskScheduler.FromCurrentSynchronizationContext() : TaskScheduler.Current; return Task.Run(workAction).ContinueWith(unusedTask => completedAction(), taskScheduler); } @@ -51,16 +73,13 @@ public static Task RunAsync(this Action workAction, Action completedAction, /// 支持异步执行工作委托和同步执行完成委托的扩展。 /// /// - /// The action - /// The completedFunction - /// The runCompletedActionInUIThread - /// The - public static Task RunAsync(this Action workAction, Func completedFunction, - bool runCompletedActionInUIThread = true) + /// The action + /// The completedFunction + /// The runCompletedActionInUIThread + /// The + public static Task RunAsync(this Action workAction, Func completedFunction, bool runCompletedActionInUIThread = true) { - var taskScheduler = runCompletedActionInUIThread - ? TaskScheduler.FromCurrentSynchronizationContext() - : TaskScheduler.Current; + TaskScheduler taskScheduler = runCompletedActionInUIThread ? TaskScheduler.FromCurrentSynchronizationContext() : TaskScheduler.Current; return Task.Run(workAction).ContinueWith(unusedTask => completedFunction(), taskScheduler); } @@ -69,16 +88,13 @@ public static Task RunAsync(this Action workAction, Func /// - /// The function - /// The completedAction - /// The runCompletedActionInUIThread - /// The - public static Task RunAsync(this Func workFunction, Action completedAction, - bool runCompletedActionInUIThread = true) + /// The function + /// The completedAction + /// The runCompletedActionInUIThread + /// The + public static Task RunAsync(this Func workFunction, Action completedAction, bool runCompletedActionInUIThread = true) { - var taskScheduler = runCompletedActionInUIThread - ? TaskScheduler.FromCurrentSynchronizationContext() - : TaskScheduler.Current; + TaskScheduler taskScheduler = runCompletedActionInUIThread ? TaskScheduler.FromCurrentSynchronizationContext() : TaskScheduler.Current; var task = Task.Run(workFunction); task.ContinueWith(unusedTask => completedAction(task.Result), taskScheduler); @@ -90,17 +106,13 @@ public static Task RunAsync(this Func workFunction, A /// /// /// - /// The workFunction - /// The completedAction - /// The runCompletedActionInUIThread - /// The - public static Task RunAsync(this Func workFunction, - Func completedAction, - bool runCompletedActionInUIThread = true) + /// The workFunction + /// The completedAction + /// The runCompletedActionInUIThread + /// The + public static Task RunAsync(this Func workFunction, Func completedAction, bool runCompletedActionInUIThread = true) { - var taskScheduler = runCompletedActionInUIThread - ? TaskScheduler.FromCurrentSynchronizationContext() - : TaskScheduler.Current; + TaskScheduler taskScheduler = runCompletedActionInUIThread ? TaskScheduler.FromCurrentSynchronizationContext() : TaskScheduler.Current; return Task.Run(workFunction).ContinueWith(workTask => completedAction(workTask.Result), taskScheduler); } @@ -108,39 +120,37 @@ public static Task RunAsync(this Func /// 支持异步执行工作委托和同步执行完成委托的扩展。 /// - /// The workTask - /// The completedAction - /// The runCompletedActionInUIThread - /// The - public static Task RunAsync(this Task workTask, Action completedAction, - bool runCompletedActionInUIThread = true) + /// The workTask + /// The completedAction + /// The runCompletedActionInUIThread + /// The + public static Task RunAsync(this Task workTask, Action completedAction, bool runCompletedActionInUIThread = true) { - var taskScheduler = runCompletedActionInUIThread - ? TaskScheduler.FromCurrentSynchronizationContext() - : TaskScheduler.Current; + TaskScheduler taskScheduler = runCompletedActionInUIThread ? TaskScheduler.FromCurrentSynchronizationContext() : TaskScheduler.Current; if (workTask.Status == TaskStatus.Created) + { workTask.Start(); + } return workTask.ContinueWith(unusedTask => completedAction(), taskScheduler); } /// - /// 支持工作任务和同步执行完成委托的扩展。 + /// 支持异步执行工作委托和同步执行完成委托的扩展。 /// - /// The action - /// The completedAction - /// The runCompletedActionInUIThread - /// The - public static Task ContinueWithRun(this Task workTask, Action completedAction, - bool runCompletedActionInUIThread = true) + /// 工作任务。 + /// 工作方法结束后的完成方法。 + /// 完成方法是否在UI线程执行,默认在UI线程执行。 + /// The + public static Task RunAsync(this Task workTask, Action completedAction, bool runCompletedActionInUIThread = true) { - var taskScheduler = runCompletedActionInUIThread - ? TaskScheduler.FromCurrentSynchronizationContext() - : TaskScheduler.Current; + TaskScheduler taskScheduler = runCompletedActionInUIThread ? TaskScheduler.FromCurrentSynchronizationContext() : TaskScheduler.Current; + if (workTask.Status == TaskStatus.Created) + { + workTask.Start(); + } - return workTask.ContinueWith(unusedTask => completedAction(), taskScheduler); + return workTask.ContinueWith(unusedTask => completedAction(workTask.Result), taskScheduler); } - - #endregion } } \ No newline at end of file From 49b2317fa301674c63da0284952f840fb660d8e6 Mon Sep 17 00:00:00 2001 From: yangzheng Date: Wed, 10 Apr 2024 18:58:55 +0800 Subject: [PATCH 8/8] Update version to 0.4.0.24100. --- Source/Wif.Core/Wif.Core.csproj | 2 +- Source/Wif.Infrastructure/Wif.Infrastructure.csproj | 2 +- Source/Wif.Utilities/Wif.Utilities.csproj | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Source/Wif.Core/Wif.Core.csproj b/Source/Wif.Core/Wif.Core.csproj index 0ea626b..699c08d 100644 --- a/Source/Wif.Core/Wif.Core.csproj +++ b/Source/Wif.Core/Wif.Core.csproj @@ -3,7 +3,7 @@ net462;net472;net6.0-windows;net8.0-windows true Frontier.Wif.Core - 0.3.0.21349 + 0.4.0.24100 LeoYang-Chuese WPF Infrastructure Framework(WIF)is an infrastructure framework that helps you develop WPF applications quickly. Copyright © 2021 LeoYang-Chuese. All rights reserved. diff --git a/Source/Wif.Infrastructure/Wif.Infrastructure.csproj b/Source/Wif.Infrastructure/Wif.Infrastructure.csproj index 19ff108..ad9c71b 100644 --- a/Source/Wif.Infrastructure/Wif.Infrastructure.csproj +++ b/Source/Wif.Infrastructure/Wif.Infrastructure.csproj @@ -3,7 +3,7 @@ net462;net472;net6.0-windows;net8.0-windows true Frontier.Wif.Infrastructure - 0.3.0.21349 + 0.4.0.24100 WPF Infrastructure Framework(WIF)is an infrastructure framework that helps you develop WPF applications quickly. LeoYang-Chuese Copyright © 2021 LeoYang-Chuese. All rights reserved. diff --git a/Source/Wif.Utilities/Wif.Utilities.csproj b/Source/Wif.Utilities/Wif.Utilities.csproj index 5ab2831..390c487 100644 --- a/Source/Wif.Utilities/Wif.Utilities.csproj +++ b/Source/Wif.Utilities/Wif.Utilities.csproj @@ -4,7 +4,7 @@ true true Frontier.Wif.Utilities - 0.3.0.21349 + 0.4.0.24100 LeoYang-Chuese WPF Infrastructure Framework(WIF)is an infrastructure framework that helps you develop WPF applications quickly. Copyright © 2021 LeoYang-Chuese. All rights reserved.