Friday, September 21, 2012

C++ Direct2D (XAML) App Walkthrough: Common

The common folder is (surprise) where you put common code and xaml files. In just about every project your first move should be to move this out to a library. I called this library Common just to keep the convention but I've seen it called "Core" or somethings that indicates this is where files should go that are to be shared between libraries and projects. There is only one file to basic template and that is the StandardStyles.xaml file.


This file is a resource dictionary. If you are new to xaml development you may not be familiar with resource dictionaries. They are kinda like a css file for xaml. They define global styles for all of your xaml files in the app. That is why this file has this comment at the top:
This file contains XAML styles that simplify application development.
    These are not merely convenient, but are required by most Visual Studio project and item templates. Removing, renaming, or otherwise modifying the content of these files may result in a project that does not build, or that will not build once additional pages are added.  If variations on these styles are desired it is recommended that you copy the content under a new name and modify your  private copy.
You dont really need to change this file. Simply create your own and reference it in the app.xaml. Like this resource has been.


                    Styles that define common aspects of the platform look and feel
                    Required by Visual Studio project and item templates
                <ResourceDictionary Source="Common/StandardStyles.xaml"/>


Another important thing to note is that if you move common out to a library you will need to update the ResourceDictionary.Souce to include the assembly name like this:

<ResourceDictionary Source="ms-appx:///Common/Files/Style.xaml" />
You can find the explanation for this here.

I am pretty bad at using resource dictionaries but I know when I should be doing it. The test is the same as code. If you are copy pasting xaml you should consider a resource dictionary. Don't forget to add the dictionaries to the app.xaml. I have wasted countless hours debugging styles that were not added to the app.xaml.

No comments:

Post a Comment