3

I have a button with a click method assigned to it. I'd like to move that method to another CS file so I may reuse it in other places. Is it possible in XAML to reference a method, click method, from a CS source file outside of the Pages source file?

<Button x:Name="TheRedButton" Click="DoNotPressTheRedButton_Click" />

I want to locate DoNotPressTheRedButton_Click in another source file.

ButtonMethods.cs

and then use it in my Page from XAML

MyPage.xaml

What do I need to write in XAML in order to do this?

I thought I need:

xmlns:helpermethods="using:CoreProject.HelperMethods"

then,

<Button x:Name="TheRedButton" Click="helpermethods:ButtonMethods.DoNotPressTheRedButton_Click" />

But I'm getting compiler errors... CS1002 and CS1513. So obviously it's not the right syntax. Is this even possible?

visc
  • 4,794
  • 6
  • 32
  • 58
  • @EdPlunkett The button methods are static so they shouldn't need and instance? The method essential does some state changes to the button itself based on the object bound to the button, `MyButton.DataContext as MyObject`. So basically these methods look at `MyObject` and set the right state on the button. It's adventitious to just have this click method in a core part of my code. I build many packages that reference core. – visc Dec 04 '17 at 21:15
  • My bad, I misread your question. But see answer -- the problem was that `x:Static` only works with properties. So write a property. – 15ee8f99-57ff-4f92-890c-b56153 Dec 04 '17 at 21:17

2 Answers2

5

Easy peasy:

<Button Click="{x:Bind helpermethods:ButtonMethods.ButtonClick}" />

Or in WPF:

<Button Click="{x:Static helpermethods:ButtonMethods.ButtonClick}" />

C#:

public static class ButtonMethods
{
    public static RoutedEventHandler ButtonClick => Button_Click;

    public static void Button_Click(object sender, RoutedEventArgs e)
    {
        // stuff
    }
}
1

You could call your method in ButtonMethods from the click handler of your TheRedButton.

Like this:

private void OnSomebodyClickedTheRedButtonWhenTheyWereNotMeantTo(object sender, RoutedEventArgs args)
{
    // do some other stuff with sender - find out which button was clicked, etc.

    ButtonMethods.RedButtonClicked(sender, args);
}

This would allow you to also do "other stuff" that you might need, within this class, from this event handler (if you wanted to).

Make sure your method is static, otherwise you would need to (unnecessarily) create an instance of the ButtonMethods class each time.

Geoff James
  • 3,122
  • 1
  • 17
  • 36
  • Yeah I considered this approach. For me I chose having less code and less flexibility. But if I needed more control I certainly could go this route. Like passing the event args to the my static click handler. – visc Dec 04 '17 at 22:09
  • 1
    Yeah, it's definitely useful if you wanted to find out which button was clicked, or to update some other properties on that View. I agree with you about the less code - don't write any more than you already need to! Glad you got it sorted :) – Geoff James Dec 04 '17 at 22:12
  • Also, in my case I was creating these buttons dynamically. So I didn't have a way to write a button click method because I didn't have a handle to it. :) – visc Dec 04 '17 at 22:22