如何只需加载图像文件并在WPF中显示它?

时间:2016-03-18 11:16:16

标签: c# wpf image

我有这个xaml:

<Window x:Class="TestWPF.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" SizeToContent="WidthAndHeight">
        <Image Width="100" Height="100" Source="{Binding theImage}"/>
</Window>

这个代码背后:

namespace TestWPF
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        BitmapImage theImage = new BitmapImage();
        public MainWindow()
        {
            InitializeComponent();
            theImage.BeginInit();
            theImage.UriSource = new Uri("dice.png", UriKind.Relative);
            theImage.CacheOption = BitmapCacheOption.OnLoad;
            theImage.EndInit();
            OnPropertyChanged("theImage");
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

为什么图像没有显示?我理解我的代码可能完全倒退。我只是在绕着WPF方式缠绕我的头。在Qt中这样的事情很简单,我似乎找不到任何相关的东西。

3 个答案:

答案 0 :(得分:1)

BitmapImage theImage应该是一个属性,绑定不能与字段一起使用。

    public BitmapImage theImage {get; set;}

    public MainWindow()
    {
        InitializeComponent();

        DataContext = this;

        theImage = new BitmapImage();
        theImage.BeginInit();
        theImage.UriSource = new Uri("dice.png", UriKind.Relative);
        theImage.CacheOption = BitmapCacheOption.OnLoad;
        theImage.EndInit();
        OnPropertyChanged("theImage");
    }

答案 1 :(得分:1)

为您的图片命名

<Image x:Name="MyImage" Width="100" Height="100"/>

并将BitmapImage指定给主窗口的ctor中的source属性

public MainWindow()
    {
        InitializeComponent();
        theImage.BeginInit();
        theImage.UriSource = new Uri("dice.png", UriKind.Relative);
        theImage.CacheOption = BitmapCacheOption.OnLoad;
        theImage.EndInit();
        MyImage.Source = theImage;
    }

答案 2 :(得分:0)

WPF应用程序的推荐模式是MVVM,其中有一个视图模型类,它声明视图中绑定的源属性(并实现INotifyPropertyChanged接口以通知属性值更改):

public class ViewModel : INotifyPropertyChanged
{
    private ImageSource theImage;

    public ImageSource TheImage
    {
        get { return theImage; }
        set
        {
            theImage = value;
            OnPropertyChanged();
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

在您的视图中,您将DataContext属性分配给视图模型的实例。如果您没有设置DataContext,则必须明确设置绑定的源对象。

public MainWindow()
{
    InitializeComponent();

    var vm = new ViewModel();
    DataContext = vm;

    vm.TheImage = new BitmapImage(new Uri("dice.png", UriKind.Relative));
}

XAML中的绑定如下所示:

<Image ... Source="{Binding TheImage}"/>