Skip to content

Instantly share code, notes, and snippets.

@m-gagne
Created September 11, 2013 02:50
Show Gist options
  • Save m-gagne/6518782 to your computer and use it in GitHub Desktop.
Save m-gagne/6518782 to your computer and use it in GitHub Desktop.
Windows 8 C# code sample that asks for a review of the app after 5 launches per app version
// -------------------------------------------------------------------
// Ask for a review after 5 starts of a new version of the Application
// -------------------------------------------------------------------
// Variable to hold the number of time the app started
int startedCount = 0;
// Variable to hold if the user actually accepted to rate the application
bool reviewedBefore = false;
// Variables to hold the current and the last version number that was started in a string
string currentVersion = string.Format("{0} - {1} - {2} - {3}", Package.Current.Id.Version.Major,
Package.Current.Id.Version.Minor, Package.Current.Id.Version.Build, Package.Current.Id.Version.Revision);
string lastVersionStarted = currentVersion;
// Check if the variables were stored in the Roaming Settings before loading them
if (Windows.Storage.ApplicationData.Current.RoamingSettings.Values.ContainsKey("startedCount"))
{
// If so, read it in the variable
startedCount = (int)Windows.Storage.ApplicationData.Current.RoamingSettings.Values["startedCount"];
}
if (Windows.Storage.ApplicationData.Current.RoamingSettings.Values.ContainsKey("reviewedBefore"))
{
// If so, read it in the variable
reviewedBefore = (bool)Windows.Storage.ApplicationData.Current.RoamingSettings.Values["reviewedBefore"];
}
if (Windows.Storage.ApplicationData.Current.RoamingSettings.Values.ContainsKey("lastVersionStarted"))
{
// If so, read it in the variable
lastVersionStarted = (string)Windows.Storage.ApplicationData.Current.RoamingSettings.Values["lastVersionStarted"];
}
// If the current version is equal to the last version started
if (currentVersion.Equals(lastVersionStarted))
// Increment the started count by 1
startedCount++;
else
// Reset the count because the new version might have fixed some problems or be better
startedCount = 0;
// And then save the variables in the Roaming Settings. Note that if the value pair was not
// created before, the method will create it automatically
Windows.Storage.ApplicationData.Current.RoamingSettings.Values["startedCount"] = startedCount;
Windows.Storage.ApplicationData.Current.RoamingSettings.Values["lastVersionStarted"] = currentVersion;
// If the app was started 5 times
if (startedCount == 5)
{
// Change the msg based on the fact that the application was reviewed before or not.
string msg;
if (!reviewedBefore)
msg = "Thank you for using [App Name] for a while now, would you like to review this app?";
else
msg = "Thank you for your ongoing use of [App Name]. We made changes since the last time you were asked to review it. Would you mind telling us what you think again?";
// Create a MessageDialog requesting a review
var md = new Windows.UI.Popups.MessageDialog(msg, "Please review my app");
// Build the 3 potential responses using the Commands collection
// If they say yes, send them to the store
md.Commands.Add(new Windows.UI.Popups.UICommand(
"Yes",
new Windows.UI.Popups.UICommandInvokedHandler(async (cmd) => {
// Find the FamilyName of the app package
string familyName = Package.Current.Id.FamilyName;
// Navigate to the store Review Page for the Application
// *** Note that the next line will launch the store app but will not find your app until
// *** it is published.
await Windows.System.Launcher.LaunchUriAsync(new Uri(string.Format("ms-windows-store:REVIEW?PFN={0}", familyName)));
// Change the status to track the fact that they agreed to a review
reviewedBefore = true;
// Store the reviewedBefore variable in local storage
Windows.Storage.ApplicationData.Current.RoamingSettings.Values["reviewedBefore"] = reviewedBefore;
})));
// If they say no, just close the dialog box
md.Commands.Add(new Windows.UI.Popups.UICommand(
"No",
new Windows.UI.Popups.UICommandInvokedHandler((cmd) => { })));
// If they say ask later, reset the count to 0 giving a grace period of 5 again
md.Commands.Add(new Windows.UI.Popups.UICommand(
"Ask Later",
new Windows.UI.Popups.UICommandInvokedHandler((cmd) => {
startedCount = 0;
Windows.Storage.ApplicationData.Current.RoamingSettings.Values["startedCount"] = startedCount;
})));
// Display the MessageBox asyncronously. ** Note that you may have to add the async keyword to
// the OnLaunched event definition above
await md.ShowAsync();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment