Control event not firing from class linked to reso

2019-08-01 07:56发布

Hi I am attempting to attach a function to text box used for entering some input information which is loaded to an interface from resource dictionary. Here is the XML,

          <ContentControl>
            <Grid>
                <Image s:DesignerItem.Code ="1773"  IsHitTestVisible="False" Stretch="Fill" Source="../Images/addition.png"  ToolTip="Addition" />
                <TextBox Height="57" Width="56" Margin="13,13,130,13" BorderThickness="0" FontSize="45" HorizontalAlignment="Center" VerticalAlignment="Center" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" TextChanged="msg_TextChange" KeyUp="msg_MouseDown"/>
                <TextBox Height="57" Width="56" Margin="132,13,12,13" BorderThickness="0" FontSize="45" HorizontalAlignment="Center" VerticalAlignment="Center" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" KeyDown="msg_MouseDown"/>
                <Button MouseDown="msg" Width="50" Height="20">ck</Button>
            </Grid>
           </ContentControl>

From the above code, I attempted to use a few different types of control events. I successfully linked the class my functions are going to be placed in using the following lines to link the class to the resource dictionary.

 x:Class="DiagramDesigner.CodeBuilding"
 x:ClassModifier="public"

Here is the code for the class I am using,

 public partial class CodeBuilding : ResourceDictionary
{
   public CodeBuilding()
   {
      InitializeComponent();
   } 

   public void msg_TextChange(object sender,EventArgs e)
   {
       MessageBox.Show("oh no, you've got me ...");
   }

   public void msg(object sender, MouseEventArgs e)
   {
       MessageBox.Show("oh no, you've got me ...");
   }
}

As you can see, I am just using a simple message to indicate if the event has been fired, the project successfully builds and runs fine, but when I attempt to trigger any of the events used in the XML the function tied to the event does not fire at all.

I am not certain if this is the best method of linking a function to an event loaded by a resource dictionary, but can anyone provide some guidance to this problem I am experiencing.

1条回答
叛逆
2楼-- · 2019-08-01 08:17

XAML file should be dependent on its partial code behind file for events to work.

Make sure Build Action for code behind file is set as Compile.

Also open your .csproj file in notepad or in any text editor and make sure DepedentUpon attribute is set on XAML file. It will look like this:

<Compile Include="CodeBuilding.xaml.cs">
  <DependentUpon>CodeBuilding.xaml</DependentUpon>
</Compile>

On a side note, simple steps to make it work like that are:

  1. Add a blank UserControl in your project. It will automatically do this work for you which I mentioned above.
  2. All you need to do is simply change the XAML file to ResourceDictionary. (Replace UserControl to ResourceDictionary).
  3. And in code behind just change the base class from UserControl to ResourceDictionary.
查看更多
登录 后发表回答