我想在数据透视表控件中显示照片列表,所以我有这个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);
}
知道我为什么会遇到异常
答案 0 :(得分:9)
您要将i
(Image
)添加到Pivot
。相反,将i
添加到p
,然后将p
(PivotItem
)添加到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);
}