Silverlight:我可以在XAML中将事件设置为基类事件处理程序吗? [英] Silverlight: Can I set an event to a base class event handler in XAML?

查看:54
本文介绍了Silverlight:我可以在XAML中将事件设置为基类事件处理程序吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个自定义用户控件,以用作某些维护功能的基类. 我希望能够将一些事件连接到基类中定义的处理程序. 我可以在后面的代码中手动执行此操作,但想在XAML中分配它们.这不可能吗?

I have created a custom user control to use as a base class for some maintenance functions. I would like to be able to wire up some events to handlers defined in the base class. I can do this manually in the code behind but would like to assign them in XAML. Is this not possible?

<src:CustomerMaintenanceControlBase x:Class="ProjectManager.CustomerMaintenanceControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit"
xmlns:my="clr-namespace:ProjectManager.CustomerServiceReference"
xmlns:src="clr-namespace:ProjectManager"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400"
PrimaryViewSourceName="customerViewSource"
                                Loaded="CustomerMaintenanceControlBase_Loaded"
>

Loaded ="CustomerMaintenanceControlBase_Loaded"会编译,但在运行时会失败. 我可以在后面的代码中将其连接起来,但这并不是那么有趣. :)

Loaded="CustomerMaintenanceControlBase_Loaded" compiles but fails at runtime. I can wire this up in the code behind but that isn't as much fun. :)

推荐答案

Codebehind可以,但是解决方案非常简单. 例如,如果您在基类中的事件方法的名称为RegisterOnPageLoad(), 然后在您的子班级中,xaml代码如下所示:

Codebehind is ok, but the solution is rather simple. If, for example, your event method in the base class has the name RegisterOnPageLoad(), then in your child class the xaml code looks like this

Loaded="RegisterOnPageLoad".

不要忘记为该方法提供正确的访问权限.

Don't forget to give the method the right access.

这篇关于Silverlight:我可以在XAML中将事件设置为基类事件处理程序吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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