0

I tried to add all my resources to a resource template file, and incorporate them as dynamic resources (precisely the way Blend does this too). The programme compiles and runs fine, but the styles aren't applied at all. Bizarrely, they are applied in the preview, though. This is the code I have:

// MyUserControl.xaml
<UserControl x:Class="MyProject.MyUserControl"
         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" mc:Ignorable="d"
         d:DesignHeight="411" d:DesignWidth="645">
     <Grid>
        <Button Content="Click here" Style="{DynamicResource MyButtonStyle}" />                
     </Grid>
 </UserControl>

-

// MyStyleTemplates.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Style x:Key="MyButtonStyle" TargetType="{x:Type Button}">
        <Setter Property="Background">
            <Setter.Value>
                <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                    <GradientStop Color="#FF263B5B" Offset="0"/>
                    <GradientStop Color="#FF65FD43" Offset="1"/>
                </LinearGradientBrush>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

-

// App.xaml
<Application x:Class="MyProject.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/MyProject;component/MyStyleTemplates.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

I tried to do exactly the same but then putting the Style code within the <UserControl.Resources> tag in the MyUserControl.xaml file, and that worked fine again, so nothing is wrong with the Style itself. Why does this happen?

Edit:

As this may have something to do with it, the folder structure may be slightly unconventional, as follows:

MyProjectFolder /
    MyProject.csproj
    MyStyleTemplates.xaml
    Main /
       App.xaml
    GUI /
       MyUserControl.xaml
       MyUserControl.xaml.cs

Is this allowed?

Yellow
  • 3,955
  • 6
  • 45
  • 74
  • Try a StaticResource rather than a Dynamicresource – Michael G Nov 14 '13 at 18:14
  • 1
    I was not able to reproduce the issue with the code provided here. Is the MyStyleTemplates resource dictionary in the same assembly as the app.xaml? Are you actually using the UserControl? – Michael G Nov 14 '13 at 18:33
  • Using `StaticResource` crashes the programme. I updated the question and included the file structure. Is there stuff in there that is not allowed? – Yellow Nov 15 '13 at 10:32
  • Is this all in the same project? – Michael G Nov 15 '13 at 15:19
  • Yes, it's all in the same project and same solution. And also Blend previews it correctly, but then runs it without the style. – Yellow Nov 15 '13 at 16:03
  • I've created a project with the code you provided, with the same folder structures. I'm able to see the button color without issue. There must be another style or resource that is overriding your MyStyleTemplates.xaml file. – Michael G Nov 15 '13 at 16:32
  • So strange... this surely is the only style in the entire project. Obviously my code here is a much simplified version though, so maybe something else is obfuscating the result... I'll have another look. – Yellow Nov 15 '13 at 16:37
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/41278/discussion-between-michael-g-and-yellow) – Michael G Nov 15 '13 at 16:40

2 Answers2

0

In your App.xaml change the ResourceDictionary merge as below

<ResourceDictionary Source="pack://application:,,,/MyProject;component/MyStyleTemplates.xaml"/>
Kumareshan
  • 1,311
  • 8
  • 8
  • Same thing: the preview is correct and it compiles, but the actual application doesn't change appearance. Could you maybe be more specific on what this addition is supposed to do? – Yellow Nov 14 '13 at 16:25
  • 3
    Why downvote this answer?? What is sooo wrong with it? It might not be a perfect answer, and I would not upvote it, but why downvote? +1 for compensation... – gehho Nov 15 '13 at 10:36
  • I don't mean any offence, but the answer is not constructive, didn't actually help, doesn't provide any explanation on why the given solution should help, and lastly, it's exactly the same as the answer given here (http://stackoverflow.com/questions/338056/resourcedictionary-in-a-separate-assembly) which I had already tried. So I wouldn't want anyone experiencing the same problem think that this is the correct solution. – Yellow Nov 15 '13 at 16:08
0

The reason was that I wanted to define my own Main() method, so I had changed the App.xaml file tag in the .csproj file from ApplicationDefition to simply Page. This works, but NOT if you also want to use resources in the manner described above.

The only trouble is that VS doesn't warn you about this, and even shows the previews correctly. Changing the tag back to ApplicationDefition solves the problem, although then you need to find another way of defining your own main method.

Yellow
  • 3,955
  • 6
  • 45
  • 74