我的 DLL 中的虚拟 MouseButtonEventHandler 被调用应用程序覆盖
Virtual MouseButtonEventHandler in my DLL overridden from calling application
我有我的 class 库,它在网格中动态创建标签
public class Class1
{
public virtual event System.Windows.Input.MouseButtonEventHandler label_event;
public Class1(Grid _grid)
{
Label l = new Label();
l.Content = "label";
l.Cursor = System.Windows.Input.Cursors.Hand;
Grid.SetColumn(_grid, 0);
Grid.SetRow(_grid, 0);
_grid.Children.Add(l);
l.MouseLeftButtonUp += label_event;
}
}
从另一个应用程序创建此 class
Class1 class1 = new Class1(grid);
class1.label_event += l_MouseLeftButtonUp;
...
void l_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
Title = sender.ToString();
}
...
编译没有错误,但是执行后出现错误 l.MouseLeftButtonUp += label_event; "Cannot be null"
如何覆盖鼠标事件?
您应该在 Class1
中引发一个事件,然后在消费 class:
中处理它
public class Class1
{
public event System.Windows.Input.MouseButtonEventHandler label_event;
public Class1(Grid _grid)
{
Label l = new Label();
l.Content = "label";
l.Cursor = System.Windows.Input.Cursors.Hand;
Grid.SetColumn(_grid, 0);
Grid.SetRow(_grid, 0);
_grid.Children.Add(l);
l.MouseLeftButtonUp += (s,e) => label_event?.Invoke(this, e);
}
}
我有我的 class 库,它在网格中动态创建标签
public class Class1
{
public virtual event System.Windows.Input.MouseButtonEventHandler label_event;
public Class1(Grid _grid)
{
Label l = new Label();
l.Content = "label";
l.Cursor = System.Windows.Input.Cursors.Hand;
Grid.SetColumn(_grid, 0);
Grid.SetRow(_grid, 0);
_grid.Children.Add(l);
l.MouseLeftButtonUp += label_event;
}
}
从另一个应用程序创建此 class
Class1 class1 = new Class1(grid);
class1.label_event += l_MouseLeftButtonUp;
...
void l_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
Title = sender.ToString();
}
...
编译没有错误,但是执行后出现错误 l.MouseLeftButtonUp += label_event; "Cannot be null"
如何覆盖鼠标事件?
您应该在 Class1
中引发一个事件,然后在消费 class:
public class Class1
{
public event System.Windows.Input.MouseButtonEventHandler label_event;
public Class1(Grid _grid)
{
Label l = new Label();
l.Content = "label";
l.Cursor = System.Windows.Input.Cursors.Hand;
Grid.SetColumn(_grid, 0);
Grid.SetRow(_grid, 0);
_grid.Children.Add(l);
l.MouseLeftButtonUp += (s,e) => label_event?.Invoke(this, e);
}
}