简体   繁体   中英

Using a user control library as dll reference in WPF

I have a user control library project with some resource dictionarys in it, created with Microsoft Visual Studio 2017. The resource dictionarys contain XAML-styles which I want to make available for other developers in the team to use them in different WPF-App projects. Duplicating the user control library project into the WPF-App projects isn't considered a good solution. I tried the following:

  • Adding the .dll from UserControleLibraryProjectName/bin/debug/ to references in the WPF-App.
  • Importing the used resource dictionarys into the WPF-App by adding this to the app.xaml:

     <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="pack://application:,,,/UserControleLibraryProjectName;component/ResourceDictionaryName.xaml" /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources> 
  • Referencing the resource-dictionary entries as usual in the MainWindow.xaml : For example Style="{StaticResource style1}" .

It compiles without error, but crashes at startup because it can't find the referenced resources. ( System.Windows.Markup.XamlParseException in System.Windows.ResourceDictionary.Source ) I'm pretty sure that the syntax of the source tag where I reference the resource dictionary is wrong, but I tried a lot of different ways... Btw. there is no error within the user control library project nor the MainWindow.xaml where I reference the dictionary entries - It works fine when I copy the resource dictionary code directly into the app.xaml .

Do you know how to use a referenced dll correctly?

Would suggest looking a the the answers to this question as the path looks good as far as I can see.

Loading merged ResourceDictionary from different assembly fails

Your source looks correctly. It is:

<ResourceDictionary Source="pack://application:,,,/[AssemblyName];component/[PathToFile].xaml" />

You also need to a reference your ClassLibrary in your app in order for it to work.

If this doesn't work, first of all, check in your project properties if your assembly name is the same as the one you put in your App.xaml.

Thanks so far, I found the problem: The .net version of the user control library project was higher that that of the WPF-App projects. Because of the resulting incompatibilities Visual Studio didn't copy the .dll into the bin/debug/ folder of the WPF-App project. Also, I had to set the build type of the referenced user controle library to resource .

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