次の方法で共有


Monetizing Windows 8 apps: examples from my book

If you are looking for code examples for Professional Windows 8 Programming: Application Development with C# and XAML book, specifically for the monetization section, you can get them directly from Wiley:

Chapter 11 code for Pro Windows 8 Programming
563.02 KB
Click to Download

What I’ve done in that example is helping you put all monetization techniques together: from unlocking the trial, to using in-app purchase. There’re some neat ideas implemented with the example, for example I use an MVVM model to wrap store licensing schema.

I recommend reading the book first, about the ideas implemented here. This snippet illustrates the first technique of using an MVVM model to wrap the store objects. This may be necessary if you need to expose them later through data binding.

 

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CatalogShoppingAppCS.Data;
using Windows.ApplicationModel.Core;
using Windows.ApplicationModel.Store;
using Windows.Foundation;
using Windows.UI.Core;

namespace CatalogShoppingAppCS
{
    public class AppLicenseDataModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private bool _licensed = false;
        private string _price;

        public AppLicenseDataModel()
        {
            if (CurrentAppSimulator.LicenseInformation.IsTrial)
            {
                CurrentAppSimulator.LicenseInformation.LicenseChanged += OnLicenseChanged;
                GetListingInformationAsync();
            }
            else
                _licensed = true;
        }

        private async void GetListingInformationAsync()
        {
            var listing = await CurrentAppSimulator.LoadListingInformationAsync();
            _price = listing.FormattedPrice;
        }

        private async void OnLicenseChanged()
        {
            if (!CurrentAppSimulator.LicenseInformation.IsTrial)
            {
                _licensed = true;
                CurrentAppSimulator.LicenseInformation.LicenseChanged -= OnLicenseChanged;

                // need this to the license change occurs on a different thread
                // to update UI bound elements from the data model
                CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    if (IsLicensed)
                    {
                        var groups = CatalogDataModel.GetGroups("AllGroups");
                        foreach (var group in groups)
                            foreach (var item in group.Items)
                                item.IsAdDisplayed = false;
                    }

                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs(String.Empty));
                    }
                });
            }
        }

        public bool IsLicensed
        {
            get { return _licensed; }
        }

        public bool IsTrial
        {
            get { return !_licensed; }
        }

        public string LicenseInfo
        {
            get
            {
                if (!_licensed)
                    return "Trial Version";
                else
                    return ("Valid until " + CurrentAppSimulator.LicenseInformation.ExpirationDate.LocalDateTime.ToString("dddd, MMMM d, yyyy"));
            }
        }

        public string FormattedPrice
        {
            get
            {
                if (!String.IsNullOrEmpty(_price))
                    return "Upgrade to the full version for " + _price;
                else
                    return "Upgrade to the full Version";
            }
        }
    }
}

Professional Windows 8 Programming: Application Development with C# and XAML