locked
Resource Dictionary from a seperate folder RRS feed

  • Question

  • Hi,

    I am keeping a xaml file (resource dictionary file) in a seperate folder say, C:\ProgramData\Myfolder\MyRD.xaml.

    How can I refer this resorce dictionary in my WPF applications ? That means, in my wpf apps, i have so many projects and in each projects, i have lot of xamls. I need to call the MyRD.xaml from each xamls from my WPF projects ? I tried this way, but this is not working.

    <
    ResourceDictionary
    >
    
    < ResourceDictionary.MergedDictionaries >
    < ResourceDictionary Source = " C:\ProgramData\Myfolder\MyRD.xaml" />
    </ ResourceDictionary.MergedDictionaries >
    </ ResourceDictionary >

    How can I use it ?
    Thanks in advance

     

     

    Tuesday, May 4, 2010 7:54 AM

Answers

  • If you want to keep the file separate and uncompiled then you need to write code to load it, there was a recent question on this.  I thought at the time you'd have to use loadxaml but I seem to recall someone said they got application.load to work. 

    http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/caa9b66f-f757-49d5-80f9-f50e7cdbc2f5

    The simplest approach and probably the most robust is to just compile into the app as a resource.  This way your user doesn't decide to delete the xaml because he doesn't understand what it is.

    • Marked as answer by Jie Bao Tuesday, May 11, 2010 1:22 AM
    Tuesday, May 4, 2010 10:15 AM
  • Hi,

     

    It is not advisable to keep your Resource dictionary file in physical location (i.e. in local disk). I am suggesting you to keep all your resource dictionary files in separate library file and refer that library to all of your rest of project where you want to use these files.

     

    Use the following code for loading and merging the dictionary file in your application. You would have to load the file using FileStream and then load the content of file using XamlReader class. Hope it should help.

     

          FileStream file = new FileStream(@"C:\ProgramData\Myfolder\MyRD.xaml", FileMode.Open);
          ResourceDictionary resource = XamlReader.Load(file) as ResourceDictionary;
          var resourceDictionaries = Application.Current.Resources.MergedDictionaries;
          if (resource != null)
          {
            resourceDictionaries.Add(resource);
          }

     

     
    Prabu
    • Proposed as answer by Jarrey Wednesday, May 5, 2010 7:29 AM
    • Marked as answer by Jie Bao Tuesday, May 11, 2010 1:22 AM
    Tuesday, May 4, 2010 10:49 AM
  • Hi uksoftengg,

    I agree with Prabu.P 's solution, you could use XamlReader to load the loose XAML resource dictionary file into application.

    For your original way, you should use the below code:

    <Application.Resources>
      <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
          <ResourceDictionary Source="file://C:/ProgramData/Myfolder/MyRD.xaml" />
        </ResourceDictionary.MergedDictionaries>
        <!-- Other Resources here -->
      </ResourceDictionary>
    </Application.Resources>

    Sincerely,

    Bob Bao


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Marked as answer by Jie Bao Tuesday, May 11, 2010 1:22 AM
    Wednesday, May 5, 2010 7:46 AM

All replies

  • I'm sure: do you want to have this resource as a part of the project? If so, just set Build action to Resource and instead of file path use the correct Uri.
    Tuesday, May 4, 2010 8:06 AM
  • If you want to keep the file separate and uncompiled then you need to write code to load it, there was a recent question on this.  I thought at the time you'd have to use loadxaml but I seem to recall someone said they got application.load to work. 

    http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/caa9b66f-f757-49d5-80f9-f50e7cdbc2f5

    The simplest approach and probably the most robust is to just compile into the app as a resource.  This way your user doesn't decide to delete the xaml because he doesn't understand what it is.

    • Marked as answer by Jie Bao Tuesday, May 11, 2010 1:22 AM
    Tuesday, May 4, 2010 10:15 AM
  • Hi,

     

    It is not advisable to keep your Resource dictionary file in physical location (i.e. in local disk). I am suggesting you to keep all your resource dictionary files in separate library file and refer that library to all of your rest of project where you want to use these files.

     

    Use the following code for loading and merging the dictionary file in your application. You would have to load the file using FileStream and then load the content of file using XamlReader class. Hope it should help.

     

          FileStream file = new FileStream(@"C:\ProgramData\Myfolder\MyRD.xaml", FileMode.Open);
          ResourceDictionary resource = XamlReader.Load(file) as ResourceDictionary;
          var resourceDictionaries = Application.Current.Resources.MergedDictionaries;
          if (resource != null)
          {
            resourceDictionaries.Add(resource);
          }

     

     
    Prabu
    • Proposed as answer by Jarrey Wednesday, May 5, 2010 7:29 AM
    • Marked as answer by Jie Bao Tuesday, May 11, 2010 1:22 AM
    Tuesday, May 4, 2010 10:49 AM
  • Hi uksoftengg,

    I agree with Prabu.P 's solution, you could use XamlReader to load the loose XAML resource dictionary file into application.

    For your original way, you should use the below code:

    <Application.Resources>
      <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
          <ResourceDictionary Source="file://C:/ProgramData/Myfolder/MyRD.xaml" />
        </ResourceDictionary.MergedDictionaries>
        <!-- Other Resources here -->
      </ResourceDictionary>
    </Application.Resources>

    Sincerely,

    Bob Bao


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Marked as answer by Jie Bao Tuesday, May 11, 2010 1:22 AM
    Wednesday, May 5, 2010 7:46 AM
  • Using VS you can add the file to each individual project as a link.  This will allow you to use the file as a normal resource dictionary but store it in a central place.  Otherwise you would have to deploy the Xaml onto client machines as well.
    Saturday, May 8, 2010 5:02 AM