WPF 预渲染一个列表,但不实际显示它

发布于 2024-10-08 04:10:52 字数 3413 浏览 0 评论 0原文

有没有办法在 WPF 中“预渲染”列表而不实际将其显示给用户?我有一个列表,第一次向用户显示时渲染速度相当慢(几秒钟)。

我想预渲染/预绘制列表,以便向用户显示列表所需的几秒钟并不明显。我希望在应用程序启动时发生这种情况 - 当我的启动屏幕显示时。

实现这一目标的最佳方法是什么?

编辑:

这是我的列表项样式。正如您所看到的,目标类型是 Button。该按钮还设置了一个 RoutedEvent(命令)。

<Style x:Key="Button_ProductFinder_Item" TargetType="Button">
    <Setter Property="Control.Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">

                <Grid Width="690" Height="181"   >

                    <Image Name="Up" Source="{StaticResource Img_ProductFinder_FriendUp}" Stretch="Uniform"/>
                    <Image Name="Down" Source="{StaticResource Img_ProductFinder_FriendDn}" Visibility="Hidden" Stretch="Uniform"/>

                    <Canvas Name="Clip" ClipToBounds="True" Height="162" Width="175" HorizontalAlignment="Left" Margin="0,10,0,0" >
                        <Image Name="DynamicFriend"  Stretch="UniformToFill" Canvas.Left="16" Canvas.Top="-10" HorizontalAlignment="Left"  Source="{Binding ImageSource}" Height="162" Width="162" />
                        <Canvas.RenderTransform>
                            <TransformGroup>
                                <TranslateTransform />
                            </TransformGroup>
                        </Canvas.RenderTransform>
                    </Canvas>

                    <TextBlock Name="DynamicName" Margin="210,69,0,0"  Style="{StaticResource ButtonFriend_Textstyle}"
                                   TextWrapping="Wrap" HorizontalAlignment="Left" Width="440" VerticalAlignment="Stretch" Height="Auto" 
                                   Text="{Binding ProductName}"  />
                </Grid>

                <ControlTemplate.Triggers>
                    <Trigger Property="IsPressed" Value="True">
                        <Setter TargetName="Up" Property="Visibility" Value="Hidden"/>
                        <Setter TargetName="Down" Property="Visibility" Value="Visible"/>
                        <Setter TargetName="Clip" Property="RenderTransform">
                            <Setter.Value>
                                <TransformGroup>
                                    <TranslateTransform Y="2"/>
                                </TransformGroup>
                            </Setter.Value>
                        </Setter>
                        <Setter TargetName="DynamicName" Property="RenderTransform">
                            <Setter.Value>
                                <TransformGroup>
                                    <TranslateTransform Y="2"/>
                                </TransformGroup>
                            </Setter.Value>
                        </Setter>
                        <Setter TargetName="DynamicName" Property="Foreground" Value="#FFFFFF"/>
                        <Trigger.EnterActions>
                            <ctrls:ButtonSoundPlayerAction SoundFile="buttonclick.wav"/>
                        </Trigger.EnterActions>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Is there a way to "pre-render" a list in WPF without actually showing it to the user? I have a list that renders fairly slowly ( a few seconds ) the first time it is displayed to the user.

I'd like to pre-render/pre-draw the list so that the few seconds it takes to display the list to the users is not noticeable. I'd like this to happen while the app is starting up - while my splash screen is displayed.

What is the best way to accomplish this?

EDIT:

Here is my list item style. As you can see the target type is a Button. The button also has a RoutedEvent (Command) set on it.

<Style x:Key="Button_ProductFinder_Item" TargetType="Button">
    <Setter Property="Control.Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">

                <Grid Width="690" Height="181"   >

                    <Image Name="Up" Source="{StaticResource Img_ProductFinder_FriendUp}" Stretch="Uniform"/>
                    <Image Name="Down" Source="{StaticResource Img_ProductFinder_FriendDn}" Visibility="Hidden" Stretch="Uniform"/>

                    <Canvas Name="Clip" ClipToBounds="True" Height="162" Width="175" HorizontalAlignment="Left" Margin="0,10,0,0" >
                        <Image Name="DynamicFriend"  Stretch="UniformToFill" Canvas.Left="16" Canvas.Top="-10" HorizontalAlignment="Left"  Source="{Binding ImageSource}" Height="162" Width="162" />
                        <Canvas.RenderTransform>
                            <TransformGroup>
                                <TranslateTransform />
                            </TransformGroup>
                        </Canvas.RenderTransform>
                    </Canvas>

                    <TextBlock Name="DynamicName" Margin="210,69,0,0"  Style="{StaticResource ButtonFriend_Textstyle}"
                                   TextWrapping="Wrap" HorizontalAlignment="Left" Width="440" VerticalAlignment="Stretch" Height="Auto" 
                                   Text="{Binding ProductName}"  />
                </Grid>

                <ControlTemplate.Triggers>
                    <Trigger Property="IsPressed" Value="True">
                        <Setter TargetName="Up" Property="Visibility" Value="Hidden"/>
                        <Setter TargetName="Down" Property="Visibility" Value="Visible"/>
                        <Setter TargetName="Clip" Property="RenderTransform">
                            <Setter.Value>
                                <TransformGroup>
                                    <TranslateTransform Y="2"/>
                                </TransformGroup>
                            </Setter.Value>
                        </Setter>
                        <Setter TargetName="DynamicName" Property="RenderTransform">
                            <Setter.Value>
                                <TransformGroup>
                                    <TranslateTransform Y="2"/>
                                </TransformGroup>
                            </Setter.Value>
                        </Setter>
                        <Setter TargetName="DynamicName" Property="Foreground" Value="#FFFFFF"/>
                        <Trigger.EnterActions>
                            <ctrls:ButtonSoundPlayerAction SoundFile="buttonclick.wav"/>
                        </Trigger.EnterActions>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

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

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

发布评论

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

评论(2

岁月染过的梦 2024-10-15 04:10:52
  • 检查什么是慢的。真的。如果它就像数据库填满了列表,那么你的问题就完全不成立了(因为性能不是 WPF 所涉及的)。

  • 列表有多大?我有一个立即渲染的 5000 个元素的列表。大多数是因为我的 4.0 虚拟列表仅呈现列表上的内容。您在填写列表时没有绑定数据,或者?

  • 最后,什么是渲染速度慢的地方?使用 wpf 分析器找出并告诉我们 - 因为在一天中您可能只是尝试优化错误的东西。

  • Check WHAT is slow. Really. Itf it is like the db filling up the list, your question is totally off (as the perforamnce is not WPF realted).

  • How large is the list? I have a list of 5000 elements rendering in a moments notice. Mosty because my 4.0 virtual list only renders what is on the list. You are not data bound while fillng up the list, or?

  • Finally, what is slow in the rndering? Use a wpf profiler to find out and tell us - because at the nd of the day you may simply try to optimize the wrong thing.

柒夜笙歌凉 2024-10-15 04:10:52

您始终可以将列表(或列表所在的整个窗口)设置为在其初始状态下折叠,然后当您从数据检索线程返回已完成的消息时取消折叠它。

我已经在其他几个程序中做到了这一点,而且效果非常好。

  1. 用户执行程序,启动屏幕显示,主窗口加载折叠。
  2. 数据检索线程执行、获取数据并报告已完成。
  3. 初始屏幕消失,显示主窗口并填充列表。

You could always set the list (or the whole window the list is contained in) to collapsed in it's initial state, and then un-collapse it when you get word back from your data retrieval thread that it is done.

I have done that with a couple of my other programs and it works pretty well.

  1. User executes the program, splash screen displays, and main window loads collapsed.
  2. Data retrieval thread executes, gets data, reports it is finished.
  3. Splash screen disappears, main window displays and list is populated.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文