Robert G. Robert G. - 1 month ago 9
C# Question

I want to handle a clickbutton event from another page

I have two pages MainPage and PlayPage.Inside MainPage I have a frame and a textblock and inside the frame I have Playpage. When I click a button from play page I change a variable but the textblock doesn't update. How do I do that?
Here is my code:

public class Swag
{
public static int swag = 0;

public void Add(int a)
{
swag += a;
}

public void Reduce(int a)
{
swag -= a;
}

public int Get()
{
return swag;
}

}

public sealed partial class MainPage : Page
{

public MainPage()
{
this.InitializeComponent();
MyFrame.Navigate(typeof(PlayPage));
SwagMeasurer.Text = Convert.ToString(Swag.Get());
}
}


public sealed partial class PlayPage : Page
{
public PlayPage()
{
this.InitializeComponent();
}

private void Clicker_Click(object sender, RoutedEventArgs e)
{
Swag.swag += 1;
}

}

Answer

Handling and Raising Events should be your new friend!

Swag :

public class Swag
{
    private static int _swag;

    public static void Add(int a)
    {
        _swag += a;

        OnUpdate?.Invoke(new SwagEventArgs(a));
        OnAddition?.Invoke(new SwagEventArgs(a));
    }

    public static void Reduce(int a)
    {
        _swag -= a;

        OnUpdate?.Invoke(new SwagEventArgs(a));
        OnSubtraction?.Invoke(new SwagEventArgs(a));
    }

    public static int Get()
    {
        return _swag;
    }

    public static event AddedValueEventHandler OnAddition;
    public static event SubtractedValueEventHandler OnSubtraction;
    public static event UpdatedValueEventHandler OnUpdate;

    public delegate void AddedValueEventHandler(SwagEventArgs e);
    public delegate void SubtractedValueEventHandler(SwagEventArgs e);
    public delegate void UpdatedValueEventHandler(SwagEventArgs e);
}

Keep in mind, that privacy should be respected!


PlayPage :

public partial class PlayPage : Page
{
    public PlayPage()
    {
        InitializeComponent();
    }

    private void Clicker_Sub_Click(object sender, RoutedEventArgs e)
    {
        Swag.Reduce(1);
    }

    private void Clicker_Add_Click(object sender, RoutedEventArgs e)
    {
        Swag.Add(1);
    }
}

Please note, that I have added a subtraction Button.


MainWindow :

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        MyFrame.Navigate(new PlayPage());
        SwagMeasurer.Text = Convert.ToString(Swag.Get());

        Swag.OnAddition += Swag_Addition;
        Swag.OnSubtraction += Swag_OnSubtraction;
        Swag.OnUpdate += Swag_OnUpdate;
    }

    private void Swag_OnUpdate(SwagEventArgs e)
    {
        SwagMeasurer.Text = Convert.ToString(Swag.Get());
    }

    private void Swag_OnSubtraction(SwagEventArgs e)
    {
        LastMode.Text = "That's a negative";
    }

    private void Swag_Addition(SwagEventArgs e)
    {
        LastMode.Text = "That's a positive";
    }
}

LastMode is also a TextBlock (reprecenting if the user has dropped or raised his skill level).

Comments