【发布时间】:2012-06-07 00:27:30
【问题描述】:
我最近开始在 DLL 中移动我的用户控件。这些控件通常看起来像这样:
<UserControl x:Class="DialogBase.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Grid>
<Button Content="OK" Click="OkButton_Click"/>
</Grid>
</UserControl>
我希望能够像在标准控件中一样使用控件中的每个按钮。例如,当有人使用上面的控件时,他应该能够在自己的方法中处理单击事件,如下所示:
<lib:UserControl1
OkButtonClick="MyCostomClickMethod"
/>
我通常是这样实现的:(用户控件的代码隐藏文件)
public static RoutedEvent OkButtonClickEvent = EventManager.RegisterRoutedEvent("OkButtonClick", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(UserControl1));
public event RoutedEventHandler OkButtonClick
{
add
{
AddHandler(OkButtonClickEvent, value);
}
remove
{
RemoveHandler(OkButtonClickEvent, value);
}
}
private void OkButton_Click(object sender, RoutedEventArgs e)
{
RaiseEvent(new RoutedEventArgs(OkButtonClickEvent));
}
这很好用,但速度很慢。单击和产生的操作之间的延迟可能长达一秒。谁能告诉我是否有更快或更好的方法来做到这一点?
【问题讨论】:
-
您是否尝试在设计时仅添加事件处理程序?
-
您是否在处理程序中执行任何 CPU 密集型任务?
-
不,它们几乎是空的,我只是向它们添加了一个消息框或控制台输出。