【发布时间】:2013-06-17 23:59:44
【问题描述】:
我无法在代码隐藏中设置 Image 的 Source。这是我的 XAML:
<StackPanel Name="stkPanel" Height="1200" Width="478" HorizontalAlignment="Center" VerticalAlignment="Top" RenderTransformOrigin="0.723,0.509">
<Image Loaded="imgPicture_Loaded_1" x:Name="imgPicture" ImageOpened="ImgSelectedPicture_ImageOpened_1" Stretch="UniformToFill" Height="309" Width="413" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="0,30,0,0"></Image>
</StackPanel>
还有代码隐藏:
private void imgPicture_Loaded_1(object sender, RoutedEventArgs e)
{
imgPict = (sender as Image);
//ScrollViewer scroll = this.LayoutRoot.Children[2] as ScrollViewer;
imgPict.Source = new BitmapImage(new Uri("/project;component/Images/avatar.png", UriKind.RelativeOrAbsolute));
//bindPicture(imgPict);
}
谁能看出我做错了什么?
【问题讨论】:
-
您确定要显示的图像的
BuildAction设置为资源吗?您初始化图像Uri的方式适用于嵌入式资源。最佳实践是使用Content作为BuildAction用于图像,Uri初始化看起来像new Uri("/Images/avatar.png", UriKind.Relative)。 -
为什么要等设置图片源?您想解决或避免什么问题?
标签: image silverlight windows-phone-7 windows-phone-8