Update your Windows Phone App to Support Theme Changes

If you’ve ever built a Windows Phone 7 or Windows Phone 8 application, you may have found that your app looks great when building it in Visual Studio. However, as soon as you attempt to change the theme of the application, the design would not appear correct.

One resource on MSDN that you may find useful is the article titled Theme Resources for Windows Phone. This article shows several tables that explain the built-in resource names for themes. Besides setting a brush or color within your application to one of the resource values, you may be interested in knowing the theme color (light or dark) or the foreground color chosen within the settings.

Windows Phone does not provide a resource that contains a value as to which theme is chosen. Rather, the visibility of one (or both) of the theme visibility resources must be checked. An example of this is shown below:

   1: Visibility isVisible = 
   2:    (Visibility)Application.Current.Resources["PhoneLightThemeVisibility"];

The foreground is a bit easier. In my case, I usually obtain a brush. The reason for this is that I’m usually using this to programmatically set the background color of one or more controls to this color. Rather than obtaining the foreground brush, I’m really interested in the foreground accent brush. The reason for this is that the accent is the color choice that is made in settings. I can get this be doing the following:

   1: SolidColorBrush Foreground = 
   2:    (SolidColorBrush)Application.Current.Resources["PhoneAccentBrush"];

After doing this once, I quickly realized that this should go into a common class library that I’ve begun to include on every phone project. So, I created a class called theme.cs which is available in a GitHub Gist (please feel free to submit revisions or suggestions as necessary). The complete Gist can also be copied from below:

One of the reasons you may need to know the theme is when you are deciding what logo to use. For instance, you wouldn’t want a white logo to appear on the light background. So, to see the Theme.cs in action, I would first update the App.xaml.cs file to call the static method by using Theme.Detect(). This should be done in both the Application_Launching and Application_Activated methods. Then, update each of the pages that use the logo to set the source in a manner similar to the following:

   1: Logo.Source = new BitmapImage(
   2:                  new Uri(
   3:                     Theme.CurrentTheme == Theme.ThemeColor.Dark ? 
   4:                        "/Assets/Logo-Light.png" : 
   5:                        "/Assets/Logo-Dark.png", 
   6:                        UriKind.Relative));

Another way to accomplish this would be to consistently use the same naming convention and location for assets such as logos. If this is the case, you can move this to the Theme.cs class and create a new static property with a private setter that automatically populates the bitmap.

Using PowerShell to Obtain CD/DVD Disk Information

As the years go by, more and more research, information, and documents can be found online. However, several business verticals still use CDs and DVDs to access research content, backup documents, and share files. In every position that I’ve ever held, I have been asked by someone to locate a disk. I had an old WMI script that would grab the information, place it in a text file on a machine, and notify me via a net message that the file was ready. This was great and did the job, but I wanted to see how I can do the same using PowerShell (and WMI).

When you install PowerShell, it’s capabilities can be expanded through modules. One of the modules that is found in the base installation of PowerShell is the Microsoft.PowerShell.Management module. This module contains a method called Get-WmiObject. This method will use WMI to gain access to certain machine information. In this case, we can obtain the drive information by using the class Win32_LogicalDisk and filtering the drive type to 2 and 5. We’ll choose 2 and 5 as this will return only the Removable Disc and Compact Disc drives. The other drive types are shown below:

Value Meaning
0 Unknown
1 No Root Directory
2 Removable Disk
3 Local Disk
4 Network Drive
5 Compact Disk
6 RAM Disk

From each of these drives, we’ll be able to obtain the DriveID (drive letter) and VolumeName (CD/DVD label or name of the removable device). Finally, we’ll pass this information into a custom psobject to display a result table such as below:

Get-ADComputerCDRomInfo

The final script appears as below:

For this script to work, remote RPC needs to be enabled, it should be executed from an administrator account, and it should either be signed or executed by allowing (temporarily) unsigned scripts to be executed. This script has been tested to work with PowerShell v1 so that it will work with legacy versions of PowerShell. With later versions of PowerShell (and potentially with this version), this may be able to be refactored.

Feel free to submit revisions to this Gist.

What came first? The baby or the app

So today is the big day where my wife is being induced with our fourth child. Given the past, today’s experience should last between 6-10 hours. Knowing I’ll be hearing monitors in the background, I figured I should do some work. Since I haven’t spent the time to complete an app for Windows Phone yet I figured I would challenge myself. I’ll be live blogging this experience today and am up against mother nature. My goal is to see what comes first: my new baby boy or my new Windows Phone app. Wish me luck (on both fronts)!

7am   It’s beginning – I have an idea on paper. Notice the attention to detail.

           WP_20130521_002

Professional ASP.NET 4.5 in C# and VB

On May 3rd, Wrox Press released Professional ASP.NET 4.5 in C# and VB. I co-authored this book with two Microsoft employees (Pranav Rastogi and Scott Hanselman)as well as two Microsoft MVPs (Christian Wenz and Todd Miranda). The book is packed with the most current information about ASP.NET 4.5. It is also the only book I am aware of that explains One ASP.NET. The foreword for this book is written by Scott Hunter of the ASP.NET team.

There are ten parts, 35 chapters, 7 appendixes, and nearly 1500 pages in this book. Here’s the table of contents for the book:

  • Part 1: ASP.NET Fundamentals
    • Chapter 1: One ASP.NET
    • Chapter 2: HTML5 and CSS3 Design with ASP.NET
    • Chapter 3: ASP.NET Web Forms Structure
  • Part 2: Controls
    • Chapter 4: ASP.NET Server Controls
    • Chapter 5: ASP.NET Web Server Controls
    • Chapter 6: Validation Server Controls
    • Chapter 7: User and Server Controls
  • Part 3: Data Access
    • Chapter 8: Data Binding
    • Chapter 9: Model Binding
    • Chapter 10: Querying with LINQ
    • Chapter 11: Entity Framework
    • Chapter 12: ASP.NET Dynamic Data
    • Chapter 13: Working with Services
  • Part IV: Providers
    • Chapter 14: Introduction to the Provider Model
    • Chapter 15: Extending the Provider Model
  • Part V: ASP.NET Features
    • Chapter 16: Working with Master Pages
    • Chapter 17: Site Navigation
    • Chapter 18: Personalization
    • Chapter 19: Membership and Role Management
    • Chapter 20: Security
  • Part VI: Application State
    • Chapter 21: State Management
    • Chapter 22: Caching
  • Part VII: Client-Side Development
    • Chapter 23: ASP.NET AJAX
    • Chapter 24: AJAX Control Toolkit
    • Chapter 25: jQuery
    • Chapter 26: Real-time Communication
    • Chapter 27: Developing Websites with Mobile in Mind
  • Part VIII: Application Configuration and Deployment
    • Chapter 28: Configuration
    • Chapter 29: Debugging and Error Handling
    • Chapter 30: Modules and Handlers
    • Chapter 31: Asynchronous Communication
    • Chapter 32: Building Global Applications
    • Chapter 33: Packaging and Deploying ASP.NET
  • Part IX: Additional ASP.NET Technologies
    • Chapter 34: ASP.NET MVC
    • Chapter 35: ASP.NET Web Pages and Razor
  • Part X: Appendixes
    • Appendix A: Migrating Older ASP.NET Projects
    • Appendix B: COM Integration
    • Appendix C: ASP.NET Ultimate Tools
    • Appendix D: Administration and Management
    • Appendix E: Dynamic Types and Languages
    • Appendix F: ASP.NET Online Resources
    • Appendix G: Visual Studio Extensibility with NuGet

You can pick up a copy at Amazon by visiting http://jasong.us/amzn-aspnet45. If you do pick up a copy, be sure to post a review of the book on your own blog, on Amazon, or via the forums on Wrox.com.