自定义标记扩展返回 RoutedEvents

发布于 2024-09-07 19:32:02 字数 1369 浏览 6 评论 0原文

我正在尝试为“关闭”按钮创建一个通用事件,它们必须关闭窗口,但在此之前将焦点设置到所有者窗口。我不想为每个文件都创建一个事件,因为这非常不切实际,因为我的应用程序中有 30 多个窗口。 (所以如果我想改变这种行为,我每次都必须改变 30 个文件)

我不确定这是否是正确的方法,但我尝试制作一个标记扩展,它返回一个 delegate(object sender, RoutedEventArgs e) 下面是代码:

delegate void RoutedDelegate(object sender, RoutedEventArgs e);
[MarkupExtensionReturnType(typeof(RoutedEvent))]
public class CloseWindowExtension : MarkupExtension
{
    Window win = null;

    public Window Win
    {
        get { return this.win; }
        set { this.win = value; }
    }

    public CloseWindowExtension(Window win)
        : base()
    {
        this.win = win;
    }



    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        if (win == null)
        {
            throw new InvalidOperationException("The window must be specified!");
        }

        return new RoutedDelegate(delegate(object sender, RoutedEventArgs e)
        {
            Extensions.FocusClose(win);
        });
    }
}

FocusClose 方法获取一个窗口,关闭它,但之前将焦点设置给其所有者。但我无法让它发挥作用。当我在 xaml 中设置按钮

Button Click="{e:CloseWindow {Binding win}}"

(win 是我的窗口名称)时,收到错误消息:

Click="{e:CloseWindow {Binding win}}" 无效。 “{e:CloseWindow {Binding win}}”不是有效的事件处理程序方法名称。只有生成的类或代码隐藏类上的实例方法才有效。第 28 行位置 17.

我做错了什么吗?这是最好的方法还是我还有其他选择? 提前致谢!

克拉克

I'm trying to create a generalized event for my Close buttons, where they have to close the window but before that set focus to the owner window. I don't want to have an event for every file for that, because that'd be pretty unpractical since I have 30+ windows in my application. (So if I wanted to change that behavior, i'd have to change on 30 files everytime)

I'm not sure if that's the correct approach, but I tried making a MarkUp Extension which returns a delegate(object sender, RoutedEventArgs e) Here is the code:

delegate void RoutedDelegate(object sender, RoutedEventArgs e);
[MarkupExtensionReturnType(typeof(RoutedEvent))]
public class CloseWindowExtension : MarkupExtension
{
    Window win = null;

    public Window Win
    {
        get { return this.win; }
        set { this.win = value; }
    }

    public CloseWindowExtension(Window win)
        : base()
    {
        this.win = win;
    }



    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        if (win == null)
        {
            throw new InvalidOperationException("The window must be specified!");
        }

        return new RoutedDelegate(delegate(object sender, RoutedEventArgs e)
        {
            Extensions.FocusClose(win);
        });
    }
}

The FocusClose method gets a window, closes it, but sets focus to its owner before. But I can't make it work. When i set my button in the xaml,

Button Click="{e:CloseWindow {Binding win}}"

(win is my Window name), I get the error message:

Click="{e:CloseWindow {Binding win}}" is not valid. '{e:CloseWindow {Binding win}}' is not a valid event handler method name. Only instance methods on the generated or code-behind class are valid. Line 28 Position 17.

Am I doing something wrong? Is this the best approach or do I have another options?
Thanks in advance!

Clark

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蒗幽 2024-09-14 19:32:02

您不能使用标记扩展来设置事件处理程序。相反,您可以使用附加行为,它允许您将命令绑定到事件。

有关详细信息,请参阅 Marlon Grech 撰写的这篇文章

You can't use a markup extension to set an event handler. Instead, you can use an attached behavior, which allows you to bind a command to an event.

See this article by Marlon Grech for details

锦上情书 2024-09-14 19:32:02

.NET 4.5+ 支持事件的标记扩展,因此您现在可以实现您想要的:)

.NET 4.5+ supports markup extensions for events, so you can implement what you wanted now :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文