我做了一个demo,后面跟着你,你可以参考一下。
xaml 部分:
<Page.Resources>
<local:MyConverter x:Key="myconverter"></local:MyConverter>
</Page.Resources>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Windows81:AdMediatorControl x:Name="AdMediator" HorizontalAlignment="Left" Height="250" Id="AdMediator-Id-FA61D7FD-4F5F-445D-AB97-DB91618DBC70" Margin="557,287,0,0" VerticalAlignment="Top" Width="300" Visibility="{Binding IsVisible,Converter={StaticResource myconverter}}" />
<Button Name="btn1" Content="Remove ad" Click="RemoveAd" Visibility="Visible" />
</Grid>
后面的代码:
public class Recording : INotifyPropertyChanged
{
private bool isVisible;
public Recording()
{
}
public bool IsVisible
{
get
{
return isVisible;
}
set
{
if (value != isVisible)
{
isVisible = value;
OnPropertyChanged("IsVisible");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
private Recording recording;
public MainPage()
{
this.InitializeComponent();
Init();
recording = new Recording();
recording.IsVisible = false;
this.DataContext = recording;
}
private async void Init()
{
StorageFile proxyFile = await Package.Current.InstalledLocation.GetFileAsync("in-app-purchase.xml");
await CurrentAppSimulator.ReloadSimulatorAsync(proxyFile);
}
public async Task<bool> PurchaseProductAsync(string productId)
{
try
{
var purchase = await CurrentAppSimulator.RequestProductPurchaseAsync(productId);
return purchase.Status == ProductPurchaseStatus.Succeeded || purchase.Status == ProductPurchaseStatus.AlreadyPurchased;
}
catch (Exception)
{
// The purchase did not complete because an error occurred.
return false;
}
}
private async void RemoveAd(object sender, RoutedEventArgs e)
{
recording.IsVisible = await this.PurchaseProductAsync("product2");
}
}
public class MyConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
if (value is Boolean && (bool)value)
{
return Visibility.Collapsed;
}
else
{
return Visibility.Visible;
}
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
我已经测试过了,购买产品后,广告将不再显示。
另外我想建议你使用另一种不绑定的方法。
xaml 部分:
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Windows81:AdMediatorControl x:Name="AdMediator" HorizontalAlignment="Left" Height="250" Id="AdMediator-Id-FA61D7FD-4F5F-445D-AB97-DB91618DBC70" Margin="557,287,0,0" VerticalAlignment="Top" Width="300" Visibility="Visible" />
<Button Name="btn1" Content="Remove ad" Click="Button_Click" Visibility="Visible" />
</Grid>
后面的代码:
namespace AdmediatorTest
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
Init();
LicenseInformation licenseInformation = CurrentAppSimulator.LicenseInformation;
if (!licenseInformation.ProductLicenses["product2"].IsActive)
{
btn1.Visibility = Visibility.Visible;
}
else
{
btn1.Visibility = Visibility.Collapsed;
}
}
private async void Init()
{
StorageFile proxyFile = await Package.Current.InstalledLocation.GetFileAsync("in-app-purchase.xml");
await CurrentAppSimulator.ReloadSimulatorAsync(proxyFile);
}
private async void Button_Click(object sender, RoutedEventArgs e)
{
LicenseInformation licenseInformation = CurrentAppSimulator.LicenseInformation;
if (!licenseInformation.ProductLicenses["product2"].IsActive)
{
try
{
await CurrentAppSimulator.RequestProductPurchaseAsync("product2");
if (licenseInformation.ProductLicenses["product2"].IsActive)
{
AdMediator.Visibility = Visibility.Collapsed;
}
else
{
AdMediator.Visibility = Visibility.Visible;
}
}
catch (Exception)
{
//rootPage.NotifyUser("Unable to buy " + productName + ".", NotifyType.ErrorMessage);
}
}
else
{
//rootPage.NotifyUser("You already own " + productName + ".", NotifyType.ErrorMessage);
}
}
}
}
另外我发现了一个awesome video关于IAP后删除广告的内容,你也可以参考一下。