【问题标题】:Getting Error CS1061 on EventSetter of App.xaml在 App.xaml 的 EventSetter 上出现错误 CS1061
【发布时间】: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


    【解决方案1】:

    通常,当无法从 XAML 访问方法时,您会收到 错误 CS1061

    最常见的情况是:

    • 事件处理程序未在代码隐藏中声明
    • XAML 的x:Class 标记与类的实际名称不匹配
    • 方法的名称与事件设置器的Handler 不匹配
    • 处理程序方法签名不正确
    • base 类中使用private 方法而不是protected
    • 在极少数情况下需要重新启动 Visual Studio

    查看XAML代码,你的类名是Learning.App

    <Application x:Class="Learning.App"
    

    但是声明事件处理程序的代码是ViewConfigAgendaDin

    public class ViewConfigAgendaDin
    

    您不能将事件处理程序放在任何地方并期望编译器自行找到它们。因为处理程序在App.XAML 中使用,您需要将事件处理程序移动到App.xaml.cs,这样就可以了。

    如果您需要它们在 ViewConfigAgendaDin 类中,请在 ViewConfigAgendaDin.xaml 中定义 Style 或从 App.xaml.cs 调用 ViewConfigAgendaDin.xaml.cs 中的方法

    编辑:

    例如:

    ViewConfigAgendaDin.xaml:

    <ViewConfigAgendaDin 
        xmlns:v="clr-namespace:MY_NAMESPACE">
    ...
        <Label Tag="{Binding RelativeSource={RelativeSource AncestorType={x:Type v:ViewConfigAgendaDin}}}" 
               Style="{StaticResource LabelTituloEstiloPadrao}"/>
    ...
    </ViewConfigAgendaDin>
    

    ViewConfigAgendaDin.xaml.cs:

    public void MyMethodForRightClick(object sender, MouseButtonEventArgs e)
    {
        MessageBox.Show("Right");
    }
    

    App.xaml.cs:

    private void lbl_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
    {
        ((sender as Label).Tag as ViewConfigAgendaDin).MyMethodForRightClick(sender, e);
    }
    

    处理这种情况的另一种方法是完全避免代码隐藏。相反,使用 MVVMCommand 绑定。您可以使用 Interactions

    轻松地将任何事件绑定到命令

    【讨论】:

    • 但是例如我在我的 app.xaml 中声明了我的 EventSetter 我不能在类中创建将使用这种样式的方法?在有问题的 lbl_MouseLeftButtonUp 方法中,我必须使用 ViewConfigAgendaDin 类中的数据表,因为我可以在该类中使用它
    • 不,您不能在ViewConfigAgendaDin 中创建方法。它必须与 xaml 属于同一类。但是您可以从App.xaml.cs 调用任何您想要的ViewConfigAgendaDin 方法,请参阅我的编辑。
    • 现在你可以理解了,这个方法就像一个接口,我也可以在我的 ViewConfigAgendaDin.xaml 中使用它? private void lbl_MouseRightButtonUp (object sender, MouseButtonEventArgs e) {      // x var global     Label l = (sender) Label; x = l.Name; MessageBox.Show (x); }
    • 我不明白你的最后评论。你可以从任何地方调用你想要的任何方法。但事件处理程序lbl_MouseRightButtonUp必须在 app.xaml.cs 中声明
    • 我认为您不能将Style 移动到ViewConfigAgendaDin.xaml。所以你需要将事件处理程序移动到 app.xaml.cs
    猜你喜欢
    • 1970-01-01
    • 2011-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多