如何在网格视图中实施adcontrol,以便它显示30个项目,然后是广告,然后是30个项目,然后是广告

时间:2012-12-22 18:15:32

标签: c# gridview windows-8 microsoft-metro

如何将adcontrol添加到gridview,我有。 (项目页面模板)

这是我使用的数据模板:

    <DataTemplate x:Key="Normal">
    <Grid HorizontalAlignment="Left" Width="180" Height="180">
        <Border Background="{StaticResource ListViewItemPlaceholderBackgroundThemeBrush}">
            <Image Source="{Binding Image}" Stretch="UniformToFill" AutomationProperties.Name="{Binding Title}"/>
        </Border>
        <StackPanel VerticalAlignment="Bottom" Background="{StaticResource ListViewItemOverlayBackgroundThemeBrush}">
            <TextBlock Text="{Binding Title}" Foreground="{StaticResource ListViewItemOverlayForegroundThemeBrush}" Style="{StaticResource TitleTextStyle}"
                       Height="40"
                       Margin="15,0,15,0"
                       TextAlignment="Center" />
            <TextBlock Text="{Binding Subtitle}" Foreground="{StaticResource ListViewItemOverlaySecondaryForegroundThemeBrush}" Style="{StaticResource CaptionTextStyle}" TextWrapping="NoWrap" Margin="15,0,15,10"
                       TextAlignment="Center" />
        </StackPanel>
    </Grid>
</DataTemplate>

我用它来显示我的项目,我也希望有广告。如何对其实施广告控制,以便它显示30个项目,然后是广告,然后是30个项目,然后是广告。 (我有添加项目的代码,我只是不知道如何添加广告)。

修改 好的,我已经完成了我应该做的所有事情,但是现在它给了我一个错误,即无法找到命名空间。

以下是错误:

The name "MyDataTemplateSelector" does not exist in the namespace "using:MyDataSelector"

在我的主页XAML代码中,我已经完成了这个:

xmlns:selectornamespace="using:MyDataSelector"

这是我的页面资源:

<Page.Resources>
    <!-- Collection of items displayed by this page -->
        <CollectionViewSource
        x:Name="itemsViewSource"
        Source="{Binding Items}"/>
    <!-- TODO: Delete this line if the key AppName is declared in App.xaml -->
    <x:String x:Key="AppName">Sample App</x:String>

    <selectornamespace:MyDataTemplateSelector x:Key="Selector" AdTemplate="{StaticResource Ad}" NormalTemplate="{StaticResource Normal}"></selectornamespace:MyDataTemplateSelector>
</Page.Resources>

这是MyDataSelector类。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace MyDataSelector
{
    private class MyDataTemplateSelector : DataTemplateSelector
    {
        public DataTemplate NormalTemplate { get; set; }
        public DataTemplate AdTemplate{ get; set; }
        protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
        {
            if (item is TestApp.Mainpage.NormalData)
                return NormalTemplate
            if (item is TestApp.Mainpage.AdData)
                return AdTemplate;

            return SelectTemplateCore(item, container);
        }
    }
}

NormalTemplate和AdTemplate在StandardStyles.xaml

任何帮助都会非常感激! 谢谢,圣诞快乐

1 个答案:

答案 0 :(得分:1)

使用datatemplate selector,然后检测该项目是否只是虚拟“广告”项目,然后显示添加数据模板而不是标准模板。

您需要在每30个项目后插入一个虚拟项目到数据源中,然后才能使其工作