Image 如何在代码隐藏中将图像源设置为图像
在代码隐藏中设置我的Image 如何在代码隐藏中将图像源设置为图像,image,silverlight,windows-phone-7,windows-phone-8,Image,Silverlight,Windows Phone 7,Windows Phone 8,在代码隐藏中设置我的图像的源代码时遇到问题。这是我的XAML: 有人能看出我做错了什么吗?首先,我不明白的是图像路径“/project;component/Images/avatar.png“don think”;”标志使路径有效。这应该适合您: <StackPanel Name="stkPanel" Height="1200" Width="478" HorizontalAlignment="Center" VerticalAlignment="Top"
图像的源代码时遇到问题。这是我的XAML:
有人能看出我做错了什么吗?首先,我不明白的是图像路径“/project;component/Images/avatar.png“don think”;”标志使路径有效。这应该适合您:
<StackPanel Name="stkPanel" Height="1200" Width="478" HorizontalAlignment="Center" VerticalAlignment="Top" RenderTransformOrigin="0.723,0.509">
<Image Loaded="imgPicture_Loaded" x:Name="imgPicture" Stretch="UniformToFill"
Height="309" Width="413" HorizontalAlignment="Center" VerticalAlignment="Top"
Margin="0,30,0,0"></Image>
</StackPanel>
您可以将映像的“复制到输出目录”属性设置为“始终复制” 是否确定要显示的图像的BuildAction
已设置为Resource?初始化图像Uri
的方式适合嵌入式资源。最好的做法是将内容
用作图像的构建操作
,而Uri
初始化将看起来像新的Uri(“/images/avatar.png”,UriKind.Relative)
。为什么要等待设置图像源?您试图解决或避免什么问题?最初使用的URI格式是有效的,允许引用不同程序集中的资源。除非您在构建XAP文件后正在执行一些不寻常的操作,否则设置“复制到输出目录”属性没有任何价值。它不会影响XAP文件。它是其他旧项目类型的遗留问题,您可能需要build/bin目录的全部内容。
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);
}
<StackPanel Name="stkPanel" Height="1200" Width="478" HorizontalAlignment="Center" VerticalAlignment="Top" RenderTransformOrigin="0.723,0.509">
<Image Loaded="imgPicture_Loaded" x:Name="imgPicture" Stretch="UniformToFill"
Height="309" Width="413" HorizontalAlignment="Center" VerticalAlignment="Top"
Margin="0,30,0,0"></Image>
</StackPanel>
private void imgPicture_Loaded(object sender, RoutedEventArgs e)
{
imgPicture.Source = new BitmapImage(new
Uri("/Images/StoreLogo.png",UriKind.Relative));
}