【问题标题】:How to call an event from other page Windows Phone?如何从 Windows Phone 的其他页面调用事件?
【发布时间】:2016-06-28 10:23:04
【问题描述】:

我为Windows Phone制作了一个上传应用程序,该功能在B类(SecondPage)中,我有一个上传任务,当这个任务完成时,它会调用一个事件并通知A类(MainPage)该任务是完成并做一些事情,环顾网站,我找到了一些解决方案,但它们没有多大帮助(只是我认为)。

  1. Notify when event from another class is triggered

  2. Raise an event of a class from a different class in C#

  3. Understanding events and event handlers in C#

  4. C# event handling (compared to Java)

  5. C#: Need one of my classes to trigger an event in another class to update a text box

这是我在 SecondPage 中的代码

public sealed partial class SecondPage : Page
{
    public event EventHandler clearHandler = delegate { };
    public SecondPage()
    {
        this.InitializeComponent();
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
    }

    private void btnClear_Tapped(object sender, TappedRoutedEventArgs e)
    {
        //some works
        if (clearHandler != null)
            clearHandler(this, null);
    }
}

在主类中

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
        this.NavigationCacheMode = NavigationCacheMode.Required;
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        SecondPage sp = new SecondPage();
        sp.clearHandler += Sp_clearHandler;
    }

    private void Sp_clearHandler(object sender, EventArgs e)
    {
        txt.Text = "";
    }

    private void btnJump_Tapped(object sender, TappedRoutedEventArgs e)
    {
        Frame.Navigate(typeof(SecondPage));
    }
}

名为txt的TextBox没有清空,各位大神帮忙看看,谢谢!

【问题讨论】:

    标签: c# delegates windows-phone eventhandler


    【解决方案1】:
    you can use ObservableCollection for notify one page to another.
    
    
    public static ObservableCollection<string> YesNoStatus { get; internal set; }
    

    你想通知的地方

    YesNoStatus = new ObservableCollection<string>();
            YesNoStatus.CollectionChanged += YesNoStatus_CollectionChanged;
    

    及其事件方法如:-

    private void YesNoStatus_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            ObservableCollection<string> obsSender = sender as ObservableCollection<string>;
        }
    

    在你的操作完成后在这个集合中添加一些东西......然后它会通知。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多