Native Trial Mode is an important feature of Windows Phone 7; there are several articles about simulating being in Trial Mode. My problem was that I had a fairly complex transition between trial mode and full mode I want the premium features to show up right away without requiring the user to restart my application. So I came up with this cute little hack to simulate the session when the user buys.
App Class
Since I want to change the trial state during the running of the program, I need to have a way to re-query trial state and communicate the change of status to the rest of the program. I could query the trial mode every time I ask, but that would be bad for performance and network chatter. Notice that there is a conditional compilation symbol called FAKESTORE, the code within the #if block is only compiled if the symbol is present. Also note the presence of the DoFakeActivate() method, this is called from the Buy Now code below.
#region Fake Store Code
public static bool IsTrial { get; private set; }
public static event EventHandler ApplicationActivated;
private void DetermineIsTrial()
{
bool armEvent = false;
bool newIsTrial = getIsTrilaFromLicenseInformation();
// I only want to fire the event when we are transitioning from
// trial to full license, I also want to have IsTrial reflect
// the current state, so I will remember to fire the event
if (!newIsTrial && IsTrial)
{
armEvent = true;
}
// Update the IsTrial status
IsTrial = newIsTrial;
// And fire the event (if necessary)
// This code will only be run once per user.
if (armEvent)
{
if (ApplicationActivated != null)
{
ApplicationActivated(new object(), EventArgs.Empty);
}
}
}
private static bool getIsTrilaFromLicenseInformation()
{
#if FAKESTORE
return !_fakeActivated;
#else
var license = new Microsoft.Phone.Marketplace.LicenseInformation();
return license.IsTrial();
#endif
}
#if FAKESTORE
private static bool _fakeActivated = false;
public void DoFakeActivate()
{
_fakeActivated = true;
DetermineIsTrial();
}
#endif
#endregion Fake Store Code
And I need to determine if the app is in trial mode when the app is launched and activated.
// Code to execute when the application is launching (eg, from Start)
// This code will not execute when the application is reactivated
private void Application_Launching(object sender, LaunchingEventArgs e)
{
DetermineIsTrial();
}
// Code to execute when the application is activated (brought to foreground)
// This code will not execute when the application is first launched
private void Application_Activated(object sender, ActivatedEventArgs e)
{
DetermineIsTrial();
}
Normally when you buy an app using MarketplaceDetailTask, the app is re activated when the user returns from the store. Because I call DetermineIsTrial from the Application_Activted, the app will be in paid for mode when the user returns.
Buy Now Form
When I run the app in Visual Studio, I get an error message from the Store and anyway, LicenseInformation.IsTrial will always return false in Visual Studio.
private void _btnBuyNow_Click(object sender, RoutedEventArgs e)
{
#if FAKESTORE
if (MessageBox.Show(
"Here we would send you to the app store.\n" +
"Do you want to simulate activation?",
"Fake App Store", MessageBoxButton.OKCancel)
== MessageBoxResult.OK)
{
var x = ((App)Application.Current);
x.DoFakeActivate();
}
#else
MarketplaceDetailTask marketplaceDetailTask = new MarketplaceDetailTask();
marketplaceDetailTask.Show();
#endif
}
It is necessary to call App. DoFakeActivate() to tell the App object to change the value of App.IsTrial. The user experience is radically different than normal
Setup Custom Configuration
I like to switch between developing / testing this part of the program and other tasks and I don’t want to have to alter actual code to change modes, so I create a custom configuration. To create a new configuration:
1. Build => Configuration Manager
2. Select <New…> from Active solution configuration:
3. Give it a name
4. Click OK
5. Right click on the StartUp Project => select Properties
6. Select the Build tab
7. Add “;FAKESTORE” to Conditional compilation symbols
No comments:
Post a Comment