简体   繁体   中英

How can I address (hopefully in XAML) a linked resource properly in WPF?

I'm trying to do something rather simple: add a simple icon to the main window of the application. This can be done rather easily. The icon itself is placed in the Resources directory and its build action is set to Resource. The XAML code for the window includes a reference to this icon:

Icon="Resources/wiser.ico"

This works just fine. However, what I really want to do is grab this icon from another place, a directory of images shared across several versions of our application (we support about 5 platforms depending on how you count). In our WinForms past, this was done by adding the resource as an existing item and choosing 'Add as Link' from the dialog. This, however, causes issues with the icon noted above.

First, it doesn't end up in the proper place in the assembly. Reflector finds it in the base path of the application, not within the Resources directory where it should be. A non-linked version of the icon does indeed appear in the Resources directory.

Second, a fix for the path (Icon="wiser.ico") allows things to compile but makes the designer throw an error, rendering the designer itself useless.

I suspect this is just a bug (or two, depending on how you count). Is there a simple way around this? Note that I had hoped to support linking from our source control system instead but that doesn't appear to be supported by SVN and the end result would likely be a touch confusing to maintain.

Two things that I can think of for sharing resources:

1) You could put your resources in a seperate assembly that you could share across your applications.

Use this syntax for getting the ico or other items (png, xaml, etc) out of the assembly:

Icon="/CommonResources;component/app.ico"

or using pack syntax

Icon="pack://application:,,,/CommonResources;component/app.ico"

2) You could use the pack application syntax to load the ico in a relative file path from your assembly.

Icon="pack://siteoforigin:,,,/app.ico"

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