使用 MVVM 将 GUI 与业务逻辑分离的最后步骤?

发布于 2024-08-17 18:09:02 字数 846 浏览 3 评论 0原文

就在最近,我了解了如何使用 MVVM 将 GUI 与底层模型解耦。我最终尽可能多地学习,将当前的应用程序转换为 MVVM,并且取得了很大的成功。现在我需要弄清楚如何实际获取 Blend 中生成的 GUI,并使用它来代替我当前在 Visual Studio 中设计的 GUI。我在网上找不到任何资源来使其尽可能无缝。我想知道你们都做了什么以及为你们做了什么工作。

我的最终解决方案是允许我在运行时从菜单中选择一种外观,并立即将 GUI 从当前的外观更改为用户选择的另一个外观。 有人可以向我指出解释如何执行此操作的帖子吗?

我当前的目标不太雄心勃勃 - 我希望能够将新的 Blend GUI 添加到我的 Visual Studio 项目中并在编译时添加,让新的 Blend GUI 出现。如果我想回到旧的 GUI,我就必须重新编译。现在,没关系。

我已将 Blend 项目添加到 VS2008 解决方案中,并将其设置为启动应用程序。效果很好——如果我运行该应用程序,就会出现我的新 GUI,而不是旧的 GUI。现在的问题是,它需要的 DLL 实际上位于不同的文件夹中——原始启动应用程序的 bin\Debug 文件夹。 我是否应该将原始 GUI 作为启动应用程序,然后让其应用程序代码隐藏在加载其他 GUI 中?

此外,每个相应的 GUI 都需要对 ViewModel 的引用。就我而言,我只是在当前的 GUI 类中实例化它。对于 Blend GUI,我也在那里实例化了一个,因为只有其中一个 GUI 会处于活动状态。 这是应该使用 Unity 框架之类的东西吗?

对于所有可能不连贯的问题,我深表歉意,但我不太确定应该如何从这里继续。我觉得我已经非常接近地向自己证明 MVVM 是从 GUI 的角度来看的最佳选择(我已经对可测试性深信不疑了)。

Just recently, I learned about using MVVM to decouple GUIs from the underlying model. I ended up learning as much as I could to convert my current application over to MVVM, and was largely successful. Now I need to figure out how to actually take a GUI generated in Blend and use it in place of my current GUI, which was designed in Visual Studio. I can't find any resources on the web for making this as seamless as possible. I'd like to know what you all have done and have had work for you.

My ultimate solution would be something that would allow me to, at runtime, select a skin from a menu and immediately have the GUI change from the current one to another that the user selects. Can anyone point me to posts that explain how to do this?

My current goal is less ambitious -- I'd like to be able to add my new Blend GUI into my Visual Studio project and when I compile, have the new Blend GUI appear. If I want to go back to the old GUI, I would have to recompile. For now, that is okay.

I've got my Blend project added to my VS2008 solution, and have set it to be the startup application. That works fine -- if I run the app, my new GUI appears instead of the old one. The problem now is that it needs DLLs that are actually in a different folder -- the bin\Debug folder of the original startup application. Am I supposed to leave my original GUI as the startup application, and then have its App codebehind load the other GUI?

Also, each of the respective GUIs needs a reference to the ViewModel. In my case, I was just instantiating it in my current GUI class. For the Blend GUI, I instantiated one there as well, since only one of the GUIs will be active. Is this where something like the Unity framework should be used?

Sorry about all of the possibly-incoherent questions, but I'm not quite sure how I should proceed from here. I feel like I'm so close to proving to myself that MVVM is the way to go from a GUI standpoint (I'm already sold on the testability bit).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

長街聽風 2024-08-24 18:09:03

我见过的所有示例都通过使用某种形式的 ResourceDictionary 交换来动态切换 GUI 外观。一些链接:

在运行时加载 XAML 资源字典

WPF 在运行时更改主题/样式

希望有帮助。

All the examples I've seen dynamically switch GUI appearance by using some form of ResourceDictionary swapping. A few links:

Load XAML Resource Dictionaries at Runtime

WPF change theme/style at runtime

Hope that helps.

又爬满兰若 2024-08-24 18:09:03

我发现了一个错误,在代码的一部分中,我使用了错误的属性来获取当前正在运行的程序集的路径。我现在正在使用

System.Reflection.Assembly.GetExecutingAssembly().Location

虽然这确实有效,但只有当我将 Blend 项目的 bin\Debug 文件夹中的 exe 复制到主应用程序的 bin\Debug 文件夹中时,它才有效。我想,我将不得不通过使用构建后事件来忍受这个问题。在过去几个月使用 .NET 时,我被宠坏了,我不必这样做(就像以前在 C++ 中一样),因为所有引用的程序集都会自动复制。如果我想调试任何隐藏代码,我还必须在 Blend 项目的设置中设置启动可执行文件,这也很不方便,特别是在路径设置不同的不同计算机上工作时。如有任何建议,我们也将不胜感激!

I found a mistake, where in one part of my code I was using the wrong property to get at the currently-running assembly's path. I am now using

System.Reflection.Assembly.GetExecutingAssembly().Location

Although this does work, it only works if I copy the exe from the Blend project's bin\Debug folder into my main application's bin\Debug folder. I will have to live with this by using a post-build event, I guess. I was so spoiled for the past several months working with .NET, where I didn't have to do this (like before in C++) because all of the referenced assemblies get automatically copied over. If I want to debug any code-behind, I also have to set the starting executable in the Blend project's settings, which is inconvenient as well, especially when working on different computers where the paths aren't set up the same. Any suggestions here would also be appreciated!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文