我有一个简单的动态Gridview:
<GridView x:Name="MainGridStations" ItemsSource="{x:Bind Stations}" IsItemClickEnabled="True" ItemClick="GridView_ItemClick">
<GridView.ItemTemplate>
<DataTemplate x:DataType="local:Station">
<Grid>
<Grid Background="White" HorizontalAlignment="Center" Width="300" Height="200" VerticalAlignment="Center">
<Image x:Name="ImageStation" Source="{Binding ImageURL}"/>
<Grid Background="#e4f0fc" Height="65" VerticalAlignment="Bottom" Opacity="0.8">
<TextBlock x:Name="StationName" Text="{Binding Name}" FontWeight="Bold" Foreground="#2c9a8b" HorizontalAlignment="Center" />
</Grid>
</Grid>
</Grid>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
现在,我想在此gridview中使用原生广告。在原生广告的代码中,我需要注册包含用于点击事件等广告的广告容器。 它将始终是gridview的第一个子项,这意味着gridview中的第一个网格容器。
我已经尝试过了:
nativeAd.RegisterAdContainer(MainGridStations.ContainerFromIndex(0));
这给了我错误,它无法从 Windows.UI.Xaml.DependencyObject 转换为Windows.UI.Xaml.FrameworkElement
基本上,我期望我无法提供xaml控件的直接名称。 我该如何克服?
答案 0 :(得分:0)
这给了我错误,它无法从Windows.UI.Xaml.DependencyObject转换为Windows.UI.Xaml.FrameworkElement
RegisterAdContainer
方法来注册充当原生广告容器的UI元素;正确跟踪广告的展示次数和点击次数是必需的。
RegisterAdContainer
的可用参数为FrameworkElement
。但是MainGridStations.ContainerFromIndex(0)
的值为DependencyObject
。因此它将引发异常。
如果您想在第一项GridView
中显示原生广告,则可以在呈现第一项时获得如下所示的第一项。
您需要继承GridView
并重写MeasureOverride
,ArrangeOverride
方法来获取项目,然后使用 Visual Helper 获取子项。
class MyGridView : GridView
{
private const string ImageStation = "ImageStation";
private const string StationName = "StationName";
private const string RootLayout = "RootLayout";
private bool isFirst;
NativeAdsManagerV2 myNativeAdsManager = null;
string myAppId = "d25517cb-12d4-4699-8bdc-52040c712cab";
string myAdUnitId = "test";
public MyGridView()
{
myNativeAdsManager = new NativeAdsManagerV2(myAppId, myAdUnitId);
myNativeAdsManager.AdReady += MyNativeAd_AdReady;
myNativeAdsManager.ErrorOccurred += MyNativeAdsManager_ErrorOccurred;
myNativeAdsManager.RequestAd();
}
private void MyNativeAdsManager_ErrorOccurred(object sender, NativeAdErrorEventArgs e)
{
}
private void MyNativeAd_AdReady(object sender, NativeAdReadyEventArgs e)
{
NativeAdV2 nativeAd = e.NativeAd;
AdTitle.Text = nativeAd.Title;
Adimage.Source = nativeAd.AdIcon.Source;
nativeAd.RegisterAdContainer(AdContainer);
}
protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
{
if (this.IndexFromContainer(element) == 0)
{
isFirst = true;
}
else
{
isFirst = false;
}
base.PrepareContainerForItemOverride(element, item);
}
protected override Size MeasureOverride(Size availableSize)
{
return base.MeasureOverride(availableSize);
}
private Image Adimage;
private TextBlock AdTitle;
private Grid AdContainer;
protected override Size ArrangeOverride(Size finalSize)
{
if (isFirst)
{
var item = this.ContainerFromIndex(0);
AdContainer = MyFindGridViewChildByName(item, RootLayout) as Grid;
Adimage = MyFindGridViewChildByName(item, ImageStation) as Image;
AdTitle = MyFindGridViewChildByName(item, StationName) as TextBlock;
isFirst = false;
}
return base.ArrangeOverride(finalSize);
}
public static DependencyObject MyFindGridViewChildByName(DependencyObject parant, string ControlName)
{
int count = VisualTreeHelper.GetChildrenCount(parant);
for (int i = 0; i < count; i++)
{
var MyChild = VisualTreeHelper.GetChild(parant, i);
if (MyChild is FrameworkElement && ((FrameworkElement)MyChild).Name == ControlName)
return MyChild;
var FindResult = MyFindGridViewChildByName(MyChild, ControlName);
if (FindResult != null)
return FindResult;
}
return null;
}
}