1

您好我正在尝试将一个函数附加到用于输入一些输入信息的文本框,这些输入信息从资源字典加载到接口。这是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>

从上面的代码中,我尝试使用几种不同类型的控制事件。我成功地链接了我的函数将被放置的类,使用以下几行将类链接到资源字典。

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

这是我正在使用的课程的代码,

 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 中使用的任何事件时,与事件相关联的函数确实根本不火。

我不确定这是否是将函数链接到资源字典加载的事件的最佳方法,但任何人都可以为我遇到的这个问题提供一些指导。

4

1 回答 1

0

XAML 文件应依赖于其部分代码隐藏文件以使事件起作用。

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

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

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

附带说明一下,使它像这样工作的简单步骤是:

  1. UserControl在您的项目中添加一个空白。它会自动为您完成我上面提到的这项工作。
  2. 您只需将 XAML 文件更改为ResourceDictionary. (将 UserControl 替换为 ResourceDictionary)。
  3. 在后面的代码中,只需将基类从 更改UserControlResourceDictionary
于 2014-02-09T08:12:18.203 回答