c# - 在 wpf 项目中查找未卡住的画笔

标签 c# wpf freeze brush

我想弄清楚如何在应用程序资源中找到所有不是 Freeze="true" 的 Brush 对象。

到目前为止我所做的是:

var temp = this.FindLogicalChildren<Brush>();
var resourceDictionary = Application.Current.Resources;

var resources = GetResources(resourceDictionary);

var brushes = resources.Cast<DictionaryEntry>()
     .Where(e => e.Value is Brush).Where(e => !(e.Value as Brush)?.IsFrozen ?? false);
foreach (var entry in brushes)
   ....

但是,这种方法无法满足标准。知道我在这里错过了什么吗?

最佳答案

所以我找到了this cached website这似乎提供了一个答案:

public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj)
    where T : DependencyObject
    {
        if (depObj != null)
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
                if (child != null && child is T)
                {
                yield return (T)child;
                }
                foreach (T childOfChild in FindVisualChildren<T>(child))
                {
                yield return childOfChild;
                }
            }
        }
    }
    foreach (Freezable item in FindVisualChildren<Freezable>(myWin))
    if( item != null && item.CanFreeze)item.Freeze();

关于c# - 在 wpf 项目中查找未卡住的画笔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47785377/

相关文章:

c# - 如何在网络上的所有机器上安装 .NET Windows Forms 应用程序?

javascript - 使用 axios 从 vue 上传 XML 文件到 asp .net core

wpf - 在 WPF 的右侧而不是下方弹出的垂直菜单

c# - 如何在折线图中显示附加点信息?

c# - 我应该使用 Prism 来开发简单的桌面应用程序吗? C#

javascript - 在不引起递归的情况下将两个 knockout 变量连接在一起

JavaScript 卡住浏览器

.net - 当我在调试器中暂停一个不相关的程序时,为什么 Windows 资源管理器搜索会卡住

c# - 如何编写采用可变数量参数的 C# 方法?

c# - 使用 C# 创建 Azure Web 应用程序