0

I have below resource file:

namespace My.XAML
{
    partial class MessageBoxResources: ResourceDictionary
    {
        public MessageBoxResources()
        {
            InitializeComponent();  // <--- Compiler throws error here
        }

        private void OnButtonMouseLeave(object sender, MouseEventArgs e)
        {
            var btn = (Button)sender;
            var gradient = btn.Background as RadialGradientBrush;
            if (gradient == null)
                return;

            gradient.GradientOrigin = new Point(0.5, 0.5); 
            gradient.Center = gradient.GradientOrigin;
        }

        private void OnButtonMouseMove(object sender, MouseEventArgs e)
        {
            var btn = (Button)sender;
            var gradient = btn.Background as RadialGradientBrush;
            if (gradient == null)
                return;

            Point pt = Mouse.GetPosition(btn);          

            RadialGradientBrush rgb_new = gradient.Clone();
            rgb_new.GradientOrigin = new Point(pt.X / btn.ActualWidth, pt.Y / btn.ActualHeight);
            rgb_new.Center = gradient.GradientOrigin;
            btn.Background = (Brush)rgb_new;
        }
    }
}

Above resource file is attached to my Style file:

<ResourceDictionary 
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
       x:Class="My.XAML.MessageBoxResources">


    <RadialGradientBrush x:Key="gradRadial" RadiusX="0.25">
        <GradientStop Color="AliceBlue" Offset="0.0"/>
        <GradientStop Color="LightSteelBlue" Offset="1.0"/>
    </RadialGradientBrush>

    <Style TargetType="Button" 
           x:Key="MessageBoxButtonStyle">
        <Setter Property="Background" Value="{StaticResource gradRadial}"/>
        <Setter Property="TextBlock.TextAlignment" Value="Center" />
            <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Border  Name="Border" CornerRadius="0" 
                             BorderBrush="#000" BorderThickness="1,1,1,1" 
                             Background="{TemplateBinding Background}">
                        <ContentPresenter x:Name="contentPresenter" 
                                          ContentTemplate="{TemplateBinding ContentTemplate}" 
                                          Content="{TemplateBinding Content}" 
                                          HorizontalAlignment="{TemplateBinding HorizontalAlignment}" 
                                          Margin="{TemplateBinding Padding}" 
                                          VerticalAlignment="{TemplateBinding VerticalAlignment}" />
                    </Border>

                    <!-- TRIGGERS -->
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter TargetName="Border" Property="BorderBrush" Value="AliceBlue" />
                            <Setter Property="Cursor" Value="Hand" />
                        </Trigger>
                        <Trigger Property="IsPressed" Value="True">
                            <Setter TargetName="Border" Property="BorderBrush" Value="LightSteelBlue" />
                            <Setter Property="Background" Value="LightSteelBlue" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <EventSetter Event="MouseLeave" Handler="OnButtonMouseLeave"/>
        <EventSetter Event="MouseMove" Handler="OnButtonMouseMove"/>
    </Style>

</ResourceDictionary>

Compiler is throwing below message error within resource file, just in constructor:

The name 'InitializeComponent' does not exist in the current context

My current compilation action in resource file properties is set to "Compile". If I change it to "Resource" then it works, no compilation error appears but then another compilation error says that no method OnButtonMouseLeave is defined within My.XAML.MessageBoxResources. Also, this resource file is placed within a "Resources" folder. I wonder what is the correct compilation action that I should put for resource file, what is the correct compilation action?

Willy
  • 9,848
  • 22
  • 141
  • 284
  • But with action Compile it should not even work (I mean even without code behind) - how did you end up with that action? Anyway correct build action is "Page". – Evk Nov 13 '17 at 10:10
  • The Built Action of the XAML file should be `Page` and that of the code behind file `Compile`, as is the default. Also make sure the code behind file is called `MessageBoxResources.xaml.cs` – Clemens Nov 13 '17 at 10:13
  • @Clemens code behind file name seems to be irrelevant, I can place code-benind class anywhere (even together with any other classes). – Evk Nov 13 '17 at 10:14
  • @Clemens it's missing because wrong build action is set, so no code is generated. If OP switches build action to Page - InitializeComponent() will be accessible in partial class MessageBoxResources in namespace My.XAML, in whatever file that class in placed (not necessary MessageBoxResources.xaml.cs). – Evk Nov 13 '17 at 10:19
  • For your information, the style file is called 'MessageBoxStyle' and I created it from VS by add a new item -> WPF -> Resource Dictionary (xaml) so it does not have code-behind. The style file has set 'Page' as compilation action. Regarding resource file, MessageBoxResources, it has set 'Compilaton' as compilation action and it is only a cs file, MessageBoxResources.cs – Willy Nov 13 '17 at 10:20
  • 1
    As said, for a derived ResourceDictionary class called MessageBoxResources, you'll need a XAML file called `MessageBoxResources.xaml` with the default Build Action `Page`, and a code file called `MessageBoxResources.xaml.cs` with the default Build Action `Compile`. Go to your project and first add a new ResourceDictionary with the appropriate name, then manually add the code behind file. Read the accepted answer to the duplicate question. – Clemens Nov 13 '17 at 10:24
  • @Evk I did at home this weekend at it was working. Now at work is not. I proceeded as what you indicated in my previous post, that is: I created the style file (xaml file, lets say, "MessageBoxStyle.xaml", no xaml.cs, this file does not have xaml.cs code-behind) with compilation action set to Page. – Willy Nov 13 '17 at 10:46
  • @Evk Then I created separately a class file, and put there the code, this file is MessageBoxResources and inherits from ResourceDictionary as indicated in the post. This file is called MessageBoxResources.cs and no xaml associated. This file has "Compilation" as a compilation action. Finally I attach this file to my Style File "MessageBoxStyle.xaml" (which does not have code behind) by perform this: x:Class="My.XAML.MessageBoxResources" I understood that class file can have different name, and I can attach to a resource dictironary file. – Willy Nov 13 '17 at 10:46
  • Well it should work like this. I can only suggest to recheck all names and namespaces once again. Both .cs and .xaml files are in the same project, right? Clemens also claims that you should name your code-behind "MessageBoxStyle.xaml.cs", and while I don't see why it can affect anything - maybe worth trying. – Evk Nov 13 '17 at 10:55
  • @Evk Yes, they are in the same project but in different folders and so namespaces. What Clemens says is to manually add the code behind file. I wonder how can I create this manually. Renaming MessageBoxResources.cs to MessageBoxStyle.xaml.cs? It is a must that they have the same name? – Willy Nov 13 '17 at 11:03
  • As I said - I don't think so (that they must have the same name). Maybe you can attach some screenshots of your xaml and cs files so we are sure all is correct. – Evk Nov 13 '17 at 11:19
  • @Evk I have solved.The namespaces were the culprit.Both files,resource file MessageBoxResources.cs (plain cs file),and Style file MessageBoxStyle.xaml MUST be in the same namespace.If not,it does not work.File names can be different, it does not matter.Then I attach resource file to style file by importing it x:Class="My.XAML.MessageBoxResources". Also style file set to Page as compilation mode and resource file set to "Compilation". – Willy Nov 13 '17 at 11:33
  • @Evk When I tested at home this weekend it was working because they were in the same namespace.Now at work,they were at different namespaces hence it was failing. – Willy Nov 13 '17 at 11:33

0 Answers0