如何在后面的代码中将图像源设置为图像

时间:2013-06-17 13:24:10

标签: image silverlight windows-phone-7 windows-phone-8

我在代码隐藏中获取Source设置的Image时遇到问题。这是我的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);


    }

谁能看到我做错了什么?

1 个答案:

答案 0 :(得分:2)

首先,我不明白的是图像路径“/project ;component/Images/avatar.png”不认为“;”标志使路径有效。这应该适合你:

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

您可以将图像的“复制到输出目录”属性设置为“始终复制”。