0
我想使用Visual Studio的Find in Files(或其他一些機制)來查找我的解決方案中的所有隱式WPF樣式(所有沒有Key的樣式並因此全局應用)。這如何實現?如何在我的解決方案中找到所有隱式WPF樣式?
我想使用Visual Studio的Find in Files(或其他一些機制)來查找我的解決方案中的所有隱式WPF樣式(所有沒有Key的樣式並因此全局應用)。這如何實現?如何在我的解決方案中找到所有隱式WPF樣式?
我們必須檢查Style
資源的Key
。如果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);
}
}
他們確實有一個Key,默認的一個。實際上,當你編寫類似''沒有指定KEY的東西時,它會以完全相同的方式工作。我可能是錯的,但我認爲Control('TextBox')的名稱是一個Key。 U可以查看文檔以找到確切的答案。 – 52hertz
你檢查了答案嗎? – AnjumSKhan