Преглед изворни кода

ref: 迁移 ValueConverter

龙腾猫跃 пре 5 месеци
родитељ
комит
9a77157dcf
3 измењених фајлова са 76 додато и 1 уклоњено
  1. 1 1
      Shared/General/Logger.cs
  2. 74 0
      Wpf/General/ValueConverters.cs
  3. 1 0
      Wpf/MeloongCore.Wpf.csproj

+ 1 - 1
Shared/General/Logger.cs

@@ -56,7 +56,7 @@ public enum LogBehavior {
 }
 
 public static class Logger {
-    internal static BaseLogger Instance { get; set; } = new();
+    public static BaseLogger Instance { get; set; } = new();
 
     // 转发给实例的方法包装
     public static void Log(string message, LogLevel level = LogLevel.Info, LogBehavior behavior = LogBehavior.None, [CallerFilePath] string filePath = "") 

+ 74 - 0
Wpf/General/ValueConverters.cs

@@ -0,0 +1,74 @@
+using System.Globalization;
+using System.Windows;
+using System.Windows.Data;
+
+namespace MeloongCore.Wpf;
+
+/// <summary>
+/// 对数据绑定进行加法运算,使用 <paramref name="parameter"/> 决定加数。
+/// </summary>
+public class AdditionConverter : IValueConverter {
+    public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture) {
+        if (value is null) return 0;
+        if (!double.TryParse(value.ToString(), out double before)) return 0;
+        double scale = 1;
+        if (parameter is not null) double.TryParse(parameter.ToString(), out scale);
+        return before + scale;
+    }
+    public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) {
+        if (value is null) return Binding.DoNothing;
+        if (!double.TryParse(value.ToString(), out double before)) return Binding.DoNothing;
+        double scale = 1;
+        if (parameter is not null) double.TryParse(parameter.ToString(), out scale);
+        return before - scale;
+    }
+}
+
+/// <summary>
+/// 对数据绑定进行乘法运算,使用 <paramref name="parameter"/> 决定乘数。
+/// </summary>
+public class MultiplicationConverter : IValueConverter {
+    public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture) {
+        if (value is null) return 0;
+        if (!double.TryParse(value.ToString(), out double before)) return 0;
+        double scale = 1;
+        if (parameter is not null) double.TryParse(parameter.ToString(), out scale);
+        return before * scale;
+    }
+    public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) {
+        if (value is null) return Binding.DoNothing;
+        if (!double.TryParse(value.ToString(), out double before)) return Binding.DoNothing;
+        double scale = 1;
+        if (parameter is not null) double.TryParse(parameter.ToString(), out scale);
+        if (scale == 0) return Binding.DoNothing;
+        return before / scale;
+    }
+}
+
+/// <summary>
+/// 将取反的 <see cref="bool"/> 绑定到 <see cref="Visibility"/>。
+/// </summary>
+public class InverseBooleanToVisibilityConverter : IValueConverter {
+    public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture) {
+        if (value is null) return Visibility.Visible;
+        return bool.TryParse(value.ToString(), out bool boolValue) ? (boolValue ? Visibility.Collapsed : Visibility.Visible) : Visibility.Visible;
+    }
+    public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) {
+        if (value is null) return false;
+        return value is Visibility vis && vis != Visibility.Visible;
+    }
+}
+
+/// <summary>
+/// 将 <see cref="bool"/> 取反。
+/// </summary>
+public class InverseBooleanConverter : IValueConverter {
+    public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture) {
+        if (value is null) return false;
+        return bool.TryParse(value.ToString(), out bool boolValue) ? !boolValue : false;
+    }
+    public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) {
+        if (value is null) return false;
+        return bool.TryParse(value.ToString(), out bool boolValue) ? !boolValue : false;
+    }
+}

+ 1 - 0
Wpf/MeloongCore.Wpf.csproj

@@ -38,6 +38,7 @@
 	</ItemGroup>
 
 	<ItemGroup>
+		<Reference Include="PresentationCore" />
 		<Reference Include="PresentationFramework" />
 	</ItemGroup>