How to get the event when clicking the microsoft ad in uwp

删除回忆录丶 提交于 2021-01-29 03:01:26

问题


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"/>


回答1:


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;
}


来源:https://stackoverflow.com/questions/51434881/how-to-get-the-event-when-clicking-the-microsoft-ad-in-uwp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!