Programmatically add Pivot Items

2020-03-30 02:53发布

I want to display a list of photos in a Pivot Control, so I have this xaml

<Grid x:Name="LayoutRoot" Background="Transparent">
    <controls:Pivot x:Name="DiaporamaPivot">
    </controls:Pivot>
</Grid>

and in the code behind I do :

    public Diaporama()
    {
        InitializeComponent();

        PivotItem p = new PivotItem();
        Image     i = new Image();

        i.Source = new BitmapImage(new Uri("/image.jpg", UriKind.Relative));
        p.Margin = new Thickness(0, -10, 0, -2);

        DiaporamaPivot.Items.Add(i);
    }

Any idea why I get an exception

1条回答
放荡不羁爱自由
2楼-- · 2020-03-30 03:29

You are adding i (Image) to Pivot. Instead, add i to p and then, add p (PivotItem) to Pivot.

public Diaporama()
{
    InitializeComponent();

    PivotItem p = new PivotItem();
    Image     i = new Image();

    i.Source = new BitmapImage(new Uri("/image.jpg", UriKind.Relative));
    p.Margin = new Thickness(0, -10, 0, -2);

    p.Content = i;
    DiaporamaPivot.Items.Add(p);
}
查看更多
登录 后发表回答