我正在创建一个WinRT(Windows 8.1和Windows Phone 8.1)应用,其中我在其中一个页面中放置了AdDuplex广告控件。
应用的用户可以选择删除广告(使用IAP)。完成后,我会从页面ViewModel中将Adduplex广告控件的Visibility
设置为Collapsed
。
这部分工作正常;但是,过了一段时间,当用户仍然在页面上时,AdDuplex广告控制会突然再次显示并开始展示广告。
一开始,我认为这是使用CurrentAppSimulator
时IAP的行为,虽然它对我没有意义,因为代码中没有任何内容可以对许可证更改作出反应,从而设置控件回到Visible
。然而,我为我的"测试license.IsActive
NoAd“产品并获得true
,表明许可证有效。
以下是我的代码的简化部分:
MyPage.xaml
<ad:AdControl
AdUnitId="{StaticResource AdUnitId}"
AppKey="{StaticResource AdAppKey}"
IsTest="True"
CollapseOnError="True"
Visibility="{Binding IsNoAdPurchased, Converter={StaticResource BooleanToVisibilityInvertedConverter}}"/>
MyPageViewModel.cs
private async void RemoveAd()
{
this.IsNoAdPurchased = await this.storeService.PurchaseProductAsync(Products.NoAd);
}
StoreService.cs
#if DEBUG
using StoreCurrentApp = Windows.ApplicationModel.Store.CurrentAppSimulator;
#else
using StoreCurrentApp = Windows.ApplicationModel.Store.CurrentApp;
#endif
public sealed class StoreService
{
public async Task<bool> PurchaseProductAsync(string productId)
{
try
{
var purchase = await StoreCurrentApp.RequestProductPurchaseAsync(productId);
return purchase.Status == ProductPurchaseStatus.Succeeded || purchase.Status == ProductPurchaseStatus.AlreadyPurchased;
}
catch (Exception)
{
// The purchase did not complete because an error occurred.
return false;
}
}
}
答案 0 :(得分:0)
我做了一个跟随你的演示,你可以参考它。
xaml part:
<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 part:
<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);
}
}
}
}
此外,我在IAP后发现有awesome video删除广告,您也可以参考它。
答案 1 :(得分:0)
这是AdDuplex广告控制的一个问题,已在版本9.0.0.13中修复。
注意:不要忘记将IsTest
设置为false
以查看“生产”行为。