locked
Sharing style sheet between Silverlight and WPF, I'm going mad RRS feed

  • Question

  • I really don't know what to do. I'm now reading them in ok, although it only works when I don't specify a directory.Of note, my style sheet is a linked file so I can share it.

        <Application.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="StyleMain.xaml" />
                    <ResourceDictionary Source="StyleBrush.xaml" />
                    <ResourceDictionary Source="StylesCommon.xaml" />
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
        </Application.Resources>

    This works but when I try the correct way like:

    <ResourceDictionary Source="MyApp;component/Assets/StyleCommon.xaml" />


    It won't find it.  Oh well, that's no big deal I guess.  But I'm getting errors.  When I'm running.  Compile wise it seems ok.

    From what I gather, I have Build Action to 'Page' Copy to Output to 'Do not copy'.  I've read that's what I'm supposed to do.

    But, then my intelisense will sometimes say, 'Build Action' is not set to resource.

    So, it seems to work even better when I put the stylesheets in the root of my app. I don't have so many, so oh well.  Intellisense always has a fit with my stylesheets.   Lots of not founds, but it seems to find it when I run it.  I hope VS 2012 does this better.  Anything I'm doing wrong?


    dan

    Thursday, September 13, 2012 10:41 PM

Answers

  • You can use relative addressing or absolute addressing.

    So my experimental wpf application has this:

    <Application x:Class="WpfApplication3.App"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 StartupUri="MainWindow.xaml">
        <Application.Resources>
            <ResourceDictionary>
    
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary 
               Source="D:\Biz\WPF\WpfApplication1\WpfApplication1\Dictionary1.xaml"/>
    
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
        </Application.Resources>
    
    </Application>

    • Marked as answer by danparker276 Saturday, September 22, 2012 5:54 PM
    Friday, September 14, 2012 6:42 PM

All replies

  • Hi Dan,

    Try this,

    <ResourceDictionary Source="pack://application:,,,/MyApp;component/Assets/StyleCommon.xaml" />

    • Proposed as answer by Pete LakerMVP Saturday, September 22, 2012 5:24 PM
    Friday, September 14, 2012 2:19 AM
  • Hi Dan,

    Try this,

    <ResourceDictionary Source="pack://application:,,,/MyApp;component/Assets/StyleCommon.xaml" />

    Nope, the intelisense finds it, but I get the same error when I run the debug and it can't find it.

    "{"Cannot locate resource 'assets/stylescommon.xaml'."}"


    dan

    Friday, September 14, 2012 2:29 AM
  • It might sound obvious, but did you remember to reference the project with the assets in?
    • Proposed as answer by Pete LakerMVP Saturday, September 22, 2012 5:24 PM
    Friday, September 14, 2012 2:39 AM
  • How would I do that?  I'm basically converting it from Silverlight and I don't know much WPF.

    In the properties, there's a refrenece Paths.  I added the assets folder, but that didn't do anything.


    dan

    Friday, September 14, 2012 3:05 AM
  • We might be talking at cross purposes here, I don't know.

    Your external resource dictionaries are in a separate project for your assets called "MyApp", which is just a straightforward c# class project.

    This asset project needs to reference PresentationCore, PresentationFramework, System.Xaml and WindowsBase 

    The ResourceDictionaries build action are all set to "Page" and "Do not copy" and MSBuild.Compile.

    I'm using .Net 4.5 and this works for me

    • Proposed as answer by Pete LakerMVP Saturday, September 22, 2012 5:24 PM
    Friday, September 14, 2012 4:05 AM
  • It's not a project though.  It's just a xaml style sheet and I'm just adding it as a link to a folder in the MyApp project.  Do I have to make a separate project?  I'm just using a folder called Assets because that's sort of a common directory people use in silverlight to put resources.

    <ResourceDictionary
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:system="clr-namespace:System;assembly=mscorlib" 
        xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
    >

    Everything works fine when I put it in the root directory though.  I saw someone else post that as their solution, I'm just wondering why it isn't working.


    dan

    Friday, September 14, 2012 4:18 AM
  • As I understand it Reference Paths are only for assemblies and not xaml style sheets, but I might be wrong. The only other way is to add them as existing files to you wpf project.
    • Proposed as answer by Pete LakerMVP Saturday, September 22, 2012 5:24 PM
    Friday, September 14, 2012 6:03 AM
  • yeah, that's what I'm trying to do:

        <Application.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="StyleMain.xaml" />
                    <ResourceDictionary Source="StyleBrush.xaml" />
                    <ResourceDictionary Source="MyApp;component/Assets/StylesCommon.xaml" />
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
        </Application.Resources>


    dan

    Friday, September 14, 2012 6:35 AM
  • In Visual Studio add a new folder to your project called Assets. In Windows Explorer copy xaml style sheet into it. In Visual Studio right click on your project and "Add Existing Item".

    <ResourceDictionary Source="/Assets/StyleMain.xaml" />



    • Proposed as answer by Pete LakerMVP Saturday, September 22, 2012 5:23 PM
    Friday, September 14, 2012 6:51 AM
  • I guess it doesn't accept linked files in directories for style sheets.

    I made a new directory Assets2 and add the StylesCommon (not as link).  It worked fine.

    Then I deleted it, and added StylesCommon "add as link" and it failed.  Is this a bug?  It works fine when I do it in silverlight.  For now I'll just keep it in the root because I don't have that many style sheets.

    I kept the same for both:

      <ResourceDictionary Source="Assets2/StylesCommon.xaml" />


    dan

    Friday, September 14, 2012 4:13 PM
  • You can use relative addressing or absolute addressing.

    So my experimental wpf application has this:

    <Application x:Class="WpfApplication3.App"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 StartupUri="MainWindow.xaml">
        <Application.Resources>
            <ResourceDictionary>
    
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary 
               Source="D:\Biz\WPF\WpfApplication1\WpfApplication1\Dictionary1.xaml"/>
    
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
        </Application.Resources>
    
    </Application>

    • Marked as answer by danparker276 Saturday, September 22, 2012 5:54 PM
    Friday, September 14, 2012 6:42 PM
  • Yeah, that will probably work too, but I'd rather just stick it in my root than do that.  Can't believe this is a bug or still a bug.  I'm sure many people would add a style sheet as a link.

    dan

    Friday, September 14, 2012 7:07 PM