我目前卡在 Visual Studio 2017 中的一个项目中。以下代码用于在 .xaml 页面上设置图像组件的源。
private void setImage()
{
var image = new Image();
var imageUrl = @"http://ddragon.leagueoflegends.com/cdn/5.2.1/img/" +
currentChamp.Image.Group + "/" + currentChamp.Image.Full;
Debug.Write(imageUrl);
BitmapImage bmi = new BitmapImage();
bmi.BeginInit();
bmi.UriSource = new Uri(imageUrl, UriKind.Absolute);
bmi.EndInit();
image.Source = bmi;
}
这里是要引用的代码。 ^^^
在所有其他问题中,我发现与我要问的问题相似,这一直是我正在尝试做的事情的正确/回答/批准的方式。但是,我收到一个错误,在任何问题中没有其他人收到
无法将 system.windows.media.imaging.BitmapImage 隐式转换为 Windows.UI.XAML.media.ImageSource
我的导入、引用和类型都已经过检查并且是正确的。我缺少框架吗?是否有可能导致此功能不再有效的更新?
我正在使用 VS2017 在 Microsoft.NETCore.UniversalWindowsPlatform 中工作
请注意,这不是 this 的副本,因为我没有引用资源文件夹并且我的路径是绝对路径。
最佳答案
Windows.Media.Imaging.BitmapImage
是一个 WPF 类。 Windows.UI.XAML.media.ImageSource
是 UWP。您找到了 WPF 的解决方案,但您的 UWP Image
类需要一个 UWP 位图对象。您需要使用 Windows.UI.Xaml.Media.Imaging.BitmapImage
的东西.
目前我无法测试 UWP,但通过阅读文档,我认为您想要这样的东西:
image.Source = new Windows.UI.Xaml.Media.Imaging.BitmapImage(new Uri(imageUrl, UriKind.Absolute));
您的 C# 文件中似乎有 using Windows.Media.Imaging;
。将其替换为 using Windows.UI.Xaml.Media.Imaging;
那么你可以使用这个:
image.Source = new BitmapImage(new Uri(imageUrl, UriKind.Absolute));
关于c# - 将 BitmapImage 转换为 Image.Source,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44833830/