首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在代码中访问generic.xaml样式

如何在代码中访问generic.xaml样式
EN

Stack Overflow用户
提问于 2018-06-20 12:31:59
回答 3查看 1K关注 0票数 1

我一直认为generic.xaml样式被合并到Application.Current.Resources中,但它们不是。

generic.xaml样式存储在哪里?

如何在代码中访问它们?(不是xaml)

更新:我知道一般的c#语法来访问ResourceDictionary中的显式或隐式样式。这个问题只是关于模板化控件的/Themes/Generic.xaml的样式。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-06-21 06:11:23

您不能从(Program )\Windows\10\ DesignTime\CommonConfiguration\Neutral\UAP\Generic文件夹直接访问应用程序中的generic.xaml资源,它们不会复制到您的应用程序中,因此它们不是应用程序的一部分,您的应用程序不能将generic.xaml中的资源直接用作通用资源字典。

Windows不使用这些物理文件(包括generic.xaml)进行运行时查找。这就是为什么它们专门存在于DesignTime文件夹中,而且默认情况下它们不会被复制到应用程序中。相反,这些资源字典作为Windows运行时本身的一部分存在于内存中,您的应用程序对主题资源(或系统资源)的XAML资源引用在运行时解析。参见资源字典结构中的主题资源部分。

因此,如果您想使用generic.xaml中的资源。您应该将其作为通用ResourceDictionary添加到应用程序中,然后可以从Page.ResourcesApplication.Current.Resources访问它。

票数 4
EN

Stack Overflow用户

发布于 2018-06-20 12:44:06

代码语言:javascript
复制
Application.Current.FindResource(key)
票数 0
EN

Stack Overflow用户

发布于 2022-05-19 04:06:49

代码语言:javascript
复制
    public T GetVisualChild<T>(System.Windows.DependencyObject parent, System.Func<T, bool> predicate) where T : System.Windows.Media.Visual
    {
        int numVisuals = System.Windows.Media.VisualTreeHelper.GetChildrenCount(parent);
        for (int i = 0; i < numVisuals; i++)
        {
            System.Windows.DependencyObject v = (DependencyObject)System.Windows.Media.VisualTreeHelper.GetChild(parent, i);
            T child = v as T;
            if (child == null)
            {
                child = GetVisualChild<T>(v, predicate);
                if (child != null)
                {
                    return child;
                }
            }
            else
            {
                if (predicate(child))
                {
                    return child;
                }
            }
        }
        return null;
    }

按钮btnTopMost= GetVisualChild(这里,v => v.Name == "btnTopMost");

"this“是您的MainWindow类实例。"btnTopMost“在Generic.xaml中的定义类似于x:Name="btnTopMost”。在WPF项目中,使用此代码,您可以访问Generic.xaml的样式控件。我想这会对你有帮助的祝你好运。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50948365

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档