10

I am trying to create an ResourceFile Called DataTemplate.xaml in an external dll and use that in a WP7 page. When I do the following in the header of my Page I get an error

<ResourceDictionary Source="pack://application:,,,/WP7SharedClassLibrary;component/DataTemplate.xaml" />

The error is "Current project does not support 'application' as the authority component of the pack URI."

Has anyone else come across this and solved this?

Derik Whittaker
  • 832
  • 1
  • 7
  • 10

3 Answers3

13

I've managed to get this to work using the following steps:

  1. Created a standard WP7 application using the "Windows Phone Application" application template called "WP7ExternalResourcesTest".
  2. Added a project to the same solution using the "Windows Phone Class Library" template called "WP7ExternalResourcesTestLibrary".
  3. Removed the default Class.cs file form the library project.
  4. Added a file called "External.xaml" using the "XML file" template and set the "Build Action" to "Page".
  5. Added the following XAML to the new XAML file:

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <SolidColorBrush x:Key="ForegroundBrush" Color="Red" />
    </ResourceDictionary>
    
  6. Built the library project, and then added a reference to it from the WP7ExternalResourcesTest project.
  7. In WP7ExternalResourcesTest, opened App.xaml and changed the Application.Resources section to the following:

    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/WP7ExternalResourcesTestLibrary;component/External.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
    
  8. In MainPage.xaml, added Foreground="{StaticResource ForegroundBrush}" to the TextBlock named "PageTitle".
  9. Ran the application in the emulator. The end result was that the TextBlock correctly displayed the words "page name" in red.

Hope this helps.

Derek Lakin
  • 16,179
  • 36
  • 51
  • Doh! Looks like I forgot step 6 :-( In my case I had to reference not only the external project but also the WP_Utilities.DLL from another project that the external project was based on. Thanks for the detailed write up! – Greg Bray Jan 26 '11 at 22:22
  • Additionally, I've just discovered that WP8 doesn't like having a period in the referenced assembly name. If you're having trouble getting it to work, that could be your issue. – moswald Jun 20 '13 at 01:51
  • so do you really need to put the ResourceDictionary in a separate assembly? – George Birbilis Nov 11 '14 at 07:17
1

I tried the pack syntax while trying to share XAML ResourceDictionary files and got the same error message. I ended up using this syntax and it worked for me.

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/MyDLLName;component/Folder/MyXAMLFile.xaml"/>                
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>
Rich Reuter
  • 612
  • 5
  • 9
  • Doesn't work for me. Gives "Attribute /WP_Utilities;component/Resources/PerformanceProgressBar.xaml value is out of range. [Line: 17 Position: 37] --- Inner Exception --- KeyNotFoundException" error at runtime. What is the build type of your MyXAMLFile.xaml file? – Greg Bray Jan 23 '11 at 00:06
0

Silverlight doesn't support pack URIs. It's a WPF feature.

If you examine the type of the Source property for the Image object in Silverlight it is Uri. But in WPF the source is a dependency property with a type of ImageSource.

Simon Brangwin
  • 858
  • 1
  • 7
  • 15