以编程方式添加枢轴项 [英] Programmatically add Pivot Items

查看:20
本文介绍了以编程方式添加枢轴项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Pivot Control 中显示照片列表,所以我有这个 xaml

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>

在我后面的代码中:

    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

推荐答案

您正在将 i (Image) 添加到 Pivot.相反,将 i 添加到 p,然后将 p (PivotItem) 添加到 Pivot>.

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

这篇关于以编程方式添加枢轴项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆