控制事件未从链接到资源字典 wpf 的类触发 [英] Control event not firing from class linked to resource dictionary wpf

查看:44
本文介绍了控制事件未从链接到资源字典 wpf 的类触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在尝试将一个函数附加到用于输入一些从资源字典加载到界面的输入信息的文本框.这是 XML,

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 ...");
   }
}

如您所见,我只是使用一条简单的消息来指示事件是否已被触发,项目成功构建并运行良好,但是当我尝试触发任何在 XML 中使用的事件与事件相关的函数根本不会触发.

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.

推荐答案

XAML 文件应该依赖其部分代码隐藏文件才能使事件工作.

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

确保代码隐藏文件的构建操作设置为编译.

同时在记事本或任何文本编辑器中打开您的 .csproj 文件,并确保在 XAML 文件上设置了 DepedentUpon 属性.它看起来像这样:

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. 在您的项目中添加一个空白的 UserControl.它会自动为您完成我上面提到的这项工作.
  2. 您只需将 XAML 文件更改为 ResourceDictionary.(将 UserControl 替换为 ResourceDictionary).
  3. 在后面的代码中,只需将基类从 UserControl 更改为 ResourceDictionary.
  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.

这篇关于控制事件未从链接到资源字典 wpf 的类触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆