2016-06-28 98 views
1

我爲Windows Phone製作了一個上傳應用程序,該功能位於B類(SecondPage)中,我有一個上傳任務,當這個任務完成時,它調用一個事件並通知A類(MainPage)完成任務並做一些事情,環顧網站,我發現了一些解決方案,但他們沒有太大的幫助(只是我認爲)。如何從其他頁面調用事件Windows Phone?

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

代碼在MainClass

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不會被清除,可你們,請幫幫我,謝謝!

回答

0
you can use ObservableCollection for notify one page to another. 


public static ObservableCollection<string> YesNoStatus { get; internal set; } 

要通知THN

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

及其活動方法,如: -

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

此集合中添加的東西后,你的操作完成..thn它會通知。

相關問題