我一直认为generic.xaml样式被合并到Application.Current.Resources中,但它们不是。
generic.xaml样式存储在哪里?
如何在代码中访问它们?(不是xaml)
更新:我知道一般的c#语法来访问ResourceDictionary中的显式或隐式样式。这个问题只是关于模板化控件的/Themes/Generic.xaml的样式。
发布于 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.Resources或Application.Current.Resources访问它。
发布于 2018-06-20 12:44:06
Application.Current.FindResource(key)发布于 2022-05-19 04:06:49
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的样式控件。我想这会对你有帮助的祝你好运。
https://stackoverflow.com/questions/50948365
复制相似问题