简体   繁体   中英

What is the correct way to define images in XAML

Currently I add an image in XAML this way :

  • Put the file in the project's folder, in a sub-directory named "Resources",
  • Set its "Build Action" property to "Resource",
  • Add in my XAML file : <Image Source="/Resources/myImage.png />

And it works great.

But in SO I keep seeing people writing this instead :

<Image Source="pack://application:,,,/MyApplicationNamespace;component/Resources/myImage.jpg" />

Also recently, I've found that in Project Properties -> Resources, you can add files like images, texts...

So which of these 3 possibilities should I use ?

The Image Source="/Resources/myImage.png syntax used in your XAML to refer to an image in a sub folder is actually equivalent to the following syntax pack://application:,,,/Resources/myImage.jpg and this is one of the variations used to access binary resources in XAML.

Since the first two options are the same written differently this leaves us with the third option.

When to use the resource file ?

I usually tend to use them when i have different resources assemblies that are used as satellite assemblies which are in turn used for localization to different cultures. Also they can be used when you want to access those resources and switch them at run-time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM