0

我有一个 WPF Window 项目和一个带有它的图像控件。我编写了另一个 dll 项目,这将是窗口的视图模型。这个 dll 项目包括一个名为“h.png”的 png 文件,我将把它作为源绑定到 WPF Window 项目。我设置了图像绑定但它不起作用,谁能告诉我如何从另一个 dll 项目中绑定图像源?

我的 C# 代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media.Imaging;

namespace vm
{
    public class VMClass
    {
        BitmapImage img;

        public BitmapImage Img
        {
            get { return img; }
            set { img = value; }
        }
        public VMClass()
        {
            img = new BitmapImage(new Uri("h.png", UriKind.RelativeOrAbsolute));


        }
    }
}

我的xml:

<Window x:Class="ImageBindingApplication.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vm="clr-namespace:vm;assembly=vm"
        Title="MainWindow" Height="350" Width="525">
    <Window.DataContext>
        <vm:VMClass/>
    </Window.DataContext>
    <Grid>
        <Border BorderThickness="2" BorderBrush="Red">
            <Image Source="{Binding Img}"/>
        </Border>
    </Grid>
</Window>
4

2 回答 2

0

1) 确保 h 图像已作为资源添加(您可以在图片属性中查看):

在此处输入图像描述

2) 将您的 uri 字符串 ( msdn ) 更改为:

 new BitmapImage(new Uri("/vm;component/h.png", UriKind.RelativeOrAbsolute));
于 2013-07-12T16:57:23.180 回答
0

首先,您需要检查为图像选择的构建操作类型(页面、资源、嵌入资源、内容...)。您可以通过在 Visual Studio 中检查图像的属性窗口来执行此操作。
然后检查如何为您的图像定义正确的 uri 路径。这取决于您的文件的位置。有关详细信息,请参阅WPF 中的 Pack URI

于 2013-07-12T16:51:01.493 回答