如何冻结所有可冻结的WPF对象? [英] How to freeze all freezable WPF objects?

查看:51
本文介绍了如何冻结所有可冻结的WPF对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想冻结窗口中的所有可冻结对象。(以获得更好的性能)

为此,我使用了如下几个循环:

    foreach (Brush item in FindLogicalChildren<Brush>(myWin))
                  if( item != null && item.CanFreeze)item.Freeze();

    foreach (Transform item in FindLogicalChildren<Transform>(myWin))
                   if( item != null && item.CanFreeze)item.Freeze();

    foreach (Geometry item in FindLogicalChildren<Geometry>(myWin))
                   if( item != null && item.CanFreeze)item.Freeze();

但它不起作用。

如何对任何可冻结的WPF对象调用Freeze()

编辑:

我刚刚意识到FindLogicalChildren找不到任何东西,所以它无法工作。

EDIT2:

如何使用一个循环对任何可冻结对象调用Freeze()。

请帮帮我。

推荐答案

您说得对,如果一切都冻结,性能会得到真正的提升。

您可以在XAML中完成此操作。

在所有资源词典中,添加ice命名空间:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:ice="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options">
然后,对于每个可冻结的XAML元素,冻结它。示例:

<SolidColorBrush ice:Freeze="True" x:Key="GlyphDisabledFillBrush" Color="{StaticResource Color_005}"/>
<LinearGradientBrush ice:Freeze="True" x:Key="PendingOrderPositiveBrush"  EndPoint="8,8" StartPoint="0,0" SpreadMethod="Repeat"  MappingMode="Absolute">
    <GradientStop ice:Freeze="True" Color="{StaticResource PendingOrderLightPositiveColor}" Offset="0"/>
    <GradientStop ice:Freeze="True" Color="{StaticResource PendingOrderLightPositiveColor}" Offset="0.44"/>
    <GradientStop ice:Freeze="True" Color="{StaticResource PendingOrderDarkPositiveColor}" Offset="0.44"/>
    <GradientStop ice:Freeze="True" Color="{StaticResource PendingOrderDarkPositiveColor}" Offset="0.6"/>
    <GradientStop ice:Freeze="True" Color="{StaticResource PendingOrderLightPositiveColor}" Offset="0.6"/>
    <GradientStop ice:Freeze="True" Color="{StaticResource PendingOrderLightPositiveColor}" Offset="1"/>
</LinearGradientBrush>

不冻结元素的好处

拥有非冻结画笔的唯一好处是我们可以在运行时更改主题。如果我们不担心主题改变,那么我们可以通过冻结所有笔刷来获得良好的性能提升。冻结元素也几乎是我们支持具有独立调度程序的多线程窗口的唯一方法。

这篇关于如何冻结所有可冻结的WPF对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆