trobboda
trobboda

Reputation: 53

Why can I retrive an image with absolute path in XAML but it doesn't work behind code?

Trying to understand Assembly, Uri and all of those things and there is some stuff I don't understand and hopefully I can ask them here in one go.

I have a solution that contains two "Assembly's" if I understood it correctly like this.

Project Assemblies

Where I want my FetchResources to fetch that image and display it on my MainWindow.

It works without a problem when I do it in xaml like this.

Xaml FetchResources

But in behind-code I use the same Uri but for some reason it won't display the image.

var uri = new Uri("pack://application,,,/WPF_UserControll_Test;component/Images/71805972.jpg", UriKind.Absolute);
BitmapImage bmi = new BitmapImage();
bmi.UriSource = uri;
testimage.Source = bmi;

I'm not sure if I've understood Assembly correctly nor do I understand Uri to the full extent. I've read Microsofts Pack URIs in WPF but it's not clear to me how it works.

Why Can I reach the image through XAML but not in behind-code?

Upvotes: 1

Views: 205

Answers (2)

Bharadhi Kugarajah
Bharadhi Kugarajah

Reputation: 16

Try this:

MSDN

The following example shows the pack URI for a XAML resource file that is located in the root folder of a referenced, version-specific assembly's project folder.

pack://application:,,,/ReferencedAssembly;v1.0.0.1;component/ResourceFile.xaml

Upvotes: 0

mm8
mm8

Reputation: 169150

You are missing a colon (:) after "application". Try this:

testimage.Source = new BitmapImage(new Uri("pack://application:,,,/WPF_UserControll_Test;component/Images/71805972.jpg", UriKind.Absolute));

And if you don't use the constructor overload that accepts a Uri, you should call the BeginInit() and EndInit() methods before and after you set the UriSource property:

BitmapImage bmi = new BitmapImage();
bmi.BeginInit();
bmi.UriSource = new Uri("pack://application:,,,/WPF_UserControll_Test;component/Images/71805972.jpg", UriKind.Absolute);
bmi.EndInit();
testimage.Source = bmi;

Upvotes: 1

Related Questions