简体   繁体   中英

How is the default template of a custom control applied with out using “Themes\Generic.xaml ?”

I created a custom content control with its default template. The template though is not located in the location "Themes\\Generic.xaml" as it is supposed to be. Instead I placed it in a separate resource dictionary with a different name. It is merged along with other resource dictionaries in the App.xaml resource dictionary.

Running the code yielded no errors and the control is visible. I tested the application on other machines and the code ran just fine , I would like to point out that these machines are development machines.

Testing the application on the client machines (non development) then revealed some strange behaviour. The custom control was not visible; it would only be visible on 1 out 3 client machines.

I was able to fix the problem by putting the template in "Themes\\Generic.xaml".

My question is how was it able to work without having to put the template in "Themes\\Generic.xaml" ? I have searched on Google but I could find any clear cut explaination .

Thank you

For your templates to be applied they need to be present in Themes\\Generic.xaml. The good news is that you may merge them here, which I find to be good practice, and makes your code cleaner. Often my generic.xaml consists of only this, one X.Generic.xaml for all your cust. controls.

Themes/Generic.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
...>
    <!-- Your custom controls split into separate files -->
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="/YourNamespace;component/Themes/ControlA.Generic.xaml" />
        <ResourceDictionary Source="/YourNamespace;component/Themes/ControlB.Generic.xaml" />
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM