简体   繁体   English

以编程方式设置Image.Source属性:我得到一个空图像

[英]Programmatically setting Image.Source property: I get an empty image

I have a folder inside my project which includes some images, for example: "Cards/1-1.png". 我的项目中有一个文件夹,其中包含一些图像,例如:“ Cards / 1-1.png”。

I have an Image element linked with XAML, and I'm trying to set it's Source property programmatically: 我有一个与XAML链接的Image元素,并且我试图通过编程方式将其设置为Source属性:

BitmapImage bitmap = new BitmapImage();
string filename= "Cards/1-1.png";
bitmap.UriSource = new Uri("/Poker;component/" + filename, UriKind.Relative);
cardImage1.Source= bitmap;  // myImage linked with XAML

I try to fill 5 images, and I checked the filenames, they're correct. 我尝试填充5张图片,然后检查了文件名,它们是正确的。 But the images are still empty (they're inside the StackPanel on the right): 但是图像仍然是空的(它们在右侧的StackPanel内部):

    <StackPanel Orientation="Horizontal" DockPanel.Dock="Right"
        Background="Green">
        <Image Width="80" Height="100" Margin="10" x:Name="cardImage1"/>
        <Image Width="80" Height="100" Margin="10" x:Name="cardImage2"/>
        <Image Width="80" Height="100" Margin="10" x:Name="cardImage3"/>
        <Image Width="80" Height="100" Margin="10" x:Name="cardImage4"/>
        <Image Width="80" Height="100" Margin="10" x:Name="cardImage5"/>
    </StackPanel>

在此处输入图片说明

I tried this code but it isn't working, i found a way to do what you exactly need: 我尝试了这段代码,但是它不起作用,我找到了一种方法来满足您的实际需求:

cardImage1.Source = new BitmapImage(new Uri("/Poker;component/" + filename, UriKind.Relative));

you assigned BitmapImage.Source to Image.Source, i think that's why it isn't working. 您将BitmapImage.Source分配给Image.Source,我认为这就是为什么它不起作用的原因。

Hope it helps. 希望能帮助到你。

BitmapImage implements ISupportInitialize interface which means any property change after initialization of object will be ignored unless wrap in BeginInit() and EndInit() . BitmapImage实现ISupportInitialize接口,这意味着对象初始化之后的任何属性更改都将被忽略,除非将它们包装在BeginInit()EndInit()

Quote from MSDN : MSDN引用:

BitmapImage implements the ISupportInitialize interface to optimize initialization on multiple properties. BitmapImage实现ISupportInitialize接口,以优化多个属性上的初始化。 Property changes can only occur during object initialization. 属性更改只能在对象初始化期间发生。 Call BeginInit to signal that initialization has begun and EndInit to signal that initialization has completed. 调用BeginInit表示初始化已开始,调用EndInit表示初始化已完成。 After initialization, property changes are ignored. 初始化后,将忽略属性更改。

BitmapImage objects created using the BitmapImage constructor are automatically initialized and property changes are ignored. 使用BitmapImage构造函数创建的BitmapImage对象将自动初始化,并且忽略属性更改。

So, change your code to wrap property initialization like this: 因此,更改您的代码以包装属性初始化,如下所示:

BitmapImage bitmap = new BitmapImage();
string filename= "Cards/1-1.png";
bitmap.BeginInit();
bitmap.UriSource = new Uri("/Poker;component/" + filename, UriKind.Relative);
bitmap.EndInit();
cardImage1.Source= bitmap;  // myImage linked with XAML

OR 要么

Initialize all relevant properties at time of initialization by passing Uri in constructor like this: 通过在构造函数中传递Uri,在初始化时初始化所有相关属性:

string filename= "Cards/1-1.png";
BitmapImage bitmap = new BitmapImage(new Uri("/Poker;component/" + filename, 
                                              UriKind.Relative));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM