Ver código fonte

20251017001 添加Controller文件夹用于存储ViewModel的逻辑控制器

向羽 孟 1 mês atrás
pai
commit
9bdc57baf9

+ 3 - 4
MvvmScaffoldFrame48.DLL/AuditTrail/ErrorMessageRecordManagement.cs

@@ -194,8 +194,7 @@ namespace MvvmScaffoldFrame48.DLL.AuditTrail
         /// <summary>
         /// 获取错误数据信息
         /// </summary>
-        /// <param name="UserID">操作员ID</param>
-        /// <returns></returns>
+        /// <param name="ErrorDateTime">错误时间</param>
         public List<ErrorMessageRecordModel> GetErrorMessageRecordByDateTime(DateTime ErrorDateTime)
         {
             var ErrorMessages = new List<ErrorMessageRecordModel>();
@@ -230,8 +229,8 @@ namespace MvvmScaffoldFrame48.DLL.AuditTrail
         /// <summary>
         /// 获取错误数据信息
         /// </summary>
-        /// <param name="UserID">操作员ID</param>
-        /// <returns></returns>
+        /// <param name="ErrorStartDateTime">错误开始时间</param>
+        /// <param name="ErrorEndDateTime">错误结束时间</param>
         public List<ErrorMessageRecordModel> GetErrorMessageRecordByDateTime(DateTime ErrorStartDateTime,DateTime ErrorEndDateTime)
         {
             var ErrorMessages = new List<ErrorMessageRecordModel>();

+ 1 - 0
MvvmScaffoldFrame48.DLL/MvvmScaffoldFrame48.Dll.csproj

@@ -78,6 +78,7 @@
     <Compile Include="LogTools\TxtLog.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="UserManager.cs" />
+    <Compile Include="WindowsTools\OnScreenKeyboardTools.cs" />
   </ItemGroup>
   <ItemGroup>
     <ProjectReference Include="..\MvvmScaffoldFrame48.MODEL\MvvmScaffoldFrame48.Model.csproj">

+ 229 - 0
MvvmScaffoldFrame48.DLL/WindowsTools/OnScreenKeyboardTools.cs

@@ -0,0 +1,229 @@
+using Microsoft.Win32;
+using System;
+using System.Collections.Generic;
+using System.Diagnostics;
+using System.IO;
+using System.Linq;
+using System.Runtime.InteropServices;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace MvvmScaffoldFrame48.DLL.WindowsTools
+{
+    public static class OnScreenKeyboardTools
+    {
+        [DllImport("kernel32.dll")]
+        private static extern IntPtr GetModuleHandle(string lpModuleName);
+
+        [DllImport("user32.dll")]
+        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
+
+        [DllImport("user32.dll")]
+        private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
+
+        private const int SW_SHOW = 5;
+        private const int SW_HIDE = 0;
+        /// <summary>
+        /// 启动软键盘(兼容Windows 10及以上版本)
+        /// </summary>
+        public static void KeyBoardShow()
+        {
+            try
+            {
+                // 检查是否为Windows 10或更高版本
+                if (IsWindows10OrHigher())
+                {
+                    // 尝试启动TabTip
+                    if (TryStartTabTip())
+                        return;
+                }
+
+                // 回退到传统osk
+                StartOnScreenKeyboard();
+            }
+            catch (Exception ex)
+            {
+                throw new InvalidOperationException("无法启动软键盘", ex);
+            }
+        }
+
+        /// <summary>
+        /// 隐藏软键盘
+        /// </summary>
+        public static void KeyBoardHide()
+        {
+            try
+            {
+                // 隐藏TabTip窗口
+                IntPtr tabTipHwnd = FindWindow("IPTip_Main_Window", null);
+                if (tabTipHwnd != IntPtr.Zero)
+                {
+                    ShowWindow(tabTipHwnd, SW_HIDE);
+                }
+
+                // 隐藏OSK窗口
+                IntPtr oskHwnd = FindWindow("OSKMainClass", "屏幕键盘");
+                if (oskHwnd != IntPtr.Zero)
+                {
+                    ShowWindow(oskHwnd, SW_HIDE);
+                }
+            }
+            catch
+            {
+                // 忽略异常
+            }
+        }
+
+        private static void StartOnScreenKeyboard()
+        {
+            try
+            {
+                // 检查是否已经运行
+                Process[] oskProcesses = Process.GetProcessesByName("osk");
+                if (oskProcesses.Length > 0)
+                {
+                    // 如果已经运行,尝试显示窗口
+                    IntPtr oskHwnd = FindWindow("OSKMainClass", "屏幕键盘");
+                    if (oskHwnd != IntPtr.Zero)
+                    {
+                        ShowWindow(oskHwnd, SW_SHOW);
+                        return;
+                    }
+                    else
+                    {
+                        // 如果找不到窗口,杀死进程重新启动
+                        foreach (var process in oskProcesses)
+                        {
+                            try { process.Kill(); } catch { }
+                        }
+                    }
+                }
+
+                Process.Start("osk.exe");
+            }
+            catch (Exception ex)
+            {
+                throw new InvalidOperationException("无法启动屏幕键盘", ex);
+            }
+        }
+
+        private static bool IsWindows10OrHigher()
+        {
+            var os = Environment.OSVersion;
+            if (os.Version.Major >= 10)
+                return true;
+
+            try
+            {
+                using (var key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion"))
+                {
+                    var currentBuild = key?.GetValue("CurrentBuild")?.ToString();
+                    if (int.TryParse(currentBuild, out int buildNumber))
+                    {
+                        // Windows 10 build 10240及以上
+                        return buildNumber >= 10240;
+                    }
+                }
+            }
+            catch
+            {
+                // 忽略异常
+            }
+
+            // 兼容性检查:Windows 8.1及以后版本的Major是6,但Minor版本不同
+            return os.Version.Major == 10 || (os.Version.Major == 6 && os.Version.Minor >= 2);
+        }
+
+        private static bool TryStartTabTip()
+        {
+            try
+            {
+                // 检查是否已经运行
+                Process[] tabTipProcesses = Process.GetProcessesByName("TabTip");
+                if (tabTipProcesses.Length > 0)
+                {
+                    // 如果找不到窗口,杀死进程重新启动
+                    foreach (var process in tabTipProcesses)
+                    {
+                        try { process.Kill(); } catch { }
+                    }
+                }
+
+                // 方法1:通过注册表查找路径
+                string tabTipPath = GetTabTipPathFromRegistry();
+                if (!string.IsNullOrEmpty(tabTipPath) && File.Exists(tabTipPath))
+                {
+                    Process.Start(tabTipPath);
+                    return true;
+                }
+
+                // 方法2:通过系统路径查找
+                string systemPath = Path.Combine(Environment.SystemDirectory, "TabTip.exe");
+                if (File.Exists(systemPath))
+                {
+                    Process.Start(systemPath);
+                    return true;
+                }
+
+                // 方法3:使用modern Windows 10路径
+                string modernPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles),
+                    @"TabletTip\1.7\TabTip.exe");
+                if (File.Exists(modernPath))
+                {
+                    Process.Start(modernPath);
+                    return true;
+                }
+
+                // 方法4:使用默认路径
+                string defaultPath = @"C:\Program Files\Common Files\Microsoft Shared\ink\TabTip.exe";
+                if (File.Exists(defaultPath))
+                {
+                    Process.Start(defaultPath);
+                    return true;
+                }
+            }
+            catch (Exception ex)
+            {
+                // 记录异常但不中断
+                System.Diagnostics.Debug.WriteLine($"启动TabTip失败: {ex.Message}");
+            }
+
+            return false;
+        }
+
+        private static string GetTabTipPathFromRegistry()
+        {
+            try
+            {
+                using (var key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Appx\AppxAllUserStore\InboxApplications"))
+                {
+                    if (key != null)
+                    {
+                        foreach (var subKeyName in key.GetSubKeyNames())
+                        {
+                            if (subKeyName.Contains("Microsoft.Windows.Keyboard"))
+                            {
+                                using (var subKey = key.OpenSubKey(subKeyName))
+                                {
+                                    var installPath = subKey?.GetValue("Path")?.ToString();
+                                    if (!string.IsNullOrEmpty(installPath))
+                                    {
+                                        string tabTipPath = Path.Combine(installPath, "TabTip.exe");
+                                        if (File.Exists(tabTipPath))
+                                            return tabTipPath;
+                                    }
+                                }
+                            }
+                        }
+                    }
+                }
+            }
+            catch
+            {
+                // 忽略注册表访问异常
+            }
+
+            return null;
+        }
+    }
+}

+ 5 - 3
MvvmScaffoldFrame48.VIEWMODEL/MvvmScaffoldFrame48.ViewModel.csproj

@@ -60,10 +60,11 @@
     <Reference Include="System.Xml" />
   </ItemGroup>
   <ItemGroup>
-    <Compile Include="BaseViewModel.cs" />
-    <Compile Include="MainViewModel.cs" />
+    <Compile Include="Controller\MainController.cs" />
+    <Compile Include="ViewModel\BaseViewModel.cs" />
+    <Compile Include="ViewModel\MainViewModel.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
-    <Compile Include="RelayComand.cs" />
+    <Compile Include="ViewModel\RelayComand.cs" />
   </ItemGroup>
   <ItemGroup>
     <ProjectReference Include="..\MvvmScaffoldFrame48.DLL\MvvmScaffoldFrame48.Dll.csproj">
@@ -75,5 +76,6 @@
       <Name>MvvmScaffoldFrame48.Model</Name>
     </ProjectReference>
   </ItemGroup>
+  <ItemGroup />
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
 </Project>

+ 1 - 1
MvvmScaffoldFrame48.VIEWMODEL/BaseViewModel.cs → MvvmScaffoldFrame48.VIEWMODEL/ViewModel/BaseViewModel.cs

@@ -6,7 +6,7 @@ using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 
-namespace MvvmScaffoldFrame48.ViewModel
+namespace MvvmScaffoldFrame48.ViewModel.ViewModel
 {
     /// <summary>
     /// 视图逻辑基类

+ 19 - 2
MvvmScaffoldFrame48.VIEWMODEL/MainViewModel.cs → MvvmScaffoldFrame48.VIEWMODEL/ViewModel/MainViewModel.cs

@@ -1,13 +1,14 @@
 // 演示类
 using MvvmScaffoldFrame48.DLL;
 using MvvmScaffoldFrame48.DLL.AuditTrail;
+using MvvmScaffoldFrame48.DLL.WindowsTools;
 using MvvmScaffoldFrame48.Model;
 using MvvmScaffoldFrame48.Model.AuditTrail;
 using System;
 using System.Collections.ObjectModel;
 using System.Windows.Input;
 
-namespace MvvmScaffoldFrame48.ViewModel
+namespace MvvmScaffoldFrame48.ViewModel.ViewModel
 {
     public class MainViewModel : BaseViewModel
     {
@@ -41,6 +42,20 @@ namespace MvvmScaffoldFrame48.ViewModel
                 }
             }
         }
+
+        private bool _staticTest = true;
+        public bool StaticTest
+        {
+            get { return _staticTest; }
+            set
+            {
+                if (_staticTest != value)
+                {
+                    _staticTest = value;
+                    OnPropertyChanged(nameof(StaticTest));
+                }
+            }
+        }
         #endregion
 
         #region 界面绑定事件
@@ -60,7 +75,7 @@ namespace MvvmScaffoldFrame48.ViewModel
             user.Email = Email;
             UserManager.AddUser(user);
 
-
+            OnScreenKeyboardTools.KeyBoardShow();
             OperationRecordManagement operationRecordManagement = new OperationRecordManagement();
             operationRecordManagement.InsertOperationRecord(new OperationRecordModel()
             {
@@ -82,6 +97,8 @@ namespace MvvmScaffoldFrame48.ViewModel
         {
             Name = "小1";
             Email = "111@qq.com";
+
+            StaticTest = !StaticTest;
         }
         #endregion
 

+ 1 - 1
MvvmScaffoldFrame48.VIEWMODEL/RelayComand.cs → MvvmScaffoldFrame48.VIEWMODEL/ViewModel/RelayComand.cs

@@ -6,7 +6,7 @@ using System.Text;
 using System.Threading.Tasks;
 using System.Windows.Input;
 
-namespace MvvmScaffoldFrame48.ViewModel
+namespace MvvmScaffoldFrame48.ViewModel.ViewModel
 {
     public class RelayCommand : ICommand
     {

+ 12 - 0
MvvmScaffoldFrame48.VIEWMODEL/controller/MainController.cs

@@ -0,0 +1,12 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace MvvmScaffoldFrame48.ViewModel.Controller
+{
+    internal class MainController
+    {
+    }
+}

+ 15 - 1
MvvmScaffoldFrame48/MainWindow.xaml

@@ -7,7 +7,7 @@
         mc:Ignorable="d"
         Title="MainWindow" Height="450" Width="800">
     <Grid>
-        <StackPanel>
+        <StackPanel Margin="0,0,0,40">
             <ToolBar>
                 <Label Content="姓名:"></Label>
                 <TextBox Text="{Binding Name}"  Width="50"></TextBox>
@@ -23,5 +23,19 @@
 
             </StackPanel>
         </StackPanel>
+        <Grid VerticalAlignment="Bottom" Height="40">
+            <Ellipse Width="20" Height="20" Margin="48,0,0,0" >
+                <Ellipse.Style>
+                    <Style TargetType="Ellipse">
+                        <Setter Property="Fill" Value="Red" />
+                        <Style.Triggers>
+                            <DataTrigger Binding="{Binding StaticTest}" Value="True">
+                                <Setter Property="Fill" Value="Green" />
+                            </DataTrigger>
+                        </Style.Triggers>
+                    </Style>
+                </Ellipse.Style>
+            </Ellipse>
+        </Grid>
     </Grid>
 </Window>