gpt4 book ai didi

c# - 如何通过代码添加事件 setter

转载 作者:行者123 更新时间:2023-11-30 14:15:09 24 4
gpt4 key购买 nike

一个完整的例子

    public delegate void mouseup_delegate(object obj, MouseButtonEventArgs args);

constructor()
{
TextBlock text_block = new TextBlock() { Text = "aa" };
Style style = new Style();
//style.Setters.Add(new EventSetter(){Event=TextBlock.MouseUpEvent, Handler=new mouseup_delegate(this.textblockClicked)});
style.Setters.Add(new EventSetter(TextBlock.MouseUpEvent, new mouseup_delegate(this.textblockClicked)));
text_block.Style = style;
}

public void textblockClicked(object sender, MouseButtonEventArgs args)
{
MessageBox.Show("mouse up");
}

但是当我运行应用程序时,出现异常:处理程序类型无效

这段代码有什么问题?

最佳答案

EventSetter 期望您提供的委托(delegate)是 MouseButtonEventHandler,而不是 mouseup_delegate

关于c# - 如何通过代码添加事件 setter ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10509140/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com