Skip to content

Instantly share code, notes, and snippets.

@robertmclaws
Last active January 12, 2017 03:25
Show Gist options
  • Save robertmclaws/8232510 to your computer and use it in GitHub Desktop.
Save robertmclaws/8232510 to your computer and use it in GitHub Desktop.
Windows Phone 8 In-App Purchase End-to-End Example using Telerik Windows Phone Controls
public partial class App : Application
{
public static ListingInformation MarketplaceListing { get; set; }
//RWM: The rest of your startup code goes here...
}
//RWM: Add this Task to your page loaded routine...
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
Task.Run(async () =>
{
try
{
var listing = await CurrentApp.LoadListingInformationAsync();
App.MarketplaceListing = listing;
}
catch (Exception ex)
{
//throw ex;
}
});
//RWM: This is how I check for the specific IAP Ad IDs I have registered.
if (!Debugger.IsAttached && (CurrentApp.LicenseInformation.ProductLicenses["WP8PPA_RemoveAds"].IsActive ||
CurrentApp.LicenseInformation.ProductLicenses["WP8PPA_RemoveAds2"].IsActive ||
CurrentApp.LicenseInformation.ProductLicenses["WP8PPA_RemoveAds3"].IsActive))
{
//RWM: Change this to show the "Thanks for buying!" in-house ad for 10 seconds and then collapse.
this.AdRotatorControl.IsEnabled = false;
this.AdRotatorControl.Visibility = Visibility.Collapsed;
}
}
using System;
using System.Collections.Generic;
using Windows.ApplicationModel.Store;
namespace EnthusiastProgramAlerts.Models
{
public class ProductListingModel
{
public string Description { get; set; }
public string FormattedPrice { get; set; }
public Uri ImageUri { get; set; }
public IEnumerable<string> Keywords { get; set; }
public string Name { get; set; }
public string ProductId { get; set; }
public ProductType ProductType { get; set; }
public string Tag { get; set; }
public bool Purchased { get; set; }
public string PurchasedText {
get { return Purchased ? "Installed!" : ""; }
}
public ProductListingModel()
{
}
public ProductListingModel(ProductListing listing)
{
Description = listing.Description;
FormattedPrice = listing.FormattedPrice;
ImageUri = listing.ImageUri;
Keywords = listing.Keywords;
Name = listing.Name;
ProductId = listing.ProductId;
ProductType = listing.ProductType;
Tag = listing.Tag;
}
public ProductListingModel(ProductListing listing, IReadOnlyDictionary<string, ProductLicense> licenses)
: this(listing)
{
Purchased = licenses[listing.ProductId].IsActive;
}
}
}
<phone:PhoneApplicationPage
x:Class="EnthusiastProgramAlerts.Views.Upgrades"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:telerikPrimitives="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Primitives"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="Black"
Background="White"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d" d:DesignHeight="728" d:DesignWidth="480"
shell:SystemTray.IsVisible="False" Loaded="PhoneApplicationPage_Loaded">
<Grid x:Name="LayoutRoot" Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<StackPanel Orientation="Horizontal">
<Canvas Width="25" Height="25" Clip="F1 M 0,0L 76,0L 76,76L 0,76L 0,0" Margin="0,1,0,0">
<Path Width="21" Height="21" Canvas.Left="2" Canvas.Top="2" Stretch="Fill" Fill="#68217a" Data="F1 M 17,23L 34,20.7738L 34,37L 17,37L 17,23 Z M 34,55.2262L 17,53L 17,39L 34,39L 34,55.2262 Z M 59,17.5L 59,37L 36,37L 36,20.5119L 59,17.5 Z M 59,58.5L 36,55.4881L 36,39L 59,39L 59,58.5 Z " UseLayoutRounding="False"/>
</Canvas>
<TextBlock Text="Windows Phone 8"
Margin="4,0,0,0"
Foreground="#68217a"
FontSize="{StaticResource PhoneFontSizeNormal}"
FontFamily="{StaticResource PhoneFontFamilySemiBold}" />
<TextBlock Text="Preview Alerts"
Margin="4,0,0,0"
Foreground="DimGray"
FontSize="{StaticResource PhoneFontSizeNormal}"
FontFamily="{StaticResource PhoneFontFamilySemiBold}" />
</StackPanel>
<TextBlock x:Name="PageTitle" Text="upgrades" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}" Foreground="Purple"/>
</StackPanel>
<Grid Grid.Row="1" Margin="0,0,0,0">
<Grid.Resources>
<DataTemplate x:Key="SlideViewItemTemplate">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<Image Grid.Row="0" Grid.RowSpan="2" Margin="6,0,6,0" Stretch="Uniform" Source="{Binding ImageUri}"/>
<TextBlock Grid.Row="1" HorizontalAlignment="Center" Canvas.ZIndex="1" Text="{Binding PurchasedText}" FontWeight="Bold" Foreground="White" />
</Grid>
</DataTemplate>
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition Height="250"/>
<RowDefinition/>
</Grid.RowDefinitions>
<telerikPrimitives:RadSlideView Grid.Row="0"
x:Name="slideView"
AdjacentItemsPreviewMode="Both"
ItemTemplate="{StaticResource SlideViewItemTemplate}"
IsFilmstripModeEnabled="True" SelectionChanged="slideView_SelectionChanged"/>
<StackPanel Grid.Row="1" Margin="24,20,24,12">
<TextBlock Text="{Binding ElementName=slideView,Path=SelectedItem.Name}" FontSize="{StaticResource PhoneFontSizeLarge}" FontFamily="{StaticResource PhoneFontFamilySemiBold}" TextWrapping="Wrap" HorizontalAlignment="Center"/>
<TextBlock Text="{Binding ElementName=slideView,Path=SelectedItem.FormattedPrice}" FontSize="{StaticResource PhoneFontSizeLarge}" FontFamily="{StaticResource PhoneFontFamilySemiBold}" TextWrapping="Wrap" HorizontalAlignment="Center"/>
<TextBlock Text="{Binding ElementName=slideView,Path=SelectedItem.Description}" Margin="0,6,0,0" FontSize="{StaticResource PhoneFontSizeNormal}" TextWrapping="Wrap" HorizontalAlignment="Center"/>
</StackPanel>
</Grid>
</Grid>
<phone:PhoneApplicationPage.ApplicationBar>
<shell:ApplicationBar IsVisible="True" IsMenuEnabled="false" BackgroundColor="#68217a" Mode="Default" ForegroundColor="White">
<shell:ApplicationBarIconButton IconUri="/Assets/LeftArrow.png" Click="ApplicationBarIconButton_Click" Text="back"/>
<shell:ApplicationBarIconButton IconUri="/Assets/appbar.cart.png" x:Name="CartButton" Text="buy" Click="CartButton_Click"/>
<shell:ApplicationBarIconButton IconUri="/Assets/RightArrow.png" Click="ApplicationBarIconButton_Click_1" Text="next"/>
</shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>
</phone:PhoneApplicationPage>
using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using Windows.ApplicationModel.Store;
using EnthusiastProgramAlerts.Models;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
namespace EnthusiastProgramAlerts.Views
{
public partial class Upgrades : PhoneApplicationPage
{
public Upgrades()
{
InitializeComponent();
}
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
BindData();
}
private void ApplicationBarIconButton_Click(object sender, EventArgs e)
{
this.slideView.MoveToPreviousItem();
}
private void ApplicationBarIconButton_Click_1(object sender, EventArgs e)
{
this.slideView.MoveToNextItem();
}
private async void CartButton_Click(object sender, EventArgs e)
{
var selectedUpgrade = (ProductListingModel)slideView.SelectedItem;
if (selectedUpgrade == null) return;
try
{
await CurrentApp.RequestProductPurchaseAsync(selectedUpgrade.ProductId, true);
if (CurrentApp.LicenseInformation.ProductLicenses[selectedUpgrade.ProductId].IsActive)
{
MessageBox.Show("Congratulations! You've purchased this upgrade!");
BindData();
if (selectedUpgrade.ProductType == ProductType.Consumable)
{
CurrentApp.ReportProductFulfillment(selectedUpgrade.ProductId);
}
}
}
catch (Exception ex)
{
//You might want to log here, but probably unnecessary.
}
}
private void slideView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var selectedUpgrade = (ProductListingModel)this.slideView.SelectedItem;
if (selectedUpgrade != null)
{
((ApplicationBarIconButton)ApplicationBar.Buttons[1]).IsEnabled = !CurrentApp.LicenseInformation.ProductLicenses[selectedUpgrade.ProductId].IsActive;
}
}
private void BindData()
{
var listings = App.MarketplaceListing.ProductListings.Select(c => new ProductListingModel(c.Value, CurrentApp.LicenseInformation.ProductLicenses)).ToList();
this.slideView.ItemsSource = listings;
if (listings.Count == 1)
{
((ApplicationBarIconButton)ApplicationBar.Buttons[0]).IsEnabled = false;
((ApplicationBarIconButton)ApplicationBar.Buttons[2]).IsEnabled = false;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment