我想处理一个点击按钮事件从另一个页面

本文关键字:事件 按钮 另一个 一个 处理 | 更新日期: 2023-09-27 18:15:33

我有两个页面MainPage和PlayPage。在MainPage里面我有一个框架和一个文本块,在框架里面我有Playpage。当我点击一个按钮从播放页面,我改变了一个变量,但文本块不更新。我怎么做呢?下面是我的代码:

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

我想处理一个点击按钮事件从另一个页面

处理和引发事件应该成为你的新朋友!

赃物:

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

请记住,隐私应该得到尊重!


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

请注意,我已经添加了减法按钮。


主窗口:

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也是一个TextBlock(表示用户是否降低或提高了他的技能等级)。


SwagEventArgs:

public class SwagEventArgs : EventArgs
{
    public SwagEventArgs(int value)
    {
        Value = value;
    }
    public readonly int Value;
}

SwagEventArgs将作为事件数据存储信息