Tim
Tim

Reputation: 1769

Adcontrol InvalidCastException when inside pivot item template

I've an error with my pivot application on silverlight for Windows Phone.

To reproduce the problem, create the Pivot Application example. Change xaml to have :

    <Grid x:Name="LayoutRoot" Background="Transparent">
    <!--Pivot Control-->
    <controls:Pivot Title="MY APPLICATION" ItemsSource="{Binding Items}">
        <controls:Pivot.HeaderTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding LineOne}" />
            </DataTemplate>
        </controls:Pivot.HeaderTemplate>
        <controls:Pivot.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <TextBlock Text="{Binding LineTwo}" />
                    <advertising:AdControl Grid.Row="2" Grid.ColumnSpan="2" Height="80" Width="480" VerticalAlignment="Top" AdUnitId="10000000" ApplicationId="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaa" />
                </StackPanel>
            </DataTemplate>
        </controls:Pivot.ItemTemplate>
    </controls:Pivot>
</Grid>

that is an adcontrol in a pivot itemtemplate.

Don't forget to add the reference to Microsoft.Advertising.Mobile.UI and to declare namespace :

xmlns:advertising="clr-namespace:Microsoft.Advertising.Mobile.UI;assembly=Microsoft.Advertising.Mobile.UI"

then, when you run the phone application (on emulator), you have an InvalidCastException.

Without the control, no problem.

How can I solve that ?

Thanks for your help

Upvotes: 1

Views: 329

Answers (1)

Edward
Edward

Reputation: 7424

If your trying to add views to your pivot you should be using a PivotItem. I'm almost positive the reason for the error is that your putting the AdControl inside of the ItemTemplate of the Pivot Control. There are better ways of doing this.

Instead just add a bottom margin to the pivot control of 80px. Then add the AdControl inside of the LayoutRoot and set it's Vertical Alignment to Bottom.

Not sure I would highly recommend this because of user experience, but if you want an AdControl on each view of the Pivot Control simply add the PivotItems you need and in each place an AdControl.

Bottom line: Don't place the AdControl in the ItemTemplate of the PivotControl.

That should do it.

Upvotes: 1

Related Questions