【问题标题】:I want to handle a clickbutton event from another page我想处理来自另一个页面的 clickbutton 事件
【发布时间】:2016-10-30 04:54:13
【问题描述】:

我有两个页面 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;
        }

    }

【问题讨论】:

  • 因为您现在根本没有更新文本块,所以每次swag 值发生变化时,您都必须更新文本块。

标签: c# wpf


【解决方案1】:

Handling and Raising Events 应该是你的新朋友!

赃物:

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

请记住,privacy should be respected!


播放页面:

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

请注意,我添加了一个减法Button


主窗口:

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 将用于将信息存储为event data

【讨论】:

  • 它告诉我找不到类型或命名空间 SwagEventArgs。为了解决这个问题(或学习),我需要做什么?我忘了提 swag 类在 MainPage 中。
  • @RobertG。是的——我的错。该类现在已在答案中实现!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多