.NET Framework 4.6 – AppContext

Compatibility switches is a new feature in the .NET Framework 4.6 which will be useful for Windows App store applications.  The .NET Framework only provided compile time features for supporting different versions.  While this is a good way to support multiple features, it is not dynamic enough with the different formats and features that Windows applications will need to support today without using a custom solution.

For just a quick overview, the compilation version features are either using Preprocessor Directives and complication symbols, and the System.Diagnostics Conditional features seen below.

Using Preprocessor Directives

By default, Visual Studio will define the “DEBUG”  compilation symbol.  You can see this under the project properties, Build tab.


If you have a “Premium” version that you want to support you can use a directive to disable the code for now.  If you see the image below, it shows that the Debug version is currently going to be executed, and it will grey out the currently non defined Premium version.


If you want to enable the Premium features you will need to add a Premium complication symbol to either the Project settings, or define one in code, such as:


Or you can define in the project settings



Using ConditionalAttribute

Another way to write your code instead off using Preprocessor directives is to use the System.Diagnostics.ConditionalAttribute.


Using AppContext from .NET Framework 4.6

For the more dynamic approach, the new NET Framework 4.6 AppContext will allow an easy way to track features and preferences set in the application.  You just have to label a feature, and whether it is enabled or not.  Then when you need to do something for this feature, such as creating a menu, you just retrieve the value.  Here is a simple example that sets the camera feature enabled for an application.  Then later when the menu is being created, it will only execute the camera menu method if the feature was enabled.


Hopefully with the overview and introduction to the new AppContext, you can see how to provide features easily to your users.

Leave a Reply

Your email address will not be published. Required fields are marked *