XAF:如何让用户在运行时个性化界面并将个性化信息保存到数据库中 win/web/entityframework/xpo
public sealed partial class MySolutionWindowsFormsModule : ModuleBase { private void Application_CreateCustomModelDifferenceStore(Object sender, CreateCustomModelDifferenceStoreEventArgs e) { e.Store = new ModelDifferenceDbStore((XafApplication)sender, typeof(ModelDifference), true, "Win"); e.Handled = true; } private void Application_CreateCustomUserModelDifferenceStore(Object sender, CreateCustomModelDifferenceStoreEventArgs e) { e.Store = new ModelDifferenceDbStore((XafApplication)sender, typeof(ModelDifference), false, "Win"); e.Handled = true; } //... public override void Setup(XafApplication application) { base.Setup(application); application.CreateCustomModelDifferenceStore += Application_CreateCustomModelDifferenceStore; application.CreateCustomUserModelDifferenceStore += Application_CreateCustomUserModelDifferenceStore; } }
注意红色的代码:
1.MySolutionWindowsFormsModule 是指 你的项目.Module.Win这个project.
2.在setup中的2行代码增加事件,CreateCustomModelDifferenceStore 是指全局的模型设置信息。
3.CreateCustomUserModelDifferenceStore ,是指当前用户的模型信息。
4.e.Store = new ModelDifferenceDbStore((XafApplication)sender, typeof(ModelDifference), true, "Win"); 其中,true代表是否为全局设置,“win"代表平台,当然在web中,需要写"web"。web项目是:你的项目.Module.Web
public sealed partial class MySolutionAspNetModule : ModuleBase { private void Application_CreateCustomModelDifferenceStore(Object sender, CreateCustomModelDifferenceStoreEventArgs e) { e.Store = new ModelDifferenceDbStore((XafApplication)sender, typeof(ModelDifference), true, "Web"); e.Handled = true; } private void Application_CreateCustomUserModelDifferenceStore(Object sender, CreateCustomModelDifferenceStoreEventArgs e) { e.Store = new ModelDifferenceDbStore((XafApplication)sender, typeof(ModelDifference), false, "Web"); e.Handled = true; } // ... public override void Setup(XafApplication application) { base.Setup(application); application.CreateCustomModelDifferenceStore += Application_CreateCustomModelDifferenceStore; application.CreateCustomUserModelDifferenceStore += Application_CreateCustomUserModelDifferenceStore; } }
下面有介绍如何启用管理UI。
如果你使用Entity Framework, 需要在DbContext中做如下声明:
using DevExpress.Persistent.BaseImpl.EF; // ... public class MyDbContext : DbContext { // ... public DbSet<ModelDifference> ModelDifferences { get; set; } public DbSet<ModelDifferenceAspect> ModelDifferenceAspects { get; set; } }
另外,一定要确保所有用户都具有读/写访问 ModelDifference 和 ModelDifferenceAspect 类型的权限,否则有的权限模块会阻止这个行为成功执行。
下面是在代码中给权限的代码:
C# public class Updater : ModuleUpdater { public override void UpdateDatabaseAfterUpdateSchema() { base.UpdateDatabaseAfterUpdateSchema(); SecuritySystemRole defaultRole = ObjectSpace.FindObject<SecuritySystemRole>( new BinaryOperator("Name", "Default")); if(defaultRole == null) { defaultRole = ObjectSpace.CreateObject<SecuritySystemRole>(); // ... defaultRole.SetTypePermissionsRecursively<ModelDifference>( SecurityOperations.ReadWriteAccess, SecuritySystemModifier.Allow); defaultRole.SetTypePermissionsRecursively<ModelDifferenceAspect>( SecurityOperations.ReadWriteAccess, SecuritySystemModifier.Allow); // The 'Create' permission is additionally required if you use the Middle Tier Application Server defaultRole.SetTypePermissionsRecursively<ModelDifference>( SecurityOperations.Create, SecuritySystemModifier.Allow); defaultRole.SetTypePermissionsRecursively<ModelDifferenceAspect>( SecurityOperations.Create, SecuritySystemModifier.Allow); } sampleUser.Roles.Add(defaultRole); // ... ObjectSpace.CommitChanges(); } // ... }
在运行时,通过菜单打开模型差异管理的列表,可以看到相关的按钮。
在这里,会列出存在的用户的模型差异(用户打开一次程序后,就会保存该用户的模型差异),如果想为没有使用过程序的用户创建模型差异,可以单击创建模型差异。
如果想要加载共享的模式差异(就是在 Visual Studio (Model.xafml) 文件中创建的那个),可以点击导入共享模型差异。
另外三个按钮是:复制模型差异、 导出模型差异和重置模型差异 ,将应用于所选记录。
在模型的差异列表视图中,有一条共享模型差异的记录。它不是指某个用户的设置,而是指应用程序全局的设置。
XAF开发成品案例参考
如果您觉得阅读本文对您有帮助,请点一下“推荐”按钮,您的“推荐”将是我最大的写作动力!欢迎各位转载,但是未经作者本人同意,转载文章之后必须在文章页面明显位置给出作者和原文连接,否则保留追究法律责任的权利。
作者博客: http://www.cnblogs.com/foreachlife
欢迎加入CIIP框架\XAF技术应用交流群: 336090194 群文件中有更多相关工具及文档资料
转载请注明出处。多谢!
欢迎加我微信: admiralcn 或扫码:

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端