简体   繁体   English

Newb:在运行时更新XAML图像元素(WIndows Phone,.NET 4)

[英]Newb: Update XAML Image Element During Runtime (WIndows Phone, .NET 4)

Greetings all, 大家问候,

Thanks for reading and sharing any insights. 感谢您阅读和分享任何见解。 I've created a new simple Windows Phone 7 application. 我创建了一个新的简单Windows Phone 7应用程序。 I have two Image objects on the form I would like to update with various .png files. 我要使用各种.png文件更新表单上的两个Image对象。 I have included the .png files into my project, and I believe I am building proper URi resources pointing to these files. 我已经将.png文件包含到我的项目中,并且我相信我正在构建指向这些文件的适当URi资源。

Uri myImage1URi = new Uri( strImage1, UriKind.RelativeOrAbsolute );
Uri myImage2URi = new Uri( strImage2, UriKind.RelativeOrAbsolute );

System.Windows.Media.Imaging.BitmapImage bi1 = new System.Windows.Media.Imaging.BitmapImage(myImage1URi);
System.Windows.Media.Imaging.BitmapImage bi2 = new System.Windows.Media.Imaging.BitmapImage(myImage2URi);

image1.Source = bi1;
image1.Stretch = Stretch.Fill;
image2.Source = bi2;
image2.Stretch = Stretch.Fill;

This alone is not accomplishing what I want (to update the images to the two from the URi's). 仅此一项并不能实现我想要的功能(将图像从URi更新为两个)。

I know there is something a bit off going on (IE: I am doing something dumb) in that all of the BitmapImage class descriptions mention that I have to do a .BeginInit() before I work with the BitmapImage object, and a .EndInit() call afterwards. 我知道发生了一些事情(即,我正在做一些愚蠢的事情),因为所有BitmapImage类描述都提到在使用BitmapImage对象之前必须先执行.BeginInit()和.EndInit ()之后致电。 These method calls don't work for me, so I know something is amiss.... 这些方法调用对我不起作用,所以我知道有些问题。

Or maybe I am competely off base and I simply need a way to tell my main window to repaint itself? 或者,也许我在基地之外竞争激烈,我只需要一种方法告诉主窗口自己重新粉刷? That thought has occurred to me as well. 我也想到了这种想法。

Thanks again. 再次感谢。

The following will load an image that is in the appropriate path and is set as having a build action content. 以下内容将加载位于适当路径中并被设置为具有构建动作内容的图像。

myImg.Source = new BitmapImage(new Uri("/images/filename.png", UriKind.Relative));

It assumes XAML on the page like: 假定页面上的XAML如下:

<Image x:Name="myImg" />

This seems very similar to what you're doing. 这似乎与您正在执行的操作非常相似。 Simplify what you're doing ot get it working. 简化您的操作,使其正常运行。
Does it work with just using one image? 仅使用一张图像就能使用吗?
Is the path in strImageN a valid path? strImageN的路径是有效路径吗?
Do the image files have their build action set to Content? 图像文件的生成操作是否设置为“内容”?

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

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