我最近开始在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>
我希望能够使用控件中的每个按钮,就像在标准控件中一样。例如,当有人使用上面的控件时,他应该能够用自己的方法处理click事件,如下所示:
<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));
}
这很好但很慢。点击和结果操作之间的延迟可能会达到一秒钟。有人可以告诉我是否有更快或更好的方法来做到这一点?