Getting Error CS1061 on EventSetter of App.xaml

半城伤御伤魂 提交于 2019-12-02 01:14:19

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

Most common cases are:

  • 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 arguments
  • using a private method in the base class instead of protected
  • a need for restarting the visual studio in rare cases

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

<Application x:Class="Learning.App"

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

public class ViewConfigAgendaDin

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.

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

Edit:

For example:

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);
}

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!