在uwp中点击微软广告时如何获取事件 [英] How to get the event when clicking the microsoft ad in uwp

查看:31
本文介绍了在uwp中点击微软广告时如何获取事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 Microsoft Advertising SDK 用于 xaml.我的应用现在可以显示广告了.但我想知道用户点击广告时的事件.

以下事件均无效.

 

解决方案

以下事件均无效.

实际上,您不能直接使用上述事件,因为它会被 Ad WebView 中显示的超链接点击忽略.

如果你想检测 AdControl 的点击事件,你可以使用一些间接的方式,使用 VisualTreeHelper 来获取 AD WebView 并监听它的NavigationStarting 事件

public static T MyFindListBoxChildOfType(DependencyObject root) where T : class{var MyQueue = new Queue();MyQueue.Enqueue(root);while (MyQueue.Count > 0){DependencyObject current = MyQueue.Dequeue();for (int i = 0; i < VisualTreeHelper.GetChildrenCount(current); i++){var child = VisualTreeHelper.GetChild(current, i);var typedChild = child as T;if (typedChild != null){返回 typedChild;}MyQueue.Enqueue(child);}}返回空;}私有无效 AdTest_AdRefreshed(对象发送者,RoutedEventArgs e){var ADWebView = MyFindListBoxChildOfType(AdTest);ADWebView.NavigationStarting += ADWebView_NavigationStarting;}私人无效 ADWebView_NavigationStarting(WebView 发送者,WebViewNavigationStartingEventArgs args){System.Diagnostics.Debug.WriteLine("AD点击---------------");}

为避免页面导航干扰,请在OnNavigatedFrom覆盖方法中取消订阅NavigationStarting.

protected override void OnNavigatedFrom(NavigationEventArgs e){base.OnNavigatedFrom(e);ADWebView.NavigationStarting -= ADWebView_NavigationStarting;}

I'm using Microsoft Advertising SDK for xaml. And my app can show the ad now. But I want to know the event when user click the ad.

None of the following event worked.

    <ads:AdControl x:Name="adAd" Grid.Row="3" ApplicationId="" AdUnitId=""
         Width="300" Height="250" AdRefreshed="OnAdRefreshed" 
         ErrorOccurred="OnErrorOccurred"
         Tapped="OnAdTapped" OnPointerDown="OnAdPointerDown" 
         PointerPressed="OnAdPointerPressed"/>

解决方案

None of the following event worked.

Actually, you could not use the above event directly, because it will be ignored by hyperlink click where displayed in the Ad WebView.

If you want detect click event of AdControl, you could use some indirect way that use VisualTreeHelper to get the AD WebView and listen it's NavigationStarting event

public static T MyFindListBoxChildOfType<T>(DependencyObject root) where T : class
{
    var MyQueue = new Queue<DependencyObject>();
    MyQueue.Enqueue(root);
    while (MyQueue.Count > 0)
    {
        DependencyObject current = MyQueue.Dequeue();
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(current); i++)
        {
            var child = VisualTreeHelper.GetChild(current, i);
            var typedChild = child as T;
            if (typedChild != null)
            {
                return typedChild;
            }
            MyQueue.Enqueue(child);
        }
    }
    return null;
}


private void AdTest_AdRefreshed(object sender, RoutedEventArgs e)
{
    var ADWebView = MyFindListBoxChildOfType<WebView>(AdTest);
    ADWebView.NavigationStarting += ADWebView_NavigationStarting;
}

private void ADWebView_NavigationStarting(WebView sender, WebViewNavigationStartingEventArgs args)
{
    System.Diagnostics.Debug.WriteLine("AD clicked---------------");
}

In order to avoid interference from page navigation, please unsubscribe NavigationStarting in OnNavigatedFrom override method.

protected override void OnNavigatedFrom(NavigationEventArgs e)
{
    base.OnNavigatedFrom(e);
    ADWebView.NavigationStarting -= ADWebView_NavigationStarting;
}

这篇关于在uwp中点击微软广告时如何获取事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆