PageRenderTime 143ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/Chapter7/AppStore/MainPage.xaml.cs

https://bitbucket.org/syncfusion/windowsapps
C# | 231 lines | 148 code | 26 blank | 57 comment | 15 complexity | 3de87e5b23f391d74bc0a0f08c22bd85 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using Windows.Foundation;
  6. using Windows.Foundation.Collections;
  7. using Windows.UI.Xaml;
  8. using Windows.UI.Xaml.Controls;
  9. using Windows.UI.Xaml.Controls.Primitives;
  10. using Windows.UI.Xaml.Data;
  11. using Windows.UI.Xaml.Input;
  12. using Windows.UI.Xaml.Media;
  13. using Windows.UI.Xaml.Navigation;
  14. // This #if approach allows swapping out to the "real" app when deploying to the store.
  15. // Note that this sample is geared around the idea of loading app information via pre-canned XML,
  16. // which is not part of the post-ship Windows Store experience.
  17. #if DEBUG
  18. using CurrentAppAccessor = Windows.ApplicationModel.Store.CurrentAppSimulator;
  19. #else
  20. using CurrentAppAccessor = Windows.ApplicationModel.Store.CurrentApp;
  21. #endif
  22. // The Basic Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234237
  23. namespace WindowsStoreAppsSuccinctly
  24. {
  25. /// <summary>
  26. /// A basic page that provides characteristics common to most applications.
  27. /// </summary>
  28. public sealed partial class MainPage : WindowsStoreAppsSuccinctly.Common.LayoutAwarePage
  29. {
  30. public MainPage()
  31. {
  32. this.InitializeComponent();
  33. UpdateOptionsControls();
  34. }
  35. /// <summary>
  36. /// Populates the page with content passed during navigation. Any saved state is also
  37. /// provided when recreating a page from a prior session.
  38. /// </summary>
  39. /// <param name="navigationParameter">The parameter value passed to
  40. /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested.
  41. /// </param>
  42. /// <param name="pageState">A dictionary of state preserved by this page during an earlier
  43. /// session. This will be null the first time a page is visited.</param>
  44. protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
  45. {
  46. }
  47. /// <summary>
  48. /// Preserves state associated with this page in case the application is suspended or the
  49. /// page is discarded from the navigation cache. Values must conform to the serialization
  50. /// requirements of <see cref="SuspensionManager.SessionState"/>.
  51. /// </summary>
  52. /// <param name="pageState">An empty dictionary to be populated with serializable state.</param>
  53. protected override void SaveState(Dictionary<String, Object> pageState)
  54. {
  55. }
  56. private void HandleLoadFullAppProfileClick(Object sender, RoutedEventArgs e)
  57. {
  58. // Load a proxy file that simulates a full-feature app
  59. LoadProxyFile("WindowsStoreProxy_FullApp.xml");
  60. }
  61. private void HandleLoadTrialAppProfileClick(Object sender, RoutedEventArgs e)
  62. {
  63. // Load a proxy file that simulates a trial-mode app
  64. LoadProxyFile("WindowsStoreProxy_TrialApp.xml");
  65. }
  66. private void HandleLoadInAppPurchaseProfileClick(Object sender, RoutedEventArgs e)
  67. {
  68. // Load a proxy file that simulates an app that supports in-app purchases
  69. LoadProxyFile("WindowsStoreProxy_InAppPurchase.xml");
  70. }
  71. private async void HandleUpgradeTrialAppClick(Object sender, RoutedEventArgs e)
  72. {
  73. // Request a purchase upgrade from the Trial Mode to the Full Feature Mode
  74. try
  75. {
  76. var receiptText = await CurrentAppAccessor.RequestAppPurchaseAsync(true);
  77. // In practice, the resuls in these exceptions can be of interest.
  78. }
  79. catch (System.Runtime.InteropServices.COMException)
  80. {
  81. // Happens with E_FAIL
  82. }
  83. catch (ArgumentException)
  84. {
  85. // Happens with E_INVALIDARG result
  86. }
  87. catch (OutOfMemoryException)
  88. {
  89. // Happens with E_OUTOFMEMORY result
  90. }
  91. }
  92. private async void HandlePurchaseProductClick(Object sender, RoutedEventArgs e)
  93. {
  94. var selectedItem = AvailableProductsList.SelectedItem as Windows.ApplicationModel.Store.ProductListing;
  95. if (selectedItem != null)
  96. {
  97. var productId = selectedItem.ProductId;
  98. try
  99. {
  100. // Request an in-app pruchase of a specific product
  101. var receiptText = await CurrentAppAccessor.RequestProductPurchaseAsync(productId, true);
  102. // In practice, the resuls in these exceptions can be of interest.
  103. }
  104. catch (System.Runtime.InteropServices.COMException)
  105. {
  106. // Happens with E_FAIL
  107. }
  108. catch (ArgumentException)
  109. {
  110. // Happens with E_INVALIDARG result
  111. }
  112. catch (OutOfMemoryException)
  113. {
  114. // Happens with E_OUTOFMEMORY result
  115. }
  116. }
  117. }
  118. private async void LoadProxyFile(String proxyProfileFileToLoad)
  119. {
  120. // Initialize WindowsStoreProxy.xml
  121. // Create the fixed folder for proxy files
  122. //var folder = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFolderAsync("Microsoft\\Windows Store\\ApiData", Windows.Storage.CreationCollisionOption.OpenIfExists);
  123. // Load the
  124. //var file = await Package.Current.InstalledLocation.GetFileAsync("WindowsStoreProxy.xml");
  125. //var storeProxyFile = await folder.CreateFileAsync(proxyProfileFileToLoad, Windows.Storage.CreationCollisionOption.ReplaceExisting);
  126. //await baselineStoreProxyFile.CopyAndReplaceAsync(storeProxyFile);
  127. // Load the proxy XML file to use
  128. var baselineStoreProxyFile = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///" + proxyProfileFileToLoad));
  129. // Update the simulator to take its values from those defined in the XML file
  130. await CurrentAppAccessor.ReloadSimulatorAsync(baselineStoreProxyFile);
  131. // Update the UI to show the current app licensing status
  132. UpdateStatusControls();
  133. UpdateOptionsControls();
  134. }
  135. private Windows.ApplicationModel.Store.LicenseInformation _licenseInformation;
  136. private Windows.ApplicationModel.Store.ListingInformation _listingInformation;
  137. private async void HandleLicenseInformationLicenseChanged()
  138. {
  139. // When the current license is changed (through an app store purchase or trial upgrade) update the UI accordingly
  140. await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
  141. {
  142. UpdateStatusControls();
  143. UpdateOptionsControls();
  144. });
  145. }
  146. private async void UpdateStatusControls()
  147. {
  148. AppIdText.Text = CurrentAppAccessor.AppId.ToString();
  149. LinkText.Text = CurrentAppAccessor.LinkUri.ToString();
  150. // Information that describes the application's listing in the Store
  151. _listingInformation = await CurrentAppAccessor.LoadListingInformationAsync();
  152. AppNameText.Text = _listingInformation.Name;
  153. AppDescriptionText.Text = _listingInformation.Description;
  154. AgeRatingText.Text = _listingInformation.AgeRating.ToString();
  155. MarketText.Text = _listingInformation.CurrentMarket;
  156. PriceText.Text = _listingInformation.FormattedPrice;
  157. // TODO - replace the simple count display with either an items control or a combo box to show the actual items...
  158. AvailableProductsText.Text = _listingInformation.ProductListings.Count().ToString();
  159. // Information about the application's current licensing status
  160. if (_licenseInformation != null) _licenseInformation.LicenseChanged -= HandleLicenseInformationLicenseChanged;
  161. _licenseInformation = CurrentAppAccessor.LicenseInformation;
  162. if (_licenseInformation != null) _licenseInformation.LicenseChanged += HandleLicenseInformationLicenseChanged;
  163. LicenseActiveText.Text = _licenseInformation.IsActive.ToString();
  164. LicenseTrialText.Text = _licenseInformation.IsTrial.ToString();
  165. LicenseExpirationText.Text = _licenseInformation.ExpirationDate.ToString("G");
  166. // TODO - replace the simple count display with either an items control or a combo box to show the actual items...
  167. LicenseProductsText.Text = _licenseInformation.ProductLicenses.Count().ToString();
  168. // Information that describes purchases made inthe application so far
  169. var appReceipt = await CurrentAppAccessor.GetAppReceiptAsync();
  170. //var productReceipt = await Windows.ApplicationModel.Store.CurrentAppSimulator.GetProductReceiptAsync(productId);
  171. // Set the Ad Control visibility based on the current Demo Mode
  172. AdControl.Visibility = _licenseInformation.IsTrial ? Visibility.Visible : Visibility.Collapsed;
  173. }
  174. private void UpdateOptionsControls()
  175. {
  176. // Update the status of the "upgrade trial" button
  177. UpgradeTrialButton.IsEnabled = _licenseInformation != null && _licenseInformation.IsTrial;
  178. // Popuplate the in-app-purchase combo box with the list of available products that can be purchased
  179. AvailableProductsList.Items.Clear();
  180. if (_listingInformation != null && _listingInformation != null)
  181. {
  182. var availableListingItems = _listingInformation.ProductListings
  183. .Where(x => _licenseInformation.ProductLicenses.ContainsKey(x.Value.ProductId) == false)
  184. .ToList();
  185. foreach (var listingItem in availableListingItems)
  186. {
  187. AvailableProductsList.Items.Add(listingItem.Value);
  188. }
  189. AvailableProductsList.DisplayMemberPath = "Name";
  190. }
  191. // Select the first item in the list by default
  192. if (AvailableProductsList.Items.Any())
  193. {
  194. AvailableProductsList.IsEnabled = true;
  195. AvailableProductsList.SelectedItem = AvailableProductsList.Items[0];
  196. }
  197. else
  198. {
  199. AvailableProductsList.IsEnabled = false;
  200. }
  201. // Update the status of the "purchase product" button
  202. PurchaseProductButton.IsEnabled = AvailableProductsList.SelectedItem != null;
  203. }
  204. }
  205. }