How can I confidently determine if a `ControlTemplate` is used in a WPF application?

How can I confidently determine if the following ControlTemplate used in a WPF application? The file name is "CheckBoxTemplates.xaml" and is located in a different assembly than the main application. Please note: there were no results when searching for the file name and resource key. In addition, finding a solution for a resource key is not reliable. Especially when there are five resource dictionary files containing the same key.

 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mwt="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Luna"> <Style x:Key="invertedCheckBox" TargetType="CheckBox"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="CheckBox" ... 

Additional Information

While user2250152 answser is true for the above XAML. It does not reliably determine if a style is being used. I am talking about this because when I used the technique for a different style, I found five resource dictionary files containing the same key. Therefore, we need to consider how I can reliably determine which style is used with specific keys using duplicate keys.

+6
source share
5 answers

ReSharper shows dead code. Although the detection algorithm is not perfect, it is a good start, allowing you to skip any style that it shows when it is used. For any style that appears to be unused, you can check whether it is really not used (for example, by searching through a solution).

+3
source

There are several options that I can think of:

  • Solution "development time": this is a search for each file for the existence of Style="{StaticResource StyleName}" or something similar. This will work in many cases, but is not automatic and not very reliable;

  • Runtime Solution: You can instantiate each control in the class library and iterate over each element in it by checking the currently assigned Style . This only works for styles that are set immediately, and not during events, etc .;

  • Solution "code": you can create your own static facade class, which is the entry point to get the style. Note that you are no longer allowed to use Style="{StaticResource StyleName}" , but Style="{StaticResource SomeStaticClass.StyleName}" . This will only work when you have a quality development team that is used for this working method. You can count the number of calls to the style throughout the program. If after a lot of time, Style is not yet used, perhaps the candidate may get rid of it.

+2
source

The unorthodox way to get there will create an attached property that prints, who uses the ControlTemplate :) think about it this way:

 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mwt="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Luna"> <Style x:Key="invertedCheckBox" TargetType="CheckBox"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="CheckBox"> <Border my:WhoIsMyDaddy="true" ...> 

my:WhoIsMyDaddy can be an attached property that fires the propertychanged event handler on initialization when set to true . After calling the handler, you can access the instance of the control that owns the attached property. In the end, just print whatever you want using this copy.

+2
source

You can use the VisualStudio and Find in Files tools. Try to find a style key, such as invertedCheckBox for the whole solution. This way you can determine where the style is used.

+1
source

Based on what you said

The file name is "CheckBoxTemplates.xaml" and is located in a different assembly than the main application. Please note: there were no results when searching for the file name and resource key.

You can be sure that this style is not used. As far as I know, automatic loading of any XAML files (except for application resources and themes) This means that any control cannot explicitly or implicitly refer to this style (which seems to be unloaded - if you don’t have any code for magic loading XAML files)

0
source

Source: https://habr.com/ru/post/980091/


All Articles