简体   繁体   中英

Passing Continuous data from one XAML page to another Windows 10

I am trying to take data that i am reading from a serial device on one xaml page and pass it to another. I can get the data to go to the other page but not continuously. Meaning for example, if i were to use a textchanged event, the data comes through from the first page but only once. This could be because the data does not change all the time? Sometimes the save value is read multiple times before changing. However i have also simulated with the data constantly changing and still the event only fires once. I am currently storing a string value in the app.xaml.cs from the main page then reading it from the second page. I have also tried with a static class and they both have same results. Im sure its a simple fix but i cant figure it out. Any help appreciated.

On the main page i have this.

  App app = Application.Current as App;
  app.readData = rotAngle.ToString("F2");

and this on the next page

  private void rawValText_DataContextChanged(FrameworkElement sender, DataContextChangedEventArgs args)
    {            
        App app = Application.Current as App;
        rawValText.Text = app.readData;
    }

but it only fires once when the page loads.

Use events. Use static class for this.

Fire event when data is recieved in Page1.

Write event handler in Page2 to receive event fired from Page1.

Simple !

You seem to be using DataContextChangedEvent incorrectly. DataContextChangedEvent fires when the DataContext changes. You should use the appropriate Event. You could either look up MVVM and use INotifyPropertyChanged, or you can keep your way and subscribe to an event on the other page, which you notify from the other:

How to subscribe to other class' events in c#?

Essentially you have to code the notification yourself.

EDIT: Sample MVVM Tutorial:

http://www.codeproject.com/Articles/126249/MVVM-Pattern-in-WPF-A-Simple-Tutorial-for-Absolute

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM