简体   繁体   中英

Passing variable between XAML pages

I have two page : Start.xaml.cs and Settings.xaml.cs. In Setting.xaml.cs i get the data form accelerometer just one time and fix a initial posiotion. In Start.xaml.cs i read the data from accelerometer continously. I want to get the accelerometer data variables from Settings.xaml.cs and bring them to Start.xaml.cs to compare them. Could you help me to make it or give me some source of information of how could i do this?

Code for Start.xaml.cs

Accelerometer acc = new Accelerometer();

public Start()
{

    InitializeComponent();
    acc.ReadingChanged += new EventHandler<AccelerometerReadingEventArgs>(acc_ReadingChanged);
    acc.Start();
}

void acc_ReadingChanged(object sender, AccelerometerReadingEventArgs e)
{
    Deployment.Current.Dispatcher.BeginInvoke(() => ThreadSafeAccelerometerChanged(e));
}

void ThreadSafeAccelerometerChanged(AccelerometerReadingEventArgs e)
{
    xText.Text = e.X.ToString("0.0000");
    yText.Text = e.Y.ToString("0.0000");
    zText.Text = e.Z.ToString("0.0000");
}

And here is the code for Settings.xaml.cs

Accelerometer acc = new Accelerometer();

private void Calib_Click(object sender, RoutedEventArgs e)
{
    acc.Start();
    acc.ReadingChanged += myAccelerometer_ReadingChanged;
}

void myAccelerometer_ReadingChanged(object sender, AccelerometerReadingEventArgs f)
{
    this.Dispatcher.BeginInvoke(delegate()
    {
        xBlock.Text = "X:"+f.X.ToString("0.0000");
        yBlock.Text = "Y:"+f.Y.ToString("0.0000");
        zBlock.Text = "Z:"+f.Z.ToString("0.0000");
        acc.Stop();
    });
}

The settings page should set members of a "global" class that both the main and settings page can access. Information on how to do that can be found in this question .

Ideally this class would just hold accelerometer type information.

You can also send your accelerometer data to different page using NavigationService. This can be used to send simple data between pages.

// In your settings page use the following code
NavigationService.Navigate(new Uri(string.format("/Start.xaml?xValue={0}&yValue={1}&zValue={2}", xBlock.Text, yBlock.Text, zBlock.Text), UriKind.Relative));

And then in your Start.xaml.cs add the following code to the OnNavigatedTo method to read the data

   protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);

        string x = "";
        string y = "";
        string z = "";

        NavigationContext.QueryString.TryGetValue("xValue", out x);
        NavigationContext.QueryString.TryGetValue("yValue", out y);
        NavigationContext.QueryString.TryGetValue("zValue", out z);
       // Then what ever you want to do with x,y and z value
    }

More on passing data between pages:- Passing parameters between pages, Windows phone 8

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