DeputyOfCopyPaster
DeputyOfCopyPaster

Reputation: 13

An image isn't loaded if I call MainWindow.xaml from another assembly

There are two assemblies there. For example A and B. The images are situated in the project A: /Resources/images/Question.png. When I call Window of assembly A from its project, - everything is OK. The picture is there.

When I call Window of assembly A from assembly B. -Window itself is OK. The picture is missing.

Is there a way to solve this issue?

 private void SetImage(string imageName)
    {
        string uri = string.Format("/Resources/images/{0}", imageName);
        var uriSource = new Uri(uri, UriKind.RelativeOrAbsolute);
        img.Source = new BitmapImage(uriSource);
    }

thank you.

Upvotes: 0

Views: 55

Answers (1)

Clemens
Clemens

Reputation: 128061

Add the name of the referenced assembly to the image's Pack URI:

private void SetImage(string imageName)
{
    var uri = "pack://application:,,,/AssemblyName;component/Resources/images/"
            + imageName;

    img.Source = new BitmapImage(new Uri(uri));
}

Replace AssemblyName by the name of your assembly that contains the image resource.

Upvotes: 1

Related Questions