简体   繁体   English

异步加载TabItem

[英]Load a TabItem asynchronously

Is it possible to load tabitems asynchronously? 是否可以异步加载Tabitem? In my case, I have a tab control that contains a few tabitems. 就我而言,我有一个包含几个选项卡的选项卡控件。 When the user clicks on one tabitem, the application freezes for a short time, to load the tabitem. 当用户单击一个Tabitem时,应用程序冻结一小段时间以加载Tabitem。

Now I want to change that. 现在我要更改它。 When the user clicks on a tabitem, an animation will be shown and after the tabitem is loaded completely, the tabitem will be shown. 当用户单击一个Tabitem时,将显示一个动画,并且在完全加载Tabitem之后,将显示该Tabitem。

Anyone an idea? 有人知道吗?

It depends on what is taking a long time to load. 这取决于花费很长时间来加载。

If the data is causing the pause, then load the data asynchronously and display a placeholder while the data loads. 如果数据导致暂停,则异步加载数据并在数据加载时显示占位符。 Once the data has finished loading, you can change it to your actual content. 数据加载完成后,您可以将其更改为实际内容。

Here's one example, 这是一个例子

<ContentControl>
    <ContentControl.Style>
        <Style TargetType="{x:Type ContentControl}">
            <Setter Property="ContentTemplate" Value="{StaticResource TabContentTemplate}" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsLoading}" Value="True">
                    <Setter Property="ContentTemplate" Value="{StaticResource LoadingTemplate}" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ContentControl.Style>
</ContentControl>

Where your RelayCommand that changes the SelectedItem might look something like this: 您的RelayCommand更改SelectedItem RelayCommand可能看起来像这样:

void ChangeTab(ITabViewModel newTab)
{
    // Set loading flag and set tab as Selected
    newTab.IsLoading = true;
    SelectedTab = newTab;

    // Create async task
    var asyncTask = new Task(() => newTab.LoadData());

    // This runs after task is finished running
    asyncTask.ContinueWith(p => newTab.IsLoading = false));

    // Start async task
    asyncTask.Start();
}

If it's your UI that is taking a while to load, then users might just have to deal with an initial load time the first time the tab loads, however you can stop the delay when switching to an already loaded tab by extending the TabControl to keep it from destorying it's TabItems . 如果是您的用户界面需要花费一些时间来加载,则用户可能只需要在标签首次加载时处理初始加载时间,但是您可以通过扩展TabControl来保持切换到已加载的标签,从而停止延迟避免破坏它的TabItems

The default behavior of WPF's TabControl is to unload a TabItem when you switch to a different tab and reload it when you go back. WPF的TabControl的默认行为是在切换到其他选项卡时卸载TabItem ,并在返回时重新加载它。 This workaround stores the ContentPresenter of an already loaded tab, and will reload the saved tab instead of loading a new tab when switching back. 此解决方法将存储已加载选项卡的ContentPresenter ,并且在切换回时将重新加载已保存的选项卡,而不是加载新的选项卡。

In most cases, the UI delay is caused by a very large number of UI elements on the screen. 在大多数情况下,UI延迟是由屏幕上大量的UI元素引起的。 Try and reduce the number of elements on the tab as well. 尝试减少标签上的元素数量。 For example, Labels consist of more elements than TextBlocks , so unless you need label-specific capabilities like selection try to use Labels instead of TextBlocks 例如, LabelsTextBlocks包含更多的元素,因此除非您需要标签特定的功能(例如选择),否则请尝试使用Labels代替TextBlocks

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

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