Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从另一个dll中的视图模型绑定图像?_C#_Wpf_Image_Binding - Fatal编程技术网

C# 如何从另一个dll中的视图模型绑定图像?

C# 如何从另一个dll中的视图模型绑定图像?,c#,wpf,image,binding,C#,Wpf,Image,Binding,我有一个WPF窗口项目和一个图像控件。 我写了另一个dll项目,这将是窗口的视图模型。 这个dll项目包括一个名为“h.png”的png文件,我将把它作为源绑定到WPF窗口项目。 我设置了图像绑定,但不起作用, 谁能告诉我如何从另一个dll项目绑定图像源 我的c#代码: 我的xaml: <Window x:Class="ImageBindingApplication.MainWindow" xmlns="http://schemas.microsoft.com/winfx/

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

我的c#代码:

我的xaml:

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

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

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

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

<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>
 new BitmapImage(new Uri("/vm;component/h.png", UriKind.RelativeOrAbsolute));