AdDuplex广告控制保持显示,尽管崩溃它
本文关键字:崩溃 显示 控制 AdDuplex | 更新日期: 2023-09-27 18:06:44
我正在创建一个WinRT (Windows 8.1和Windows Phone 8.1)应用程序,我在其中一个页面中放置了AdDuplex广告控件。
应用的用户可以选择删除广告(带有IAP)。当它们这样做时,我将AdDuplex广告控件的Visibility
设置为Collapsed
,从页面ViewModel。
这部分工作正常;然而,过了一段时间,当用户还在页面时,AdDuplex广告控制突然又变得可见并开始显示广告。
一开始,我认为这是IAP在使用CurrentAppSimulator
时的行为,尽管这对我来说没有意义,因为我在代码中没有对许可更改做出反应,因此将控制设置回Visible
。然而,我在我的"NoAd"产品上测试了license.IsActive
,得到了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;
}
}
}
我做了一个演示,你可以参考一下。
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);
}
}
}
}
此外,我发现了一个很棒的视频,关于在IAP后删除广告,你也可以参考它。
这是AdDuplex广告控制的一个问题,已在9.0.0.13版修复。
注意:不要忘记将IsTest
设置为false
,以查看"生产"行为