How do I call a call method from XAML in WPF?

前端 未结 3 793

How do I call a call method from XAML in WPF?

3条回答
  •  醉酒成梦
    2020-12-19 10:45

    Except the commands there is another way that allows you to call a method directly from XAML. It is not commonly used but the option is still there.

    The method must have one of the two types of signatures

    • void Foo()
    • void Bar(object sender, EventArgs e)

    To make it working, you have to include references and namespaces of the Microsoft.Expression.Interactions and System.Windows.Interactivity into your project. Easiest way is to install a nuget. In the example below the namespaces are defined as xmlns:i and xmlns:ei.

    
    
        
    
    

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;
        }
    
        public void VoidParameterlessMethod() 
            => Console.WriteLine("VoidParameterlessMethod called");
    
        public void EventHandlerLikeMethod(object o, EventArgs e) 
            => Console.WriteLine("EventHandlerLikeMethod called");
    
    }
    

提交回复
热议问题