fhnaseer
fhnaseer

Reputation: 7277

Using Resources in different WPF xaml user control

I have two projects. One is a library and other one is a WPF application. In my library project I created a resources file (Resources.resx) and added some string in it and made its access as public.

Now in my WPF project I added reference of library project. Now I am trying to use library resource file in my XAML control.

Here is my code. Here MyApplication is wpf project and MyLibrary is dll project.

<UserControl x:Class="MyApplication.LauncherView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:p="clr-namespace:MyLibrary"
    <StackPanel>
        <Button Content="{x:Static p:Resources.TextFromResource}" Command="{Binding LaunchCommand}" />
    </StackPanel>
</UserControl>

But at the end I receive these errors.

Error 1 The name "Resources" does not exist in the namespace "clr-namespace:MyLibrary". Error 2 Cannot find the type 'Resources'. Note that type names are case sensitive.

Upvotes: 2

Views: 4205

Answers (2)

Arsen Mkrtchyan
Arsen Mkrtchyan

Reputation: 50712

For using the class, not in your assembly, you need to define assembly

xmlns:p="clr-namespace:MyNamespace;assembly=MyLibrary"

But for including resource, you don't need to include namespace, because it is not a class, and doesn't have namespace...you need to include resource file.. here is the right way

<ResourceDictionary Source="pack://application:,,,/MyLibrary;component/Subfolder/MyResourceFile.xaml"/>

After which you can use

Content="{x:StaticResource TextFromResourceKey}"

Upvotes: 2

J R B
J R B

Reputation: 2136

You have use merge dictionary.

< ResourceDictionary.MergedResources> < ResourceDictionary Source="pack://application:,,,/MyLibrary;component/component/Subfolder/MyResourceFile.xaml"/ > < /ResourceDictionary.MergedResources>

after that you can use it.

Upvotes: -1

Related Questions