【问题标题】:Set the height of an image after it's been downloaded in a Windows 8 App?在 Windows 8 应用程序中下载图像后设置图像的高度?
【发布时间】:2012-11-24 04:23:17
【问题描述】:

我正在尝试在 Windows 8 应用程序中动态设置 Image 元素的高度。为此,我需要知道源图像的高度。通常这可以通过调用 BitmapImage.PixelHeight 或 BitmapImage.DecodePixelHeight 来访问,但由于图像尚未下载,它返回 0。我可以像这样设置回调:

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); 
}

我在该回调中得到了我需要的正确像素高度。问题是我需要将它传递给要添加该图像的用户控件的实例。有没有一种方法可以 a.) 将实例传递到回调的参数中,以便我可以在那里修改它,或者 b.) 以某种方式在没有回调的情况下异步获取高度?

【问题讨论】:

    标签: c# windows-8 microsoft-metro winrt-xaml


    【解决方案1】:

    我最终解决此类问题的方法是创建一个自定义用户控件来显示图像,它会在加载图像时自行调整大小。

    这是我的 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>
    

    这是我的 .xaml.cs(使用已删除的语句):

    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
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多