Tips, tricks, and guides for developing on modern Windows platforms
Because universal Windows apps combine two apps in one, it helps to know some tricks, so here are three to help avoid that extra complexity, and streamline your universal Windows app development.
If you’re totally new to universal Windows apps, check out my “Hello World” example to get started, then check back here for the tips.
You should mostly keep platform-specific code in its own project (e.g. phone code in the phone project), but you can use conditional compilation when you want minor code differences in shared code files.
For Windows Phone code use (C#):
// enter Windows Phone-only code here
Visual Basic.Net uses “If” and “End If” (all the words capitalized):
// insert Windows Phone-only code here
For Windows Store code use (C#):
# if WINDOWS_APP
// Windows-only code here
Here's an example:
As you probably know (or guessed), code is only compiled when it matches the output app platform. You also may have noticed that in the image above the code is coloured differently for the two #if blocks. Read on to see why that is...
Visual Studio disables code that is excluded by the conditional compilation #if blocks. If you're working on the Windows app version and have a code block encased in:
that code will not compile. You can set the current context of a universal app from the dropdown under the file tab at the top of the editor window (A):
In the above example the current context is the Windows version of the app, so the code in the #if WINDOWS_PHONE_APP block is disabled and the code in the #if WINDOWS_APP block is active (B, you can tell which is active because it's in the correct colour, while the inactive code is in the same colour as commented out code).
This functionality is only available if the code file is in the shared project (and therefore exists in both the output apps).
Simply open the dropdown (A) and select the other platform to switch context.
When I switch this example over to the phone version it looks like this:
Code that is disabled by a conditional compilation block is effectively ignored, so that code could have errors in, and you wouldn't be notified of them until you try to compile the app with that code active.
If you're new to Visual Studio it may not be obvious how to choose which version of the universal app you want to build/run/deploy. Your universal app solution has (at least) three projects - Windows app, Windows Phone app, and the shared project. Whichever out of the Windows and Windows Phone apps is selected as your startup project will build/run/deploy when you choose one of those options.
The current startup project's name is displayed in bold, as you can see in the following example, where the Windows version of the app is the startup project:
To switch to the phone version you just right-click on the Windows Phone project and choose Set as StartUp Project:
Now any build/run actions will act on the phone version (e.g. if you deploy the app it will deploy to your phone).
I hope you found these little tips helpful. I'll add more as I discover them.