在 App.xaml 的 EventSetter 上获取错误 CS1061 [英] Getting Error CS1061 on EventSetter of App.xaml

查看:36
本文介绍了在 App.xaml 的 EventSetter 上获取错误 CS1061的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过我的代码创建一个元素并为其关联一个样式,同时关联它的 EventSetter,该样式运行良好,但是当我尝试运行该函数时它不起作用.

I'm trying to create an element by my code and associate a style for it, also associating its EventSetter, the style works perfectly but when I try to run the function it does not work.

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 中

when the application is being built, two errors are fired into the EventSetter

错误 CS1061应用程序"不包含设置lbl_MouseLeftButtonUp"并且找不到任何lbl_MouseLeftButtonUp"接受App"类型的第一个参数的扩展方法(是否存在缺少使用指令或程序集参考?)

Error CS1061 "App" does not contain a setting for "lbl_MouseLeftButtonUp" and could not find any "lbl_MouseLeftButtonUp" extension method that accepts a first argument of type "App" (is there a usage directive or assembly reference missing?)

同样的错误也会发生在正确的事件上,我该如何在我的类中实现这两个方法而不会出现问题?

The same error also happens for the right event, how can I do to implement these two methods in my class where I am using this without giving problems?

推荐答案

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

Generally, you get Error CS1061 when a method is inaccessible from XAML.

最常见的情况是:

  • 未在代码隐藏中声明事件处理程序
  • XAML 的 x:Class 标记与类的实际名称不匹配
  • 与事件设置器的Handler不匹配的方法名称
  • 不正确的处理程序方法签名
  • base 类中使用private 方法而不是protected
  • 在极少数情况下需要重新启动 Visual Studio
  • event handler is not declared in code-behind
  • XAML's x:Class tag not matching the actual name of the class
  • name of the method not matching the Handler of event setter
  • incorrect handler method signature
  • using a private method in the base class instead of protected
  • a need for restarting the visual studio in rare cases

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

Looking at the XAML code, your class name is Learning.App

<Application x:Class="Learning.App"

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

But the code behind in which the event handlers are declared is ViewConfigAgendaDin

public class ViewConfigAgendaDin

您不能将事件处理程序放在任何地方并期望编译器自己找到它们.因为处理程序是在 App.XAML 中使用的,所以您需要将事件处理程序移动到 App.xaml.cs去吧.

You can't put the event handlers anywhere and expect the compiler to find them by itself. Because the handler is used in App.XAML, you need to Move the event handlers to App.xaml.cs and it will be good to go.

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

If you need them to be in ViewConfigAgendaDin class, either define the Style in ViewConfigAgendaDin.xaml or call a method in ViewConfigAgendaDin.xaml.cs from App.xaml.cs

例如:

ViewConfigAgendaDin.xaml:

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:

ViewConfigAgendaDin.xaml.cs:

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

App.xaml.cs:

App.xaml.cs:

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

<小时>

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


Another way to handle this situation is to avoid code-behind altogether. Instead, make use of MVVM and Command Binding. You can easily bind any event to a command using Interactions

这篇关于在 App.xaml 的 EventSetter 上获取错误 CS1061的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆