C# 设置图像后面的高度';是否已在Windows 8应用程序中下载?
我正在尝试在Windows8应用程序中动态设置图像元素的高度。为此,我需要知道源图像的高度。通常,可以通过调用BitmapImage.PixelHeight或BitmapImage.DecodePixelHeight来访问该图像,但由于图像尚未下载,因此返回0。我可以这样设置回调:C# 设置图像后面的高度';是否已在Windows 8应用程序中下载?,c#,windows-8,microsoft-metro,winrt-xaml,C#,Windows 8,Microsoft Metro,Winrt Xaml,我正在尝试在Windows8应用程序中动态设置图像元素的高度。为此,我需要知道源图像的高度。通常,可以通过调用BitmapImage.PixelHeight或BitmapImage.DecodePixelHeight来访问该图像,但由于图像尚未下载,因此返回0。我可以这样设置回调: image = new BitmapImage(new Uri(link.Url)); System.Diagnostics.Debug.WriteLine("height :" + image.PixelHeigh
image = new BitmapImage(new Uri(link.Url));
System.Diagnostics.Debug.WriteLine("height :" + image.PixelHeight) // returns 0
instance.Source = image;
instance.Height = image.PixelHeight // what I'd like to do, but cant.
image.DownloadProgress += image_DownloadProgress;
static void image_DownloadProgress(object sender, DownloadProgressEventArgs e)
{
BitmapImage i = (BitmapImage)sender;
// returns the actual height I need
System.Diagnostics.Debug.WriteLine("post-download height: " + i.PixelHeight);
}
我在回调中得到了正确的像素高度。问题是,我需要将其传递给要将该图像添加到的usercontrol实例。有没有一种方法,我可以a.)将实例传递到回调的参数中,这样我就可以在那里修改它,或者b.)在没有回调的情况下以某种方式异步获取高度?我最终解决了这类问题,就是创建一个自定义usercontrol来显示图像,当加载图像时,它会自动调整大小 这是我的xaml:
<UserControl
x:Name="userControl"
x:Class="BoardsForWindows.Controls.BBPostImage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:BoardsForWindows.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
<StackPanel Orientation="Vertical">
<Image x:Name="imageViewer" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
</StackPanel>
</UserControl>
namespace BoardsForWindows.Controls
{
public sealed partial class BBPostImage : UserControl
{
private BitmapImage bitmapImage;
public string Source
{
get { return (string)GetValue(SourceProperty); }
set { SetValue(SourceProperty, value); }
}
// Using a DependencyProperty as the backing store for Source. This enables animation, styling, binding, etc...
public static readonly DependencyProperty SourceProperty =
DependencyProperty.Register("Source", typeof(string), typeof(BBPostImage), new PropertyMetadata(null, SourceChanged));
public BBPostImage()
{
this.InitializeComponent();
}
public static void SourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
BBPostImage image = d as BBPostImage;
if (image == null)
{
return;
}
string url = (e.NewValue ?? string.Empty).ToString();
Uri uri = null;
if (String.IsNullOrEmpty(url) || !Uri.TryCreate(url, UriKind.Absolute, out uri))
{
image.imageViewer.Width = 200;
image.imageViewer.Height = 40;
image.imageViewer.Source = null;
return;
}
image.bitmapImage = new BitmapImage();
image.bitmapImage.ImageFailed += image.bitmapImage_ImageFailed;
image.bitmapImage.ImageOpened += image.bitmapImage_ImageOpened;
image.bitmapImage.UriSource = uri;
image.imageViewer.Source = null;
image.imageViewer.Source = image.bitmapImage;
}
void bitmapImage_ImageOpened(object sender, RoutedEventArgs e)
{
Width = bitmapImage.PixelWidth;
Height = bitmapImage.PixelHeight;
}
void bitmapImage_ImageFailed(object sender, ExceptionRoutedEventArgs e)
{
// TODO
}
}
}