3

私の問題の何が問題なのかを理解しようとして、さまざまな投稿を閲覧していました。基本的に、ユーザーコントロールにイメージタグがあり、URL にバインドしたいソースがあります。ただし、これは機能しません。返す ValueConverter を使用しようとしましたがBitmapImage(new Uri((string)value)); 、これは機能しません。私が得ることができた唯一のことは、URLにバインドできず、バインドしたい画像をダウンロードする必要があるということです. 検索したすべての画像をダウンロードしたくありません。イメージをローカルにダウンロードしなくても、このタスクを達成するための回避策はありますか。BitmapImage を返す ValueConverter メソッドが最適だと思いました。助けてください?

public class MyViewModel
{
    private string _posterUrl;
        public string PosterUrl
        {
            get
            {
                //Get Image Url, this is an example and will be retrieved from somewhere else.
                _posterUrl = "http://www.eurobuzz.org/wp-content/uploads/2012/08/logo.jpg";
                return _posterUrl;    
            }
            set 
            { 
                _posterUrl = value;
                NofityPropertyChanged(p => p.PosterUrl);
            }
        }
}

これは私のValueConverterです:

public class BitmapImageConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if(value is string)
            return new BitmapImage(new Uri((string)value, UriKind.RelativeOrAbsolute));

        if(value is Uri)
            return new BitmapImage((Uri)value);

        throw new NotSupportedException();
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}

これは私のXAMLです:

<Image Source="{Binding PosterUrl, Converter={StaticResource bitmapImageConverter}}" Width="100" Height="100" />

したがって、これは imageurl を含む PosterUrl プロパティにバインドされ、これはビットマップ画像に変換されます。何か案は?

4

2 に答える 2

1

それを試してみてください

<Image Helpers:ImageAsyncHelper.SourceUri="{Binding Url, IsAsync=True}" x:Name="img" />

どこ

using System;
using System.Windows;
using System.Windows.Data;
using System.Windows.Controls;

public class ImageAsyncHelper : DependencyObject {
    public static Uri GetSourceUri(DependencyObject obj){
        return (Uri)obj.GetValue(SourceUriProperty);
    }

    public static void SetSourceUri(DependencyObject obj, Uri value){
        obj.SetValue(SourceUriProperty, value);
    }

    public static readonly DependencyProperty SourceUriProperty =
        DependencyProperty.RegisterAttached("SourceUri",
            typeof(Uri),
            typeof(ImageAsyncHelper),
            new PropertyMetadata { PropertyChangedCallback = (obj, e) =>
                ((Image)obj).SetBinding(
                    Image.SourceProperty,
                    new Binding("VerifiedUri"){
                        Source = new ImageAsyncHelper{
                            _givenUri = (Uri)e.NewValue
                        },
                        IsAsync = true
                    }
                )
            }
        );

    private Uri _givenUri;
    public Uri VerifiedUri {
        get {
            try {
                System.Net.Dns.GetHostEntry(_givenUri.DnsSafeHost);
                return _givenUri;
            }
            catch (Exception) {
                return null;
            }
        }
    }
}

public Uri Url {
    get {
        return new Uri(SomeString, UriKind.Absolute);
    }
}
于 2013-01-16T10:47:24.803 に答える
0

これをやろうとしていますか?

<UserControl x:Class="WpfApplication1.UserControl1"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<StackPanel Orientation="Vertical">
    <Image Height="100" Width="100" Source="{Binding}" />

    <Image Height="100" Width="100" Source="http://www.eurobuzz.org/wp-content/uploads/2012/08/logo.jpg"/>
</StackPanel>

コードビハインド:

public partial class UserControl1 : UserControl
{
    public UserControl1()
    {
        this.DataContext = "http://www.eurobuzz.org/wp-content/uploads/2012/08/logo.jpg";
        InitializeComponent();
    }
}
于 2013-01-16T10:15:04.760 に答える