我想使用Visual Studio的文件查找(或其他一些机制)来查找我的解决方案中的所有隐式WPF样式(所有没有Key的样式因此自我应用全球范围内)。如何实现这一目标?
答案 0 :(得分:1)
我们必须检查Key
资源的Style
。如果Key
的值为System.Type
类型且其基类为System.Windows.FrameworkElement
,则表示它是隐式Style
。
static List<Style> _styles = new List<Style>();
private void Button_Click(object sender, RoutedEventArgs e)
{
// Check for Application
var appResDict = Application.Current.Resources;
foreach (DictionaryEntry entry in appResDict)
{
if ((entry.Key is System.Type) && ((Type)entry.Key).IsSubclassOf(typeof(System.Windows.FrameworkElement)))
_styles.Add((Style)entry.Value);
}
// Check for Window
var resDict = this.Resources;
foreach (DictionaryEntry entry in resDict)
{
if ((entry.Key is System.Type) && ((Type)entry.Key).IsSubclassOf(typeof(System.Windows.FrameworkElement)))
_styles.Add((Style)entry.Value);
}
// Check for all other controls
MainWindow.EnumVisual(this);
MessageBox.Show(_styles.Count.ToString());
}
// Enumerate all the descendants of the visual object.
static public void EnumVisual(Visual myVisual)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(myVisual); i++)
{
// Retrieve child visual at specified index value.
Visual childVisual = (Visual)VisualTreeHelper.GetChild(myVisual, i);
// Check for implicit style
if (childVisual is FrameworkElement)
{
FrameworkElement elem = (FrameworkElement)childVisual;
var resDict = elem.Resources;
foreach (DictionaryEntry entry in resDict)
{
if ((entry.Key is System.Type) && ((Type)entry.Key).IsSubclassOf(typeof(System.Windows.FrameworkElement)))
_styles.Add((Style)entry.Value);
}
}
// Enumerate children of the child visual object.
EnumVisual(childVisual);
}
}