Reputation: 810
Below program is to select metre and centimeter from a picker and they both are concatenated like if metre is 53 and cm is 4 then the result is 53.4. What I want to do is, the final output height(example:53.4), age, gender should be accessed through Page1.xaml(next page). I have many values of this kind which i need to implement in a formula. How to transfer these values through IsolatedStorage in Windows Phone to the next page? Thank you.
//Selecting height
private void MHSelect_Click(object sender, RoutedEventArgs e)
{
int mhvalue1 = MHMeterSelector.SelectedItem;
int mhvalue2 = MHCentimeterSelector.SelectedItem;
if(mhvalue1 == 0)
{
mhvalue1 = MHMeterSelector.DefaultValue;
}
MHeight_btn.Content = float.Parse(string.Format("{0}.{1}", mhvalue1.ToString(), mhvalue2.ToString())) + " cm";
//height is selected and concatenated here. mhvalue1 and mhvalue2 are the metre and centimetre values.
}
//Selecting Age
private void MASelect_Click(object sender, RoutedEventArgs e)
{
int mavalue1 = MAMeterSelector.SelectedItem;
if(mavalue1 == 0)
{
mavalue1 = MAMeterSelector.DefaultValue;
}
MAge_btn.Content = mavalue1;
//Age is selected and mavalue1 is the selected age.
}
//Selecting Gender
private void MGenderListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ListBoxItem kbi = ((sender as ListBox).SelectedItem as ListBoxItem);
MGender_btn.Content = kbi.Content.ToString();
//kbi is selected and kbi.Content should be transfered to next page
}
Upvotes: 0
Views: 303
Reputation: 84
I often make a class to store my variables in called ClsController. In order to get access to the anywhere do the following:
In App.xaml.cs add this:
private static ClsController controlerLink = null;
public static ClsController ControlerLink
{
get
{
if (controlerLink == null)
{
controlerLink = new ClsController();
}
return controlerLink;
}
}
When you want to access the class, just write this in the top of your class:
ClsController controlerLink = App.ControlerLink;
string myData = controlerlink.name;
or to call method
controlerLink.MyMethod();
Upvotes: 0
Reputation: 249
Methods to pass parameters
1.Using the query string
You can pass parameters through the query string, using this method means have to convert your data to strings and url encode them. You should only use this to pass simple data.
Navigating page:
page.NavigationService.Navigate(new Uri("/Views/Page.xaml?parameter=test",UriKind.Relative));
Destination page:
string parameter = string.Empty;
if (NavigationContext.QueryString.TryGetValue("parameter", out parameter)) {
{
this.label.Text= parameter;
}
2.Using NavigationEventArgs
Navigating page:
page.NavigationService.Navigate(new Uri("/Views/Page.xaml?parameter=test",UriKind.Relative));
// and ..
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
// NavigationEventArgs returns destination page
Page destinationPage = e.Content as Page;
if (destinationPage != null)
{
// Change property of destination page
destinationPage.PublicProperty = "String or object..";
Destination page:
// Just use the value of "PublicProperty"..
3.Using Manual navigation
Navigating page:
page.NavigationService.Navigate(new Page("passing a string to the constructor"));
Destination page:
public Page(string value) {// Use the value in the constructor...}
Difference between Uri and manual navigation I think the main difference here is the application life cycle. Pages created manually are kept in memory for navigation reasons. Read more about it here.
Passing complex objects You can use method one or two to pass complex objects (recommended). You can also add custom properties to the Application class or store data in Application.Current.Properties.
Upvotes: 0
Reputation: 1035
I am not sure why do you need to transfer the data using IsolatedStorage? If you only want to transfer some data from one page to another page you can use Application.Current.Resources.Add().
For example, Imagine you have two pages including Page1 and Page2. you want to send an integer from the Page1 to Page2. So your code should be something like this:
Page1
int sendInteger=0;
Application.Current.Resources.Clear();
Application.Current.Resources.Add("send",sendThis);
Page2
int receiveInteger; // receive the integer
object obj = Application.Current.Resources["send"];
receiveInteger =(int)obj;
using this method you can only transfer data from one page to another. I doesnt save your data on any file.
Upvotes: 1
Reputation: 10889
I come from the "windows" world, but I would do the following:
Get the Isolated storage for your User / APP
using (var isoStorage = IsolatedStorageFile.GetUserStoreForApplication()) {
}
Create a File in there for your data, possibly simply serialize your data - I think I would create a single Class "Height" which contains Metres and Centimetres as Integer Properties. This could be saved easily. If the File Exists, I would overwrite it.
In your next Page, get the Isolated Storage File, deserialize the Data and Display them.
Upvotes: 0