复合 WPF:显示/隐藏视图?

发布于 2024-08-02 19:58:12 字数 500 浏览 1 评论 0原文

我正在快速了解 Composite WPF,构建一个小型演示应用程序来解决这些问题。我的应用程序有一个区域和两个模块:模块 A 和模块 B。每个模块都包含一个简单的“Hello World”文本块。这两个模块均设置为按需加载,按照 此 MSDN How-至

外壳有两个按钮,“加载模块 A”和“加载模块 B”。单击按钮时,会加载相应的模块。因此,假设我单击“加载模块 A”,然后单击“加载模块 B”。模块 A,然后模块 B 按预期加载。但如果我再次单击“加载模块 A”,则什么也没有发生。

我被困在这一点上。我认为我的问题是我需要激活和停用模块中的视图,而不是使用按需加载。但我不知道如何做到这一点,也找不到任何讨论它的文档或博客。

所以,这是我的问题:如何加载/卸载(或显示/隐藏)视图?如果有人可以向我指出示例代码,我将非常感激。感谢您的帮助。

I am getting up to speed on Composite WPF, building a small demo app to work through the issues. My app has one region, and two modules, Module A and Module B. Each module contains a simple "Hello World" text block. Both modules are set up as load-on-demand, as per this MSDN How-To.

The shell has two buttons, "Load Module A" and "Load Module B". When a button is clicked, the corresponding module is loaded. So, lets say I click "Load Module A", then "Load Module B". Module A, then Module B load as expected. But if I click "Load Module A" again, nothing happens.

I'm stuck at this point. I think my problem is that I need to activate and deactivate the views in the modules, rather than using load-on-demand. But I have no idea how to do that, and I can't find any documentation or blogs that talk about it.

So, here's my question: How to I load/unload (or show/hide) views? If someone can point me to sample code, I'd really appreciate it. Thanks for your help.

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

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

发布评论

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

评论(1

不甘平庸 2024-08-09 19:58:12

我找到了答案。方法如下:在启动时加载所有模块,然后根据需要激活和停用视图。我打算将这个问题写成一篇 CodeProject 文章,但这里是如何做到这一点的概述:

(1) 在模块的 Initialize() 方法中,添加模块,但不要激活它:

public void Initialize()
{
    // Get main region
    var mainRegion = m_RegionManager.Regions["MainRegion"];

    // Load Module B
    var newView = new ModuleBView();
    mainRegion.Add(newView, "ModuleA.ModuleAView");
}

请注意,Add () 方法有两个参数。第二个参数是视图的名称,我们将其设置为视图的 ToString() 方法生成的值。我们将在下一步中了解原因。

(2)当激活一个视图时,我们需要停用前一个视图。但我们可能不知道视图的名称,因此我们停用所有活动视图:

public static void ClearRegion(IRegion region)
{
    // Get existing view names
    var oldViewNames = new List<string>();
    foreach (var v in region.Views)
    {
        var s = v.ToString();
        oldViewNames.Add(s);
    }

    // Remove existing views
    foreach (var oldViewName in oldViewNames)
    {
        var oldView = region.GetView(oldViewName);
        region.Deactivate(oldView);
    }
}

由于我们将每个视图的名称设置为其 ToString() 值,因此我们可以轻松获取名称,而无需提前了解它们的任何信息。

(3) 现在我们激活新视图。我在 MVVM ICommand.Execute() 方法中执行此操作:

public void Execute(object parameter)
{
    // Get main region
    var mainRegion = m_ViewModel.RegionManager.Regions["MainRegion"];

    // Clear region
    ModuleServices.ClearRegion(mainRegion);

    // Activate Module A view
    var moduleAView = mainRegion.GetView("ModuleA.ModuleAView");
    mainRegion.Activate(moduleAView);
}

希望这足以让您继续下去。就像我说的,我计划为 CodeProject 写一篇更完整的文章,其中包含演示代码。

大卫·维尼曼

前瞻系统

I found my answer. Here is the approach: Load all the modules at startup, then activate and deactivate views as you need them. I am going to write this issue up as a CodeProject article, but here is an outline of how to do it:

(1) In the module Initialize() method, add the module, but don't activate it:

public void Initialize()
{
    // Get main region
    var mainRegion = m_RegionManager.Regions["MainRegion"];

    // Load Module B
    var newView = new ModuleBView();
    mainRegion.Add(newView, "ModuleA.ModuleAView");
}

Note that the Add() method has two parameters. The second parameter is the name of the view, which we set to the value produced by the ToString() method of the view. We'll see why in the next step.

(2) When activating a view, we need to deactivate the previous view. But we may not know the name of the view, so we deactivate all active views:

public static void ClearRegion(IRegion region)
{
    // Get existing view names
    var oldViewNames = new List<string>();
    foreach (var v in region.Views)
    {
        var s = v.ToString();
        oldViewNames.Add(s);
    }

    // Remove existing views
    foreach (var oldViewName in oldViewNames)
    {
        var oldView = region.GetView(oldViewName);
        region.Deactivate(oldView);
    }
}

Since we set the name of each view equal to its ToString() value, we can get the names easily without knowing anything about them in advance.

(3) Now we activate the new view. I do it in an MVVM ICommand.Execute() method:

public void Execute(object parameter)
{
    // Get main region
    var mainRegion = m_ViewModel.RegionManager.Regions["MainRegion"];

    // Clear region
    ModuleServices.ClearRegion(mainRegion);

    // Activate Module A view
    var moduleAView = mainRegion.GetView("ModuleA.ModuleAView");
    mainRegion.Activate(moduleAView);
}

Hopefully, that will be enough to get you going. Like I said, I plan to do a more complete writeup, with demo code, for CodeProject.

David Veeneman

Foresight Systems

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