我有一个资源字典,其中包含一个上下文菜单:
<ResourceDictionary x:Class="MyApp.Components.MyContextMenu"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyApp"
xmlns:components="clr-namespace:MyApp.Components">
<ContextMenu ContextMenuOpening="OnContextMenuOpening">
资源字典的 XAML 具有以下代码:
using System;
using System.Windows;
using System.Windows.Controls;
namespace MyApp.Components
{
public partial class MyContextMenu : ResourceDictionary
{
public MyContextMenu()
{
InitializeComponent();
}
void OnContextMenuOpening(object sender, ContextMenuEventArgs e)
{
Console.WriteLine("here i am!");
}
}
}
日志没有显示。我想知道为什么事件没有触发或者没有到达正确的位置 - 问题是因为我将上下文菜单包装在这个资源字典中吗?更新:有趣的是,如果我删除代码后面的函数,编译时会出现错误:
“ContextMenu_OnContextMenuOpening”不包含对“MyApp.Components.MyContextMenu”类型的第一个参数的定义,也找不到接受该类型作为第一个参数的扩展方法“ContextMenu_OnContextMenuOpening”(是否缺少使用指令或程序集引用?)
更新2:看起来
Console.WriteLine
和 Debug.WriteLine
都能产生输出,但只有在我点击项目底部附近时才会“随机”地产生输出。可能是某种碰撞检测出了问题?
Console.WriteLine
的输出结果吗? 将其更改为System.Diagnostics.Debug.WriteLine
,然后在输出窗口дёжЈЂжџҐгЂ‚ - nemesv