【问题标题】:Same module multiple times as TabItems与 TabItems 多次使用相同的模块
【发布时间】:2009-01-16 19:55:06
【问题描述】:

这是我的场景:

  1. 带有 1 个 TabControl 和 1 个名为 MenuRegion 的区域的外壳
  2. MenuRegion 包含每个可用模块(应用程序)的按钮。

我想使用 Prism (Composite Application Library for WPF) 实现以下功能:单击其中一个按钮时,我需要向 TabControl 添加一个新的 TabItem,并加载相应模块(应用程序)的单个实例在此 TabItem 内。 一个模块可能会在 TabControl 中出现多次。


非常感谢您的回答。但我不相信你在使用 Prism (http://www.codeplex.com/CompositeWPF) 是吗?我的问题与 Prism 更相关,我现在对其进行了编辑以使其更加清晰。

在 Prism 中,您可以将模块的视图动态加载到区域中。我不确定如何在我的场景中执行此操作,因为这些区域是动态设置的。我将如何命名它们?

谢谢!

【问题讨论】:

    标签: wpf prism


    【解决方案1】:

    我是这个 PRISM 世界的新手(1 周经验 :)))并且有同样的要求! 首先,您必须从 here 获取 Regionextensions。

    我(可能是你的)问题的解决方案如下:

    • 有 2 个区域(菜单和 tabcontrol - 用于类似 mdi 的行为)

    • tabitem header必须准备一个关闭按钮(绑定到关闭这个tabitem的命令——实际上隐藏了这个tabitem)

    • 将事件从菜单项发送到应该加载视图的模块(我已按需实例化模块)。在模块的初始化方法中订阅菜单项发送的事件。在事件处理方法中,您只需重新显示 tabitem

    如果这是对您的抽象,我可以向您发送我开发的框架应用程序。

    【讨论】:

      【解决方案2】:

      我们做了类似的事情,尽管我们已经创建了标签项(没有内容)并根据需要显示/隐藏。当标签项被选中时,我们加载标签内容。

      private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
          {
              if (e.OriginalSource != sender) return;
      
              TabControl tabControl = (TabControl)sender;
              TabItem tabItem = (TabItem)tabControl.SelectedItem;
      
              if (!tabItem.HasContent)
                  AddTabContent(tabItem); // This will cause a refresh once the content is loaded.
              else
                  Refresh(tabItem);
          }
      
      
      
      private void AddTabContent(TabItem tabItem)
          {
              IOptimusPage page = tabItem.Tag as IOptimusPage;
      
              //This allows lazy loading of controls
              if (page != null)
              {
                  if (!tabItem.HasContent)
                  {
                      CustomerEngagementUserControl control = page.GetControl(DataContext as CustomerEngagementUIObject, Services);
      
                      tabItem.Content = control;
                  }
              }
      
          }
      

      标签项内容在标签项标签中指定,使用负责创建内容的页面。

      <TabItem
      Header="Personal Background"
      Style="{StaticResource FirstBreadcrumbTabItem}"
      x:Name="PersonalBackgroundTab">
          <TabItem.Tag>
              <Pages:FfnaPersonalBackgroundPage />
          </TabItem.Tag>
      </TabItem>
      

      页面创建控件。

      class FfnaPersonalBackgroundPage : IOptimusPage
      {
          #region IOptimusPage Members
      
          public CustomerEngagementUserControl GetControl(CustomerEngagementUIObject dataContext, CustomerEngagementServices services)
          {
              CustomerEngagementUserControl control = new FfnaPersonalBackgroundControl();
              control.DataContext = dataContext;
              control.Services = services;
              return control;
          }
      
          #endregion
      }
      

      您可以使用类似的技术来动态创建标签项。

      【讨论】:

        【解决方案3】:

        我知道回复已经很晚了,但我正在做类似的事情,虽然还没有得到完整的解决方案。

        此代码发生在我在演示者中处理的按钮的单击事件上。该模块在配置文件中定义。

        ModuleInfo moduleInfoObject = this.moduleEnumerator.GetModule("ModuleA"); 
        
        Assembly assembly = this.LoadAssembly(moduleInfoObject);
        
        Type type = assembly.GetType(moduleInfoObject.ModuleType);
        IModule aModule = this.CreateModule(type);                                    
        aModule.Initialize();  
        
        
        // - - - -Helper Methods - - - -
        // - - - LoadAssembly - - -       
        private Assembly LoadAssembly(ModuleInfo moduleInfo)
            {            
                string assemblyFile = moduleInfo.AssemblyFile;
                assemblyFile = this.GetModulePath(assemblyFile);
        
                FileInfo file = new FileInfo(assemblyFile);
                Assembly assembly;
        
                try
                {
                    assembly = Assembly.LoadFrom(file.FullName);
                } 
                catch (Exception ex)
                {
                    throw new ModuleLoadException(null, assemblyFile, ex.Message, ex);
                } 
        
                return assembly;
        
            } // LoadAssembly(moduleInfo)
        
        
        // - - - CreateModule - - -
        private IModule CreateModule(Type type)
            {
                return (IModule)containerFacade.Resolve(type);            
            } // CreateModule(type)
        
        
        // - - - GetModulePath - - -
        private string GetModulePath(string assemblyFile)
            {
                if (String.IsNullOrEmpty(assemblyFile))
                {
                    throw new ArgumentNullException("assemblyFile");
                } // if
        
                if (Path.IsPathRooted(assemblyFile) == false)
                {
                    assemblyFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, assemblyFile);
                } // if
        
                return assemblyFile;
            } // GetModulePath(assemblyFile)
        

        【讨论】:

          猜你喜欢
          • 2011-05-21
          • 1970-01-01
          • 1970-01-01
          • 2016-06-12
          • 2018-09-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多