简体   繁体   中英

How to display passed information for Windows Phone 8.1

I am trying to develop a app for windows phone 8.1. I am complete beginner in app dev. Right now my question is - how can I displaay the information passed in another page inside a textbox or some sort. Its a shopping app and I am trying to get the name of the item which is a button, appear in the Basket page when the basket is clicked on. Code is below. So ie Item from Menu.xaml show up in Basket.xaml when button in Menu.xaml is selected.

Basket.Xaml

  protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        Menu.PassedData data = e.Parameter as Menu.PassedData;

    }

So say for example I have a txt box in Basket.xaml which aims to show the Item chosen in Menu.xaml how can I go about doing that?

If the data you want to pass is of primitive type you can pass like you did. If not primitive data You can store in LocalSettings. To do so you have to serialize your class to Json,xml

LocalSettings.Values[key]=Json Converter.Serialize(object)

But LocalSettings has some size restrictions, if data to be passed is very big you can serialize and store to a file and use that in next page

Here us code to serialize and deserialize

MemoryStream sessionData = new MemoryStream();
DataContractSerializer serializer = new 
            DataContractSerializer(typeof(Menu.PassedData;
));
serializer.WriteObject(sessionData, data);


StorageFile file = await ApplicationData.Current.LocalFolder
                         .CreateFileAsync(sFileName);
using (Stream fileStream = await file.OpenStreamForWriteAsync())
{
    sessionData.Seek(0, SeekOrigin.Begin);
    await sessionData.CopyToAsync(fileStream);
    await fileStream.FlushAsync();
}
Deserialize back this way -


StorageFile file = await ApplicationData.Current.LocalFolder.
                           GetFileAsync(sFileName);
using (IInputStream inStream = await file.OpenSequentialReadAsync())
{
    DataContractSerializer serializer = 
            new DataContractSerializer(typeof(Menu.PassedData;
));
    var data = (Menu.PassedData;
)serializer
                     .ReadObject(inStream.AsStreamForRead());
}

For more information please refer this link

In the second page (the basket one, if I understood correctly), you should be able to access the passed data, pretty easy :

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    var passedData = e.Parameter as PassedData; //cast the object back to PassedData type.
    someTextBlockOnBasket.Text = passedData.Name;

}

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