【发布时间】:2018-08-02 17:24:33
【问题描述】:
我正在尝试通过我的代码创建一个元素并为其关联一个样式,同时关联它的 EventSetter,该样式可以完美运行,但是当我尝试运行该函数时它不起作用。
App.xaml
<Application x:Class="Learning.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Learning">
<Application.Resources>
<Style TargetType="Label" x:Key="LabelTituloEstiloPadrao">
<Setter Property="Background" Value="White" />
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="Margin" Value="40,20,0,0" />
<EventSetter Event="MouseLeftButtonUp" Handler="lbl_MouseLeftButtonUp"/>
<EventSetter Event="MouseRightButtonUp" Handler="lbl_MouseRightButtonUp"/>
</Style>
</ResourceDictionary>
</Application.Resources>
</Application>
MainWindow.xaml.cs
public ViewConfigAgendaDin()
{
InitializeComponent();
ConfigInicial();
Label l = new Label();
lblTeste.Style = (Style)App.Current.Resources["LabelTituloEstiloPadrao"];
StackHorarios.Children.Add(l);
}
private void lbl_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
{
MessageBox.Show("Right");
}
public void lbl_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
MessageBox.Show("Left");
}
在构建应用程序时,EventSetter 会触发两个错误
错误 CS1061“应用程序”不包含以下设置 “lbl_MouseLeftButtonUp”,找不到任何“lbl_MouseLeftButtonUp” 接受“App”类型的第一个参数的扩展方法(是否存在 缺少使用指令或程序集引用?)
同样的错误也会发生在正确的事件上,我怎样才能在我使用它的类中实现这两种方法而不会出现问题?
【问题讨论】:
标签: c# wpf eventhandler