Files
community/Ink Canvas/Windows/SettingsViews/MainWindowSettingsHelper.cs
T

584 lines
24 KiB
C#
Raw Normal View History

2026-01-02 01:35:22 +08:00
using System;
using System.Linq;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Media = System.Windows.Media;
2026-04-05 21:50:53 +08:00
namespace Ink_Canvas.Windows.SettingsViews
2026-01-02 01:35:22 +08:00
{
public static class MainWindowSettingsHelper
{
private static MainWindow GetMainWindow()
{
return Application.Current.MainWindow as MainWindow;
}
2026-01-10 17:31:55 +08:00
/// <summary>
/// 调用 MainWindow 中的方法
/// </summary>
2026-01-02 01:35:22 +08:00
public static void InvokeMainWindowMethod(string methodName, params object[] parameters)
{
try
{
var mainWindow = GetMainWindow();
if (mainWindow == null) return;
var method = mainWindow.GetType().GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
if (method != null)
{
method.Invoke(mainWindow, parameters);
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"调用 MainWindow 方法 {methodName} 失败: {ex.Message}");
}
}
2026-01-10 17:31:55 +08:00
/// <summary>
/// 调用 MainWindow 中的 ToggleSwitch 事件处理方法
/// </summary>
2026-01-02 01:35:22 +08:00
public static void InvokeToggleSwitchToggled(string toggleSwitchName, bool isOn)
{
try
{
var mainWindow = GetMainWindow();
if (mainWindow == null) return;
2026-01-10 17:31:55 +08:00
// 获取 MainWindow 中的 ToggleSwitch 控件
2026-01-02 01:35:22 +08:00
var toggleSwitch = mainWindow.FindName(toggleSwitchName);
if (toggleSwitch == null)
{
2026-01-10 17:31:55 +08:00
// 如果找不到控件,尝试通过反射设置属性
2026-01-02 01:35:22 +08:00
var property = mainWindow.GetType().GetProperty(toggleSwitchName, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
if (property != null && property.PropertyType.Name.Contains("ToggleSwitch"))
{
var isOnProperty = property.PropertyType.GetProperty("IsOn");
if (isOnProperty != null)
{
var toggleSwitchInstance = property.GetValue(mainWindow);
if (toggleSwitchInstance != null)
{
isOnProperty.SetValue(toggleSwitchInstance, isOn);
}
}
}
2026-01-10 17:32:14 +08:00
// 即使找不到控件,也尝试触发事件(可能通过反射调用)
2026-01-02 01:35:22 +08:00
var toggledMethodName = toggleSwitchName + "_Toggled";
2026-01-10 17:32:14 +08:00
InvokeMainWindowMethod(toggledMethodName, null, new RoutedEventArgs());
2026-03-03 16:04:20 +08:00
2026-01-10 17:31:55 +08:00
// 通知新设置面板同步状态
2026-01-02 01:35:22 +08:00
NotifySettingsPanelsSyncState(toggleSwitchName);
return;
}
2026-01-10 17:31:55 +08:00
// 设置 ToggleSwitch 的 IsOn 属性
2026-01-02 01:35:22 +08:00
var toggleSwitchType = toggleSwitch.GetType();
var isOnProp = toggleSwitchType.GetProperty("IsOn");
if (isOnProp != null)
{
isOnProp.SetValue(toggleSwitch, isOn);
}
2026-01-10 17:31:55 +08:00
// 触发 Toggled 事件
2026-01-02 01:35:22 +08:00
var toggledMethodName2 = toggleSwitchName + "_Toggled";
InvokeMainWindowMethod(toggledMethodName2, toggleSwitch, new RoutedEventArgs());
2026-03-03 16:04:20 +08:00
2026-01-10 17:31:55 +08:00
// 通知新设置面板同步状态
2026-01-02 01:35:22 +08:00
NotifySettingsPanelsSyncState(toggleSwitchName);
2026-03-03 16:04:20 +08:00
2026-01-10 17:31:55 +08:00
// 检查是否需要更新主题
2026-01-02 01:35:22 +08:00
NotifyThemeUpdateIfNeeded(toggleSwitchName);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"调用 ToggleSwitch {toggleSwitchName} 失败: {ex.Message}");
}
}
2026-01-10 17:31:55 +08:00
/// <summary>
/// 调用 MainWindow 中的 ComboBox 事件处理方法
/// </summary>
2026-01-02 01:35:22 +08:00
public static void InvokeComboBoxSelectionChanged(string comboBoxName, object selectedItem)
{
try
{
var mainWindow = GetMainWindow();
if (mainWindow == null) return;
2026-01-10 17:31:55 +08:00
// 获取 MainWindow 中的 ComboBox 控件
2026-01-02 01:35:22 +08:00
var comboBox = mainWindow.FindName(comboBoxName) as System.Windows.Controls.ComboBox;
if (comboBox != null)
{
comboBox.SelectedItem = selectedItem;
2026-03-03 16:04:20 +08:00
2026-01-10 17:31:55 +08:00
// 触发 SelectionChanged 事件
2026-01-02 01:35:22 +08:00
var selectionChangedMethodName = comboBoxName + "_SelectionChanged";
InvokeMainWindowMethod(selectionChangedMethodName, comboBox, new System.Windows.Controls.SelectionChangedEventArgs(
2026-03-03 16:04:20 +08:00
System.Windows.Controls.Primitives.Selector.SelectionChangedEvent,
new System.Collections.IList[0],
2026-01-02 01:35:22 +08:00
new System.Collections.IList[0]));
}
2026-03-03 16:04:20 +08:00
2026-01-10 17:31:55 +08:00
// 通知新设置面板同步状态
2026-01-02 01:35:22 +08:00
NotifySettingsPanelsSyncState(comboBoxName);
2026-03-03 16:04:20 +08:00
2026-01-10 17:31:55 +08:00
// 检查是否需要更新主题
2026-01-02 01:35:22 +08:00
NotifyThemeUpdateIfNeeded(comboBoxName);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"调用 ComboBox {comboBoxName} 失败: {ex.Message}");
}
}
2026-01-10 17:31:55 +08:00
/// <summary>
/// 调用 MainWindow 中的 Slider 事件处理方法
/// </summary>
2026-01-02 01:35:22 +08:00
public static void InvokeSliderValueChanged(string sliderName, double value)
{
try
{
var mainWindow = GetMainWindow();
if (mainWindow == null) return;
2026-01-10 17:31:55 +08:00
// 获取 MainWindow 中的 Slider 控件
2026-01-02 01:35:22 +08:00
var slider = mainWindow.FindName(sliderName) as System.Windows.Controls.Slider;
if (slider != null)
{
var oldValue = slider.Value;
2026-01-10 17:31:46 +08:00
slider.Value = value;
2026-03-03 16:04:20 +08:00
2026-01-10 17:31:55 +08:00
// 触发 ValueChanged 事件
2026-01-02 01:35:22 +08:00
var valueChangedMethodName = sliderName + "_ValueChanged";
2026-03-03 16:04:20 +08:00
InvokeMainWindowMethod(valueChangedMethodName, slider,
2026-01-02 01:35:22 +08:00
new System.Windows.RoutedPropertyChangedEventArgs<double>(oldValue, value));
}
2026-03-03 16:04:20 +08:00
2026-01-10 17:31:55 +08:00
// 通知新设置面板同步状态
2026-01-02 01:35:22 +08:00
NotifySettingsPanelsSyncState(sliderName);
2026-03-03 16:04:20 +08:00
2026-01-10 17:31:55 +08:00
// 检查是否需要更新主题(某些Slider可能影响UI外观)
2026-01-02 01:35:22 +08:00
NotifyThemeUpdateIfNeeded(sliderName);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"调用 Slider {sliderName} 失败: {ex.Message}");
}
}
2026-01-10 17:31:55 +08:00
/// <summary>
/// 调用 MainWindow 中的 CheckBox 事件处理方法
/// </summary>
2026-01-02 01:35:22 +08:00
public static void InvokeCheckBoxCheckedChanged(string checkBoxName, bool isChecked)
{
try
{
var mainWindow = GetMainWindow();
if (mainWindow == null) return;
2026-01-10 17:31:55 +08:00
// 获取 MainWindow 中的 CheckBox 控件
2026-01-02 01:35:22 +08:00
var checkBox = mainWindow.FindName(checkBoxName) as System.Windows.Controls.CheckBox;
if (checkBox != null)
{
checkBox.IsChecked = isChecked;
2026-03-03 16:04:20 +08:00
2026-01-10 17:31:55 +08:00
// 尝试多种可能的方法名
2026-01-02 01:35:22 +08:00
var methodNames = new[]
{
checkBoxName + "_IsCheckChanged",
checkBoxName + "_IsCheckChange",
checkBoxName + "_Checked",
checkBoxName + "_Unchecked"
};
2026-03-03 16:04:20 +08:00
2026-01-02 01:35:22 +08:00
foreach (var methodName in methodNames)
{
var method = mainWindow.GetType().GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
if (method != null)
{
InvokeMainWindowMethod(methodName, checkBox, new RoutedEventArgs());
break;
}
}
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"调用 CheckBox {checkBoxName} 失败: {ex.Message}");
}
}
2026-01-10 17:31:55 +08:00
/// <summary>
/// 安全地修改设置并保存,优先调用 MainWindow 中的事件处理方法
/// 如果找不到对应的事件处理方法,则直接修改设置并保存
/// </summary>
/// <param name="action">设置修改的 Action,例如:() => MainWindow.Settings.Startup.IsAutoUpdate = true</param>
/// <param name="eventHandlerName">可选:要调用的 MainWindow 事件处理方法名(如 "ToggleSwitchIsAutoUpdate_Toggled"</param>
/// <param name="controlName">可选:控件名称,用于状态同步(如 "ToggleSwitchIsAutoUpdate"</param>
/// <param name="eventHandlerParams">可选:事件处理方法的参数</param>
2026-01-02 01:35:22 +08:00
public static void UpdateSettingSafely(Action action, string eventHandlerName = null, string controlName = null, params object[] eventHandlerParams)
{
try
{
2026-01-10 17:31:55 +08:00
// 如果提供了事件处理方法名,优先调用
2026-01-02 01:35:22 +08:00
if (!string.IsNullOrEmpty(eventHandlerName))
{
var mainWindow = GetMainWindow();
if (mainWindow != null)
{
var method = mainWindow.GetType().GetMethod(eventHandlerName, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
if (method != null)
{
2026-01-10 17:31:55 +08:00
// 调用事件处理方法(它会自动保存设置并触发状态同步)
2026-01-02 01:35:22 +08:00
method.Invoke(mainWindow, eventHandlerParams);
2026-03-03 16:04:20 +08:00
2026-01-10 17:31:55 +08:00
// 如果提供了控件名称,确保状态同步
2026-01-02 01:35:22 +08:00
if (!string.IsNullOrEmpty(controlName))
{
NotifySettingsPanelsSyncState(controlName);
}
return;
}
}
}
2026-01-10 17:31:55 +08:00
// 如果没有事件处理方法或调用失败,直接修改设置并保存
2026-01-02 01:35:22 +08:00
action?.Invoke();
MainWindow.SaveSettingsToFile();
2026-03-03 16:04:20 +08:00
2026-01-10 17:31:55 +08:00
// 如果提供了控件名称,通知面板同步状态
2026-01-02 01:35:22 +08:00
if (!string.IsNullOrEmpty(controlName))
{
NotifySettingsPanelsSyncState(controlName);
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"更新设置失败: {ex.Message}");
}
}
2026-01-10 17:31:55 +08:00
/// <summary>
/// 直接修改设置属性并保存(用于没有对应事件处理方法的设置项)
/// </summary>
/// <param name="action">设置修改的 Action</param>
/// <param name="controlName">可选:控件名称,用于状态同步(如 "ToggleSwitchIsAutoUpdate"</param>
2026-01-02 01:35:22 +08:00
public static void UpdateSettingDirectly(Action action, string controlName = null)
{
try
{
action?.Invoke();
MainWindow.SaveSettingsToFile();
2026-03-03 16:04:20 +08:00
2026-01-10 17:31:55 +08:00
// 如果提供了控件名称,通知面板同步状态
2026-01-02 01:35:22 +08:00
if (!string.IsNullOrEmpty(controlName))
{
NotifySettingsPanelsSyncState(controlName);
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"直接更新设置失败: {ex.Message}");
}
}
2026-01-10 17:31:55 +08:00
/// <summary>
/// 调用 MainWindow 中的 TextBox 事件处理方法
/// </summary>
2026-01-02 01:35:22 +08:00
public static void InvokeTextBoxTextChanged(string textBoxName, string text)
{
try
{
var mainWindow = GetMainWindow();
if (mainWindow == null) return;
2026-01-10 17:31:55 +08:00
// 获取 MainWindow 中的 TextBox 控件
2026-01-02 01:35:22 +08:00
var textBox = mainWindow.FindName(textBoxName) as System.Windows.Controls.TextBox;
if (textBox != null)
{
textBox.Text = text;
2026-03-03 16:04:20 +08:00
2026-01-10 17:31:55 +08:00
// 触发 TextChanged 事件
2026-01-02 01:35:22 +08:00
var textChangedMethodName = textBoxName + "_TextChanged";
InvokeMainWindowMethod(textChangedMethodName, textBox, new System.Windows.Controls.TextChangedEventArgs(
System.Windows.Controls.Primitives.TextBoxBase.TextChangedEvent,
System.Windows.Controls.UndoAction.None));
}
2026-03-03 16:04:20 +08:00
2026-01-10 17:31:55 +08:00
// 通知新设置面板同步状态
2026-01-02 01:35:22 +08:00
NotifySettingsPanelsSyncState(textBoxName);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"调用 TextBox {textBoxName} 失败: {ex.Message}");
}
}
2026-01-10 17:31:55 +08:00
/// <summary>
/// 通知所有新设置面板同步指定控件的状态
/// </summary>
2026-01-02 01:35:22 +08:00
public static void NotifySettingsPanelsSyncState(string controlName)
{
try
{
2026-01-10 17:31:55 +08:00
// 延迟执行,确保设置已经保存
2026-01-02 01:35:22 +08:00
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
{
2026-01-10 17:31:55 +08:00
// 查找所有打开的设置窗口
2026-01-02 01:35:22 +08:00
foreach (Window window in Application.Current.Windows)
{
2026-04-05 21:50:53 +08:00
if (window.GetType().Name == "SettingsWindow")
2026-01-02 01:35:22 +08:00
{
2026-04-05 17:58:57 +08:00
// 同步所有面板(保守策略)
SyncAllPanels(window);
2026-01-10 17:31:55 +08:00
break; // 通常只有一个设置窗口
2026-01-02 01:35:22 +08:00
}
}
}), System.Windows.Threading.DispatcherPriority.Background);
}
catch (Exception ex)
{
2026-01-10 17:31:55 +08:00
System.Diagnostics.Debug.WriteLine($"通知设置面板同步状态失败: {ex.Message}");
2026-01-02 01:35:22 +08:00
}
}
2026-01-10 17:31:55 +08:00
/// <summary>
/// 同步所有面板的状态(保守策略)
/// </summary>
2026-01-02 01:35:22 +08:00
private static void SyncAllPanels(Window settingsWindow)
{
try
{
2026-04-05 17:58:57 +08:00
// 获取所有页面属性
var pageProperties = settingsWindow.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)
.Where(p => p.PropertyType.Name.EndsWith("Page") &&
p.PropertyType.IsSubclassOf(typeof(System.Windows.Controls.Page)));
2026-01-02 01:35:22 +08:00
2026-04-05 17:58:57 +08:00
foreach (var pageProp in pageProperties)
2026-01-02 01:35:22 +08:00
{
try
{
2026-04-05 17:58:57 +08:00
var page = pageProp.GetValue(settingsWindow) as System.Windows.Controls.Page;
if (page != null)
2026-01-02 01:35:22 +08:00
{
2026-01-10 17:31:55 +08:00
// 调用 LoadSettings 方法
2026-04-05 17:58:57 +08:00
var loadMethod = page.GetType().GetMethod("LoadSettings",
2026-01-02 01:35:22 +08:00
BindingFlags.Public | BindingFlags.Instance);
if (loadMethod != null)
{
2026-04-05 17:58:57 +08:00
loadMethod.Invoke(page, null);
2026-01-02 01:35:22 +08:00
}
}
}
catch (Exception ex)
{
2026-04-05 17:58:57 +08:00
System.Diagnostics.Debug.WriteLine($"同步页面 {pageProp.Name} 状态失败: {ex.Message}");
2026-01-02 01:35:22 +08:00
}
}
}
catch (Exception ex)
{
2026-04-05 17:58:57 +08:00
System.Diagnostics.Debug.WriteLine($"同步所有页面状态失败: {ex.Message}");
2026-01-02 01:35:22 +08:00
}
}
2026-01-10 17:31:55 +08:00
/// <summary>
/// 检查并通知设置窗口更新主题(如果设置变化可能影响主题)
/// </summary>
/// <param name="controlName">控件名称,用于判断是否是主题相关的设置</param>
2026-01-02 01:35:22 +08:00
public static void NotifyThemeUpdateIfNeeded(string controlName)
{
try
{
if (string.IsNullOrEmpty(controlName)) return;
2026-01-10 17:31:55 +08:00
// 定义可能影响主题的控件名称列表(扩展版)
2026-01-02 01:35:22 +08:00
var themeRelatedControls = new[]
{
2026-01-10 17:31:55 +08:00
"ComboBoxTheme", // 主题选择
"ToggleSwitchEnableTrayIcon", // 托盘图标(可能影响图标颜色)
"ComboBoxFloatingBarImg", // 浮动栏图标
"ComboBoxUnFoldBtnImg", // 展开按钮图标
"ComboBoxSplashScreenStyle", // 启动画面样式
"ToggleSwitchEnableSplashScreen", // 启动画面开关
"ViewboxFloatingBarScaleTransformValueSlider", // 浮动栏缩放(可能影响UI
"ViewboxFloatingBarOpacityValueSlider", // 浮动栏透明度
"ViewboxFloatingBarOpacityInPPTValueSlider", // PPT中浮动栏透明度
"UnFoldBtnImg", // 展开按钮图标(选项按钮)
"FloatingBarImg", // 浮动栏图标(选项按钮)
"Theme" // 主题(选项按钮)
2026-01-02 01:35:22 +08:00
};
2026-01-10 17:31:55 +08:00
// 检查是否是主题相关的设置(使用更灵活的匹配)
2026-01-02 01:35:22 +08:00
bool isThemeRelated = false;
string controlNameLower = controlName.ToLower();
2026-03-03 16:04:20 +08:00
2026-01-02 01:35:22 +08:00
foreach (var themeControl in themeRelatedControls)
{
string themeControlLower = themeControl.ToLower();
2026-01-10 17:31:55 +08:00
// 检查是否包含或匹配
2026-03-03 16:04:20 +08:00
if (controlNameLower.Contains(themeControlLower) ||
2026-01-02 01:35:22 +08:00
themeControlLower.Contains(controlNameLower) ||
controlNameLower == themeControlLower)
{
isThemeRelated = true;
break;
}
}
if (isThemeRelated)
{
2026-01-10 17:31:55 +08:00
// 延迟通知,确保设置已保存
2026-01-02 01:35:22 +08:00
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
{
2026-01-10 17:31:55 +08:00
// 通知设置窗口更新主题
2026-01-02 01:35:22 +08:00
NotifySettingsWindowThemeUpdate();
}), System.Windows.Threading.DispatcherPriority.Background);
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"通知主题更新失败: {ex.Message}");
}
}
2026-01-10 17:31:55 +08:00
/// <summary>
2026-04-05 17:58:57 +08:00
/// 通知设置窗口更新所有页面的主题
2026-01-10 17:31:55 +08:00
/// </summary>
2026-01-02 01:35:22 +08:00
private static void NotifySettingsWindowThemeUpdate()
{
try
{
2026-01-10 17:31:55 +08:00
// 查找所有打开的设置窗口
2026-01-02 01:35:22 +08:00
foreach (Window window in Application.Current.Windows)
{
2026-04-05 21:50:53 +08:00
// 使用类型名称匹配,因为 SettingsWindow 在不同的命名空间中
if (window.GetType().Name == "SettingsWindow")
2026-01-02 01:35:22 +08:00
{
2026-01-10 17:31:55 +08:00
// 同时调用 ApplyTheme 方法更新窗口本身
2026-03-03 16:04:20 +08:00
var applyThemeMethod = window.GetType().GetMethod("ApplyTheme",
2026-01-02 01:35:22 +08:00
BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
if (applyThemeMethod != null)
{
applyThemeMethod.Invoke(window, null);
}
2026-03-03 16:04:20 +08:00
2026-01-10 17:31:55 +08:00
break; // 通常只有一个设置窗口
2026-01-02 01:35:22 +08:00
}
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"通知设置窗口主题更新失败: {ex.Message}");
}
}
2026-01-10 17:31:55 +08:00
/// <summary>
2026-04-05 17:58:57 +08:00
/// 强制更新所有设置页面的主题(公共方法,可在外部调用)
2026-01-10 17:31:55 +08:00
/// </summary>
2026-01-02 01:35:22 +08:00
public static void ForceUpdateAllPanelsTheme()
{
NotifySettingsWindowThemeUpdate();
}
2026-01-10 17:31:55 +08:00
/// <summary>
/// 调用 MainWindow 中的 ComboBox 事件处理方法(增强版,支持主题更新通知)
/// </summary>
2026-01-02 01:35:22 +08:00
public static void InvokeComboBoxSelectionChangedWithThemeCheck(string comboBoxName, object selectedItem)
{
InvokeComboBoxSelectionChanged(comboBoxName, selectedItem);
NotifyThemeUpdateIfNeeded(comboBoxName);
}
2026-01-10 17:31:55 +08:00
/// <summary>
/// 调用 MainWindow 中的 ToggleSwitch 事件处理方法(增强版,支持主题更新通知)
/// </summary>
2026-01-02 01:35:22 +08:00
public static void InvokeToggleSwitchToggledWithThemeCheck(string toggleSwitchName, bool isOn)
{
InvokeToggleSwitchToggled(toggleSwitchName, isOn);
NotifyThemeUpdateIfNeeded(toggleSwitchName);
}
2026-01-10 17:31:55 +08:00
/// <summary>
/// 为控件树中的所有交互控件启用触摸支持(公共方法,可在外部调用)
/// </summary>
/// <param name="parent">父控件</param>
2026-01-02 01:35:22 +08:00
public static void EnableTouchSupportForControls(DependencyObject parent)
{
if (parent == null) return;
for (int i = 0; i < Media.VisualTreeHelper.GetChildrenCount(parent); i++)
{
var child = Media.VisualTreeHelper.GetChild(parent, i);
2026-03-03 16:04:20 +08:00
2026-01-10 17:31:55 +08:00
// 为 Border 控件(ToggleSwitch、选项按钮等)启用触摸支持
2026-01-02 01:35:22 +08:00
if (child is Border border)
{
2026-01-10 17:31:55 +08:00
// 检查是否是交互控件(有 Tag 或 Cursor 为 Hand
2026-01-02 01:35:22 +08:00
if (border.Tag != null || border.Cursor == Cursors.Hand)
{
border.IsManipulationEnabled = true;
2026-03-03 16:04:20 +08:00
2026-01-10 17:31:55 +08:00
// 添加触摸事件支持,将触摸事件转换为鼠标事件
2026-01-02 01:35:22 +08:00
border.TouchDown += (s, e) =>
{
var touchPoint = e.GetTouchPoint(border);
var mouseEvent = new MouseButtonEventArgs(Mouse.PrimaryDevice, Environment.TickCount, MouseButton.Left)
{
RoutedEvent = UIElement.MouseLeftButtonDownEvent,
Source = border
};
border.RaiseEvent(mouseEvent);
border.CaptureTouch(e.TouchDevice);
e.Handled = true;
};
2026-03-03 16:04:20 +08:00
2026-01-10 17:31:55 +08:00
// 添加触摸释放事件
2026-01-02 01:35:22 +08:00
border.TouchUp += (s, e) =>
{
border.ReleaseTouchCapture(e.TouchDevice);
e.Handled = true;
};
}
}
2026-01-10 17:31:55 +08:00
// 为 Button 控件启用触摸支持
2026-01-02 01:35:22 +08:00
else if (child is Button button)
{
button.IsManipulationEnabled = true;
}
2026-01-10 17:31:55 +08:00
// 为 ComboBox 启用触摸支持
2026-01-02 01:35:22 +08:00
else if (child is ComboBox comboBox)
{
comboBox.IsManipulationEnabled = true;
}
2026-01-10 17:31:55 +08:00
// 为 Slider 启用触摸支持
2026-01-02 01:35:22 +08:00
else if (child is Slider slider)
{
slider.IsManipulationEnabled = true;
}
2026-01-10 17:31:55 +08:00
// 为 TextBox 启用触摸支持
2026-01-02 01:35:22 +08:00
else if (child is TextBox textBox)
{
textBox.IsManipulationEnabled = true;
}
2026-01-10 17:31:55 +08:00
// 为 CheckBox 启用触摸支持
2026-01-02 01:35:22 +08:00
else if (child is CheckBox checkBox)
{
checkBox.IsManipulationEnabled = true;
}
2026-01-10 17:31:55 +08:00
// 为 RadioButton 启用触摸支持
2026-01-02 01:35:22 +08:00
else if (child is RadioButton radioButton)
{
radioButton.IsManipulationEnabled = true;
}
2026-03-03 16:04:20 +08:00
2026-01-10 17:31:55 +08:00
// 递归处理子元素
2026-01-02 01:35:22 +08:00
EnableTouchSupportForControls(child);
}
}
}
}