之前在弄unity时在蛮牛找到了wp8加广告的方法,然后自己改进了一下,wp8.1下可用,以下是关键代码
private void creatAD()
{
DispatcherTimer _dispatcherTimer = new DispatcherTimer();
_dispatcherTimer.Interval = TimeSpan.FromSeconds(0.1); //延时添加广告控件
_dispatcherTimer.Tick += (sender, e) =>
{
_dispatcherTimer.Stop();
if (adControl == null)
{
adControl = new Microsoft.Advertising.Mobile.UI.AdControl()
{
AutoRefreshIntervalInSeconds = 60,
ApplicationId = "test_client",
AdUnitId = "Image320_50",
VerticalAlignment = VerticalAlignment.Bottom,
HorizontalAlignment = HorizontalAlignment.Left,
IsBackgroundTransparent = true,
};
adControl.Height = 80;
adControl.Width = 480;
adControl.IsAutoRefreshEnabled = true;
adControl.Margin = new Thickness(0, 0, 0, 0);
}
};
_dispatcherTimer.Start();
}
private async void showAD()
{
await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
if (adControl != null)
{
DXSwapChainPanel.Children.Add(adControl);
adControl.Margin = new Thickness(0, 0, 0, 0);
adControl.Height = 80;
adControl.Width = 480;
}
});
}
private async void HideAD()
{
await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
if (DXSwapChainPanel.Children.Contains(adControl))
{
DXSwapChainPanel.Children.Remove(adControl);
}
});
}