1

I making an upload app for Windows Phone, the function is in the class B (SecondPage), I have a upload task, when this task is completed, it call a event and notice class A (MainPage) that the task is done and do some things, looking around the site, I found some solutions but they don't help to much (just I think that).

  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

Here are my code in 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);
    }
}

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

The TextBox named txt is not cleared, can you guys please help me, thank you!

Community
  • 1
  • 1
iamatsundere181
  • 1,401
  • 1
  • 16
  • 38

1 Answers1

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


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

where you want to notify thn

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

and its event method like:-

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

add something in this collection after your operation completed ..thn it will notify.