C# 用户控件自定义事件崩溃

C# 用户控件自定义事件崩溃,c#,wpf,xaml,user-controls,C#,Wpf,Xaml,User Controls,我使用ClickEvent制作了一个简单的自定义控件: ImageButton.xaml: <UserControl x:Name="ImgButton" x:Class="WpfApplication1.ImageButton" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/200

我使用ClickEvent制作了一个简单的自定义控件:

ImageButton.xaml:

<UserControl x:Name="ImgButton" x:Class="WpfApplication1.ImageButton"
         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" 
         d:DesignHeight="300" d:DesignWidth="300">
    <Grid></Grid>
</UserControl>

如果我在处理click事件,当我单击控件时,它会正常工作,否则我会崩溃。那个么,我该怎么办呢?

在引发事件之前,必须检查是否已将处理程序添加到事件中:

void ImageButton_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
    if ((mouse_down) && (mouse_in) && Click != null)
    {
        Click(this, null);
    }
    mouse_down = false;
}
void ImageButton_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
    if ((mouse_down) && (mouse_in) && Click != null)
    {
        Click(this, null);
    }
    mouse_down = false;
}