首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在EventSetter of App.xaml上获取误差App.xaml

在EventSetter of App.xaml上获取误差App.xaml
EN

Stack Overflow用户
提问于 2018-08-02 17:24:33
回答 1查看 517关注 0票数 2

我试图通过代码创建一个元素,并为它关联一个样式,同时也将它的EventSetter关联在一起,样式工作得很完美,但是当我试图运行这个函数时,它不起作用。

App.xaml

代码语言:javascript
复制
<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

代码语言:javascript
复制
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 "App“不包含"lbl_MouseLeftButtonUp”的设置,也找不到任何接受"App“类型的第一个参数的"lbl_MouseLeftButtonUp”扩展方法(是否缺少使用指令或程序集引用?)

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

EN

回答 1

Stack Overflow用户

发布于 2018-08-02 19:34:46

通常,当无法从XAML访问方法时,就会得到Error CS1061

最常见的情况是:

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

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

代码语言:javascript
复制
<Application x:Class="Learning.App"

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

代码语言:javascript
复制
public class ViewConfigAgendaDin

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

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

编辑:

例如:

ViewConfigAgendaDin.xaml:

代码语言:javascript
复制
<ViewConfigAgendaDin 
    xmlns:v="clr-namespace:MY_NAMESPACE">
...
    <Label Tag="{Binding RelativeSource={RelativeSource AncestorType={x:Type v:ViewConfigAgendaDin}}}" 
           Style="{StaticResource LabelTituloEstiloPadrao}"/>
...
</ViewConfigAgendaDin>

ViewConfigAgendaDin.xaml.cs:

代码语言:javascript
复制
public void MyMethodForRightClick(object sender, MouseButtonEventArgs e)
{
    MessageBox.Show("Right");
}

App.xaml.cs:

代码语言:javascript
复制
private void lbl_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
{
    ((sender as Label).Tag as ViewConfigAgendaDin).MyMethodForRightClick(sender, e);
}

处理这种情况的另一种方法是完全避免代码隐藏。相反,使用MVVM和命令绑定。可以使用Interactions轻松地将任何事件绑定到命令

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51659165

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档