5

I'm working on a WPF class library, not an application. Here is an example of a Label I'm making in c# and I'd like to "style" it using XAML.

   private void CreateElement(int i)
    {
        UIElementOut[i] = new Label();
        var uiElement = (Label)UIElementOut[i];
        uiElement.HorizontalAlignment = HorizontalAlignment.Center;
        uiElement.VerticalAlignment = VerticalAlignment.Center;
        uiElement.FontFamily = new FontFamily(FFontInput[i]);
        uiElement.FontSize = Convert.ToDouble(FontSizeIn[i]);
        uiElement.Content = TextIn[i];
        Brush BgBrushColor = new SolidColorBrush(RGBAToMediaColor(FBgCol[i]));
        Brush FgBrushColor = new SolidColorBrush(RGBAToMediaColor(FFgCol[i]));
        uiElement.Background = BgBrushColor;
        uiElement.Foreground = FgBrushColor;
        Uri uri = new Uri("Styles/LabelStyle.xaml", UriKind.Relative);
        StreamResourceInfo info = Application.GetContentStream(uri);
        System.Windows.Markup.XamlReader reader = new System.Windows.Markup.XamlReader();
        ResourceDictionary myResourceDictionary = (ResourceDictionary)reader.LoadAsync(info.Stream);
        Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
        Style myLabelStyle = myResourceDictionary["LabelStyle"] as Style;
        uiElement.Style = myLabelStyle;
    }

For this I have ressourcedictionnary containing my LabelStyle, everything is compiling without problem.

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:local="clr-namespace:WpfApplication1">
<Style x:Key="LabelStyle" TargetType="{x:Type Label}">
    <Setter Property="Height" Value="53" />
    <Setter Property="Width" Value="130" />
    <Setter Property="HorizontalAlignment" Value="Left" />
    <Setter Property="Margin" Value="99,71,0,0" />
    <Setter Property="VerticalAlignment" Value= "Top" />
    <Setter Property="Foreground" Value="#FFE75959" />
    <Setter Property="FontFamily" Value="Calibri" />
    <Setter Property="FontSize" Value="40" />
</Style>

but when I use my dll later on, the style is not applied and I have this error message :

ERR : Assembly.GetEntryAssembly() returns null. Set the Application.ResourceAssembly property or use the pack://application:,,,/assemblyname;component/ syntax to specify the assembly to load the resource from.

here is my actual App.xaml with build action setup to page :

<Application x:Class="WpfApplication1.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:local="clr-namespace:WpfApplication1"
         StartupUri="MainWindow.xaml">
<Application.Resources>

</Application.Resources>

How to specify the assembly to load the resource from ? I'm fairly new to WPF and I'm stuck on this problem, thanks in advance.

EDIT 1 :

I tried as my Assembly Name is WpfApplication1 (see here http://postimg.org/image/ksyj9xi5p/)

ResourceDictionary myResourceDictionary = Application.LoadComponent(new Uri("/WpfApplication1;component/Styles/LabelStyle.xaml", UriKind.RelativeOrAbsolute)) as ResourceDictionary;

instead of

ResourceDictionary myResourceDictionary = (ResourceDictionary)reader.LoadAsync(info.Stream);

and get the same error.

lecloneur
  • 424
  • 5
  • 20
  • possible duplicate of [ResourceDictionary in a separate assembly](http://stackoverflow.com/questions/338056/resourcedictionary-in-a-separate-assembly) – Colin Grealy Sep 17 '15 at 04:39
  • seems like this http://stackoverflow.com/questions/706700/load-wpf-styles-or-other-static-resources-from-an-external-file-or-assembly?rq=1 would help. – J R B Sep 17 '15 at 05:21
  • Did you replace `assemblyname` by the actual name of your assembly? – Clemens Sep 17 '15 at 06:24
  • @Clemens yes I did, and get the same error, my assembly name in my solution is : AssemblyInfo.cs – lecloneur Sep 17 '15 at 07:15
  • That's not the assembly name but just the AssemblyInfo file. Take a look at the project properties under the "Application" tab. There you'll find the actual assembly name. – Clemens Sep 17 '15 at 07:28
  • @Clemens I did check and the name was wrong, I edited my first post, actual assembly name is WpfApplication1 but error is still there. – lecloneur Sep 17 '15 at 07:44
  • WpfApplication1 is certainly not the assembly name of your class library. Look at the class library's properties page. – Clemens Sep 17 '15 at 08:11
  • @Clemens, I think it is, see this screenshot : http://postimg.org/image/ksyj9xi5p/ – lecloneur Sep 17 '15 at 09:14

3 Answers3

2

Did you try to replace your

Uri uri = new Uri("Styles/LabelStyle.xaml", UriKind.Relative);

by the suggestion that is indicated in you error, that is using the "Pack" syntax ?

pack://application:,,,/assemblyname;component/

Given the information you provided

Uri uri = new Uri("pack://application:,,,/WpfApplication1;component/Styles/LabelStyle.xaml", UriKind.Relative);
Bruno
  • 1,944
  • 13
  • 22
  • 1
    I'm getting there I think but I've got this error message now : "A relative URI cannot be created because the 'uriString' parameter represents an absolute URI." But if I change to absolute, it says "Cannot use absolute URI", any hint ? thank you – lecloneur Sep 24 '15 at 05:57
0

This might help you

ResourceDictionary myResourceDictionary = Application.LoadComponent(new Uri("/assemblyname;component/Styles/LabelStyle.xaml", UriKind.RelativeOrAbsolute)) as ResourceDictionary;

You can then find resources within it in the usual manner, e.g., myDictionary["LabelStyle"]

Mohit S
  • 13,723
  • 6
  • 34
  • 69
  • thanks but I'm adding those lines : ResourceDictionary myResourceDictionary = Application.LoadComponent(new Uri("/assemblyname;component/Styles/LabelStyle.xaml", UriKind.RelativeOrAbsolute)) as ResourceDictionary; Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary); Style myLabelStyle = myResourceDictionary["LabelStyle"] as Style; uiElement.Style = myLabelStyle; and I still have the error – lecloneur Sep 17 '15 at 05:01
0

You can set the property: System.Windows.Application.ResourceAssembly as suggested in the error message. Read carefully the documentation. Note that the property can only be set once.

Maxence
  • 12,868
  • 5
  • 57
  • 69