ImFramework 1.0.1

dotnet add package ImFramework --version 1.0.1
                    
NuGet\Install-Package ImFramework -Version 1.0.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ImFramework" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ImFramework" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="ImFramework" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ImFramework --version 1.0.1
                    
#r "nuget: ImFramework, 1.0.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package ImFramework@1.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=ImFramework&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=ImFramework&version=1.0.1
                    
Install as a Cake Tool

ImFramework 项目说明

ImFramework 是一个基于 WPF.NET Framework 4.7.2 的客户端基础框架库,为桌面应用提供 MVVM 支撑、事件通信、撤销重做、日志、行为与转换器等通用能力,便于快速搭建可维护的 WPF 应用。


技术栈与依赖

项目 说明
目标框架 .NET Framework 4.7.2
输出类型 类库 (DLL)
UI 框架 WPF (PresentationCore, PresentationFramework, WindowsBase, System.Xaml)
第三方库 Newtonsoft.Json 13.0.3

项目结构概览

ImFramework/
├── Behaviour/      # 附加行为(拖拽、缩放)
├── Common/         # 通用辅助类与绑定代理
├── Convert/        # 值转换器(XAML 绑定用)
├── Effects/        # 视觉特效(闪烁等)
├── Events/         # 消息派发与对象池
├── Log/            # 日志管理
├── Model/          # 基础数据模型(INotifyPropertyChanged)
├── UndoRedo/       # 撤销/重做
├── Utils/          # 工具方法
└── ViewModel/      # 视图模型基类与命令

模块分析

1. Model(模型基类)

  • NotifyBase
    实现 INotifyPropertyChanged,通过 DoNotify([CallerMemberName] string propertyName) 触发属性变更通知,供 XAML 绑定与 MVVM 使用。

2. ViewModel(视图模型与命令)

  • ViewModelBase
    继承 NotifyBase,实现 IViewModel(含 View 属性)。
    内置窗口相关命令与默认实现:

    • CloseWindowCommand:关闭窗口
    • MinWindowCommand:最小化
    • MaxWindowCommand:最大化/还原(会保存还原时的窗口位置与大小)

    子类可重写 InitProperty()InitCommand() 扩展属性和命令。

  • CommandBase
    实现 ICommand,通过委托封装 Execute 与可选的 CanExecute,并与 CommandManager.RequerySuggested 联动,便于在 XAML 中绑定按钮等操作。

3. Events(事件与消息)

  • IMsg / Message
    消息接口:SenderMsgTypeData
    Message 使用 对象池 ObjectPool<IMsg> 复用实例,减少分配。

  • MsgDispatcher(注:类名为 MsgDispater,拼写为 Dispatcher)
    静态消息中心:按 msgType 注册/移除 Action<IMsg>,通过 Send(msgType)Send(msgType, data)Send(sender, msgType, data) 派发。无订阅者时写一条 Info 日志。

  • ObjectPool<T>
    泛型对象池,构造时指定初始容量、创建函数与可选的回收函数,Get()/Release() 用于取还对象。

4. UndoRedo(撤销/重做)

  • IUndoCommand
    接口:Execute() 执行,Unexecute() 撤销。

  • UndoCommandManager
    单例,维护撤销栈与重做栈。

    • ExecuteCommand(command):执行命令并压入撤销栈,清空重做栈。
    • Undo():从撤销栈弹出并 Unexecute,再压入重做栈。
    • Redo():从重做栈弹出并 Execute,再压入撤销栈。
    • Clear():清空两栈。
      内部使用锁保证多线程安全。

5. Log(日志)

  • LogManager
    静态日志类。
    • 级别:InfoErrorFatal
    • 日志写入程序集目录下 Logs 文件夹,文件名带时间戳与 GUID。
    • 使用 ConcurrentQueue 缓冲,后台任务异步写文件,避免阻塞调用方。
    • 提供 Log(level, message)LogInfoLogErrorLogFatal,部分重载支持 showMessageBox
    • CleanOldLogs(daysToKeep) 按天数清理旧文件,Stop() 停止写盘任务。

6. Behaviour(附加行为)

  • DragBehaviour
    附加属性 Drag,挂到 UIElement 上可拖拽移动,内部使用 TranslateTransform 和鼠标事件。

  • ScaleBehaviour
    附加属性 Scale,挂到 UIElement 上可通过鼠标滚轮缩放,内部使用 ScaleTransform

二者均通过 DependencyProperty.RegisterAttached 实现,在 XAML 中通过 Behaviour:xxx.Attach="True" 等形式使用。

7. Common(通用)

  • BindingProxy
    继承 Freezable,暴露一个 Data 依赖属性,用于在 XAML 树中做“绑定中转”,解决跨层级绑定或命名作用域问题。

  • PasswordBoxHelper
    PasswordBox 提供 BoundPassword 附加属性,使密码可与 ViewModel 属性双向绑定(避免直接绑定 Password 的安全与设计问题)。

  • CustomCursorHelper
    自定义光标相关辅助(具体用法见源码)。

8. Convert(值转换器)

均在 System.Windows.Data.IValueConverter 下,供 XAML Binding 的 Converter 使用:

转换器 作用
CustomBooleanToVisibilityConverter bool → Visibility;支持 ConverterParameter 取反(如 "False" 时 true→Collapsed, false→Visible)
ZeroToVisibilityConverter 字符串空/非空 → Visible/Collapsed
MultiplyByConverter double × parameter(字符串解析)→ 乘积,用于按比例换算
ColorConvert 颜色名字符串 → SolidColorBrush(通过反射取 Colors 静态属性)
Radio2BoolConverter 单选与当前选中值比较 → bool;反向为 bool → 选中值(用于 RadioButton 绑定)
BoolToVisibilityConverter bool → Visibility,可选 CollapseWhenInvisible(false 时 Hidden)

9. Effects(特效)

  • BlinkingEffect
    附加属性 IsBlinking,当为 true 时对 Path 做透明度闪烁动画(Storyboard + DoubleAnimation),用于高亮或提示。

10. Utils(工具)

  • Utils(命名空间为 ImFramework.Common
    静态工具方法集合,包括但不限于:
    • 字符串:IsNumericArrAddGetValidationByArray
    • 颜色、图像、加密、进程等常见辅助(详见 Utils.cs
      与 WPF 的 System.Windows.MediaBitmapImage 等有交互。

使用建议

  1. MVVM:界面绑定使用 ViewModelBase + NotifyBase + CommandBase,数据驱动 UI。
  2. 解耦:跨模块通信用 MsgDispatcher 按消息类型订阅/发送,避免强引用。
  3. 可撤销操作:实现 IUndoCommand 并交给 UndoCommandManager 统一执行/撤销/重做。
  4. 日志:关键流程与异常用 LogManager 记录,便于排查与审计。
  5. XAML:需要时在资源中声明上述 Converter、在控件上挂 Behaviour 或 Effect,或在 ViewModel 中通过 BindingProxy 解决绑定上下文问题。

小结

ImFramework 子项目是一个无 UI 控件、仅提供基础能力的 WPF 支撑库:
MVVM 基类与命令、消息与对象池、撤销重做、日志、拖拽/缩放行为、常用转换器与工具方法。适合作为桌面产品或内部工具的统一基础层,在此基础上再扩展业务与界面。

Product Compatible and additional computed target framework versions.
.NET Framework net472 is compatible.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETFramework 4.7.2

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.1 118 2/10/2026
1.0.0 112 2/10/2026