C# 位图图像绑定
这段代码有什么问题?当我将XAML中的图像源绑定到代码中的firstimage时,为什么firstimage没有显示在XAML页面上? 包含代码的类不是分部类C# 位图图像绑定,c#,xaml,windows-runtime,C#,Xaml,Windows Runtime,这段代码有什么问题?当我将XAML中的图像源绑定到代码中的firstimage时,为什么firstimage没有显示在XAML页面上? 包含代码的类不是分部类 if (ImagesAsSource[realty.ObjectId].Count == 0) //private static Uri _baseUri = new Uri("ms-appx:///"); { Uri img = new Uri(_baseUri, "Assets/back.jpg"); BitmapIm
if (ImagesAsSource[realty.ObjectId].Count == 0)
//private static Uri _baseUri = new Uri("ms-appx:///");
{
Uri img = new Uri(_baseUri, "Assets/back.jpg");
BitmapImage result = new BitmapImage();
result.UriSource = img;
// firstimage is type of ImageSoure
firstimage =result;
}
//I tried this:
//private static Uri _baseUri = new Uri("ms-appx:///");
{
Uri img = new Uri(_baseUri, "Assets/back.jpg");
BitmapImage result = new BitmapImage(img );
// firstimage is type of ImageSoure
firstimage =result;
}
您应该尝试以下方法:
Uri img = new Uri(_baseUri, "Assets/back.jpg");
var webClient = new WebClient();
byte[] imageBytes = webClient.DownloadData(img);
BitmapImage result = new BitmapImage(imageBytes);
事实上,我不知道BitmapImage的构造函数,所以可能您应该将字节[]转换为流这是正确的解决方案
Uri _baseUri = new Uri("ms-appx:///");
Uri img = new Uri(_baseUri, "Assets/back.png");
BitmapImage result = new BitmapImage(img);
// firstimage is type of ImageSoure
firstimage.Source = result;
问题是您正在尝试从URI字符串创建BitmapImage。解决方案是什么?您可以尝试从answer@Valin从
Uri
(不是Uri字符串)创建位图图像不是问题,而是创建位图图像的正确方法。@user3105491请显示绑定到firstimage
的XAML。您是否确保firstimage
是公共财产?这不是正确的解决方案,为什么要使用WebClient?如果应用程序不使用Internet怎么办?您使用了URI,因此使用webclient从中获取数据似乎是合乎逻辑的。但它们是本地URI,而不是Internet URI。如果应用程序不使用Internet怎么办?应该告诉用户使用Internet来运行图像。这不是一个可行的解决方案&顺便说一句,BitmapImage
没有将byte[]
作为参数。-1有两个原因。首先,对本地文件使用WebClient没有任何意义。其次,BitmapImage没有将字节数组作为参数的构造函数。包含它的类不是分部类。firstimage
似乎不是图像控件,而是ImageSource类型的字段或属性。