我正在尝试以编程方式生成StackPanel并将图像添加到StackPanel.不知怎的,我得到一个空的StackPanel.我没有看到我的代码有任何问题,它没有抛出任何异常:
- StackPanel Sp = new StackPanel();
- Sp.Orientation = Orientation.Horizontal;
- Image Img = new Image();
- BitmapImage BitImg = new BitmapImage(new Uri(
- "/MyProject;component/Images/image1.png",UriKind.Relative));
- Img.Source = BitImg;
- Sp.Children.Add(Img);
[编辑]
我尝试了另一种方式添加图像,它的工作原理.它引起了我的兴趣,因为它们在我看来基本上是一样的:
- Image Img = new Image();
- Img.Source = new BitmapImage(new Uri(
- "pack://application:,/MyProject;component/Images/image1.png"));
以下代码不起作用(图像丢失):
- Image Img = new Image();
- BitmapImage ImgSource = new BitmapImage(new Uri(
- "pack://application:,/MyProject;component/Images/image1.png",UriKind.Relative));
- Img.Source = BitImg;
他们为什么不同?