我在设置 Image Source 属性时遇到问题...所以我尝试使用此代码的每个变体,但由于某种原因它不起作用。 当我手动设置属性时它可以工作,但是当我想在代码中更改图片时它只是空白。
`BitmapImage` bm = new BitmapImage();
bm.UriSource=new Uri(this.BaseUri,@"\\Assets\logo6.png");
this.image.Source = image;
我在按钮事件中使用这段代码,因此我可以在图像控件中更改图片。
最佳答案
您以错误的方式设置了路径。尝试这两个选项中的任何一个都会起作用。
还要确保您的图像属性 Build Action 应设置为 Content
和 Copy To OutPut Directory to Copy to newer
或 Copy Always
BitmapImage bm = new BitmapImage();
bm.UriSource = new Uri(@"\Assets\Tiles\IconicTileSmall.png",UriKind.Relative);
image.Source = bm;
和
BitmapImage bm = new BitmapImage();
bm.UriSource = new Uri("\\Assets\\Tiles\\IconicTileSmall.png",UriKind.Relative);
image.Source = bm;
基本上您是在混合使用这两种方式。
关于c# - 图像源不会显示图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25493653/