简体   繁体   English

TabControl - 切换选项卡时将 UserControl TabItems 保留在内存中

[英]TabControl - Keeping UserControl TabItems in Memory When Switching Tabs

I had a tab control where each tab item was a custom made user control.我有一个选项卡控件,其中每个选项卡项都是自定义的用户控件。 The issue there was whenever the program would launch, each individual control would be initialized and loaded.问题是每当程序启动时,每个单独的控件都会被初始化和加载。 Then, when switching to the tab, it would have to reload again.然后,当切换到选项卡时,它必须再次重新加载。

I've since changed how the tab items are loaded as shown below.我已经更改了选项卡项目的加载方式,如下所示。 This prevents the user controls from initializing and loading until they're clicked on (which is preferred):这可以防止用户控件初始化和加载,直到它们被点击(这是首选):

<TabControl>
  <TabItem>
      <TabItem.ContentTemplate>
          <DataTemplate>
              <local:ctlHome />
          </DataTemplate>
      </TabItem.ContentTemplate>
  </TabItem>
  <TabItem>
      <TabItem.ContentTemplate>
          <DataTemplate>
              <local:ctlTwo />
          </DataTemplate>
      </TabItem.ContentTemplate>
  </TabItem>
  ...
</TabControl>

When I first rolled this out, every time I would switch to a new tab, it would call the constructor -> Unload the previous tab -> Load the new tab , which is great.当我第一次推出它时,每次我切换到一个新选项卡时,它都会调用构造函数 -> 卸载上一个选项卡 -> 加载新选项卡,这很棒。 It would only call the constructor once, and whenever I re-selected the tab, it would just unload -> load as expected.它只会调用一次构造函数,每当我重新选择选项卡时,它只会卸载 - >按预期加载

Now, every time I select a tab, it calls the constructor and re-initializes the entire control.现在,每次我选择一个选项卡时,它都会调用构造函数并重新初始化整个控件。 I'm not sure why it's no longer keeping the tab in memory.我不确定为什么它不再将标签保留在内存中。

Is there any way I can keep the user controls in memory once they've initialized using this method?使用此方法初始化后,有什么方法可以将用户控件保留在内存中?

If you use a DataTemplate with a ContentControl , only the initially visible control gets loaded :如果您将DataTemplateContentControl一起使用,则只会加载最初可见的控件:

<TabControl xmlns:s="clr-namespace:System;assembly=mscorlib">
    <TabControl.Items>
        <s:String>home</s:String>
        <s:String>two</s:String>
    </TabControl.Items>
    <TabControl.ContentTemplate>
        <DataTemplate>
            <ContentControl>
                <ContentControl.Style>
                    <Style TargetType="ContentControl">
                        <Setter Property="Content">
                            <Setter.Value>
                                <local:ctlHome />
                            </Setter.Value>
                        </Setter>
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding}" Value="two">
                                <Setter Property="Content">
                                    <Setter.Value>
                                        <local:ctlTwo />
                                    </Setter.Value>
                                </Setter>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </ContentControl.Style>
            </ContentControl>
        </DataTemplate>
    </TabControl.ContentTemplate>
</TabControl>

Both controls get initialized immediately though, but this shouldn't be an issue since you should not perform anything heavy in the constructors.虽然这两个控件都会立即初始化,但这应该不是问题,因为您不应该在构造函数中执行任何繁重的操作。 Move any initialization logic to the Loaded event handler or similar.将任何初始化逻辑移动到Loaded事件处理程序或类似的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM