4. Verifying Trial and Full Mode
Creating trial applications
is the central theme of this article and we have spent the first part
of it looking at various issues that may arise as part of trialing an
application. When you allow application trials, one of your most
important tasks is to ensure that certain application features are
accessible to full license holders only; otherwise, there would be no
reason to buy an application.
For the Currency Converter application, you must ensure that that the MoreStuff.xaml
page is visible only when the application runs with a full license. You
have already seen the code that performs that check; in this section,
you must verify that the trial mode indeed behaves as expected. You will
learn the technique of writing your own version of the LicenseInformation class just discussed to validate the trial mode of an application.
With the Currency Converter application open, right-click the project name in Solution Explorer, select Add => New Item, and then select Class from the list of available items. Name the new class "LicenseInformation" and click OK.
Make the LicenseInformation
class look like the one here:
public sealed class LicenseInformation
{
// Fields
private boolm_fIsTrial = true;
// Methods
public boolIsTrial()
{
int num = 0;
if (num != 0)
{
this.m_fIsTrial = true;
}
return this.m_fIsTrial;
}
// Nested Types
internal static class NativeMethods
{
// Fields
internal const intS_FALSE = 1;
internal const intS_OK = 0;
}
}
You will test the trial mode shortly, right after we put finishing touches on our Currency Converter application.
5. Adding Finishing Touches
You are nearly ready to test
the Currency Converter application—just a few items remain. In this
section, we will complete the application and take it for a test drive.
Follow these steps to get there.
Before an application is
functional, it needs to know what currency to convert to what. We have
created two list boxes inside the MainPage.xaml
file to allow the user to make her selection. To keep things simple in
the first version, you include only three currencies: the US dollar, the
euro, and the Russian ruble. When an application loads, you need to
load list boxes with those currencies.
Open MainPage.xaml.cs and paste the following LoadCurrencies method inside the MainPage() constructor.
private void LoadCurrencies()
{
lstConvertFrom.Items.Add(svcCurrencyConverter.Currency.USD);
lstConvertFrom.Items.Add(svcCurrencyConverter.Currency.EUR);
lstConvertFrom.Items.Add(svcCurrencyConverter.Currency.RUB);
lstConvertTo.Items.Add(svcCurrencyConverter.Currency.USD);
lstConvertTo.Items.Add(svcCurrencyConverter.Currency.EUR);
lstConvertTo.Items.Add(svcCurrencyConverter.Currency.RUB);
}
MoreStuff.xaml
needs code to perform calculations on the currency rates passed in and
entered into the application. This code belongs inside
thebtnCalculateDamage_Click event.
In Design view, double-click the Calculate Damage button and replace the btnCalculateDamage_Click event code with the following:
private void btnCalculateDamage_Click(object sender, RoutedEventArgs e)
{
decimal decTotalToReceive;
decimal decTotalAccordingToConversionRate;
decTotalToReceive = Convert.ToDecimal(txtExchangeRateQuoted.Text) *
decTotalToConvert;
decTotalAccordingToConversionRate = Convert.ToDecimal(dblExchgRate) *
decTotalToConvert;
txtDamageExplained.Text = "With exchange rate quoted, you will receive " +
decTotalToReceive.ToString() + "\r\n";
txtDamageExplained.Text = txtDamageExplained.Text + "Given market exchange
rate, you should receive " + decTotalAccordingToConversionRate.ToString() + "\r\n";
txtDamageExplained.Text = txtDamageExplained.Text + "You lose " +
(decTotalAccordingToConversionRate - decTotalToReceive).ToString();
}
Finally, Upgrade.xaml
needs code to bring up the Windows Phone Marketplace and load the
application review page, which will enable the user to purchase a full
version of the application if the user elects to do so.
Add the following using directive to the top of the Upgrade.xaml.cs file:
using Microsoft.Phone.Tasks;
Next, bring up the Upgade.xaml page in design mode and double-click the "Yes, Upgrade" button. Make that button's click event look like the following:
private void btnUpgrade_Click(object sender, RoutedEventArgs e)
{
MarketplaceReviewTask marketplaceReviewTask = new MarketplaceReviewTask();
marketplaceReviewTask.Show();
}
You're now done writing code
for the Currency Converter application. The application should compile
and run if you press F5 now. If, for some reason, there are errors
preventing an application from launching, it's best to compare your code
to code available for download for this article.
Assuming the code runs,
the current value of the IsTrial method returned by our own
implementation of the LicenseInformation class is true. Therefore, if we
run the application and click the More Stuff button, we should see a
message prompting us to upgrade to the full version of an application.
That is the expected behavior. Let's go ahead and change the value of
m_fIsTrial to false. We should now see the "More Stuff" screen, just as
we expected the application with full license to behave.
We can also verify that
the program works as expected by entering values and asking it to
convert those from one currency to another. For instance, today, as
shown in Figure 5,
$345 is only 267.86 euros. To get that output, type "345" in the
"Amount to Convert" text box, select "USD" from the "Convert From" list
box and select "EUR" from the "Convert To" list box. Then, press the
Convert button. Assuming that the connection to the Internet is
available, you should get results that are similar.
While the Currency
Converter application is functional, it can stand many improvements,
particularly in the area of validating user input. For instance, an
application throws an error if the user tries to go to the "More Stuff"
screen without entering a value in the "Amount to Convert" text box.
Addressing this and other issues is left as an exercise for the reader.