Windows Phone 7 – Speed Up Development with ...
Often times there is a routine, repetitive task that you perform during development to help you meet an end goal. For instance, a lot of applications have a login screen. Just a basic screen that requires you to enter your username / password, then click “Login”. Well, if you’re writing an application, pretty soon this entering of your credentials gets old. In order to relieve this pain, I’d like to revisit an old friend, the preprocessor directive.
A preprocessor directive can be used for conditional compilation. In other platforms, this conditional uses a separate preprocessor. However, C# the directives are processed as if there were only one. Altogether, C# has 14 preprocessor directives. To show how you can use a preprocessor directive to speed up your development, lets consider the login screen again.
If you have a basic page, you may be tempted to enter the credentials in your constructor like the following:
public Login()
{
InitializeComponent();
if (Microsoft.Devices.Environment.DeviceType == Microsoft.Devices.DeviceType.Emulator)
{
usernameTextBox.Text = "testUserName";
passwordBox.Password = "testPassword";
}
}
The problem here is the .xap file. If you publish this application, your test credentials will actually be stored in the code. Because of this, someone could use a tool like RedGate’s .NET Reflector and see your credentials. If you’re a solo developer, you may actually be using your real credentials which could open a personal pandora’s box for you. To help you avoid this unfortunate situation, you could improve the previous code by doing the following:
public Login()
{
InitializeComponent();
#if DEBUG
usernameTextBox.Text = "testUserName";
passwordBox.Password = "testPassword";
#endif
}
This approach will remove your credential code when you compile your application in Release mode. Something you should do before you submit your application to the Windows Phone Marketplace. The reason your credentials are removed is because of the special DEBUG directive. This directive is defined by Visual Studio be default. Basically, it means anytime your application is compiled under “Debug” configuration, the code in-between the #if DEBUG -> #endif directives will be included.


