2

In my VSTO Application, I need to use the ResourceDictionary which is in separate assembly.

I created a separate ResourceDictionary named generic.xaml in the VSTO application under themes folder like /themes/generic.xaml. In that file, I have referenced the ResourceDictionary from the external assembly using the Pack URIs.

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
   <ResourceDictionary.MergedDictionaries>
     <ResourceDictionary Source="pack://application:,,,/ExternalAssemblyName;component/Themes/resource.xaml"  />
   </ResourceDictionary.MergedDictionaries>
 </ResourceDictionary>

Also changed AssemblyInfo to the following:

[assembly: ThemeInfo(
ResourceDictionaryLocation.None,
ResourceDictionaryLocation.SourceAssembly)]

But still found no luck. Any help would be good.

P.S. Please don't suggest putting ResourceDictionary in App.Xaml because I am working on VSTO application so there will be no App.xaml.

Jayakrishnan
  • 4,232
  • 2
  • 23
  • 35
  • If the file name is generic.xaml then define the pack url as `pack://application:,,,/ExternalAssemblyName;component/Themes/generic.xaml`. Also, does you shared assembly is being loaded into AppDomain? – user1672994 May 16 '18 at 06:48
  • Yes, I have defined the pack url as per the file name of the resourcedictionary.No, Can you please tell me how to load the shared assembly into AppDomain? – Jayakrishnan May 16 '18 at 06:54

1 Answers1

1

Using VSTO does not prevent you from using App.xaml :)

  1. Create an App.xaml as you normally would and import resource dictionary in Application.Resources
  2. In add-in startup initialise WPF application

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        //Ensure the singleton WPF Application is instantiated
        if (System.Windows.Application.Current == null)
        {
            new App();
        }
    
        //Take control of WPF application shutdown
        System.Windows.Application.Current.ShutdownMode = System.Windows.ShutdownMode.OnExplicitShutdown;
    }
    
  3. Make sure to shutdown WPF application when closing add-in

    private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
    {
        if (System.Windows.Application.Current != null)
        {
            System.Windows.Application.Current.Shutdown();
        }
    }
    
  4. Use your styles in XAML

Example on Github

didzispetkus
  • 131
  • 5
  • Checked your GitHub project - and it does work! But inside the VS it reports an error in `MyWindow.xaml`, the error is: "MyLabelStyle could not be resolved". It does not prevent this to work, but it is not a way to work inside VS with errors. How can it be fixed? May be some dictionary reference? – Yeshurun Kubi Aug 03 '22 at 10:17