page state

branedge

New member
Jul 9, 2009
29
0
0
Visit site
I have an app that i'm working on where I add an image from the camera roll onto the main page they I navigate to a secondary page to create some text that I will overlay on the main picture. the problem is when I leave the main page to go to the text page when I come back to the main page my image is gone and I have to add it again. can someone tell me how to save the image in isolated storage or how to navigate back to the page without loosing the image.

thanks
 

Im_Q

New member
Nov 26, 2012
119
0
0
Visit site
I second the suggestion for handling the text input in the same page. If this is not ideal, please indicate whether you're using WinRT or Silverlight so we can make further suggestions.
 

diapers

New member
Apr 16, 2012
99
0
0
Visit site
I don't know why people are suggesting you do it in one page. The image will still be blank if the screen goes idle or the user presses the start button to check a text or phone call. Saving the page state should be part of your normal page code. In your case, just save the image as a bmp to isolated storage in OnNavigatedFrom(..), and restore it in OnNavigatedTo(..).

e.g when the page is navigated away...

if (bmp != null)
{
string tmpfilename = "TempJPEG" + Guid.NewGuid().ToString();
WriteableBitmap bitmap = new WriteableBitmap(bmp);
IsolatedStorageFileStream myFileStream = IsolatedStorageFile.GetUserStoreForApplication().CreateFile(tmpfilename);
bitmap.SaveJpeg(myFileStream, bitmap.PixelWidth, bitmap.PixelHeight, 0, 100);
myFileStream.Close();
State["ImageTempFileName"] = tmpfilename;
bmp = null;
}

when you navigate back, check if there's a saved state...

if (State.ContainsKey("ImageTempFileName")) //isNewInstance N/A here since we set the bmp to null on navigatefrom
{
string tmpfilename = (string)State["ImageTempFileName"];
bmp = new BitmapImage();
try
{
using (IsolatedStorageFileStream isoStreamRead = new IsolatedStorageFileStream(tmpfilename, System.IO.FileMode.Open, IsolatedStorageFile.GetUserStoreForApplication()))
{
bmp.SetSource(isoStreamRead);
myImage.Source = bmp;
myImage.Stretch = Stretch.Uniform;
}
}
catch (IsolatedStorageException ex)
{
System.Diagnostics.Debug.WriteLine("IsolatedStorageException: " + ex.Message);
}
IsolatedStorageFile.GetUserStoreForApplication().DeleteFile(tmpfilename);
}



Not too difficult.
 

Im_Q

New member
Nov 26, 2012
119
0
0
Visit site
I don't know why people are suggesting you do it in one page.

Because if it's a quick/simple/lightweight operation, it's jarring to leave the content of focus then come back. The exception is when the operation requires maximum screen real estate. That's why there's two ways a ComboBox opens in WP (expand vs full screen).

The image will still be blank if the screen goes idle or the user presses the start button to check a text or phone call.

Without knowing his code, we can't assume that. In WP8.0 Silverlight, everything should stay in memory so long as the app doesn't get terminated. In WinRT, the page only gets unloaded if you navigate to another page in the same app or gets terminated so I agree that in WinRT, you should be saving your page state when appropriate for any page. In either case, leaving the app or screen idling shouldn't result in lost page state unless the app terminates. And when I say terminate, I mean tombstone.

I'm not saying you're wrong, I'm just saying we need a bit more info to suggest the best course of action for the OP.

Edit: More info as in WinRT or not. Because this sounds a lot like the NavigationCacheMode issue that people going from SL to WinRT get caught up on.
 

diapers

New member
Apr 16, 2012
99
0
0
Visit site
I'm not wrong you will lose your data if the app is tombstoned!

Every page in your app should implement this.. and implement it properly. It will save you a lot of tombstone/fast resume headaches

A lot of devs don't notice that their app only goes dormant during testing and forget to implement proper savestates in case they get tombstoned..

"In Windows Phone OS 7.1, apps are made dormant when the user navigates away, as long as sufficient memory is available for the foreground app to run smoothly. When an app is dormant and then restored, the UI state is automatically preserved. To verify that your page state is restored properly after tombstoning, you need to enable automatic tombstoning in the debugger."

How to preserve and restore page state for Windows Phone 8
 

Im_Q

New member
Nov 26, 2012
119
0
0
Visit site
I'm not wrong you will lose your data if the app is tombstoned!

... I didn't say you're wrong and I explicit said that. I'm not sure why you're exclaiming it. I also gave ample examples of cases that a page might get unloaded and lose its state (two of which involves non-user app termination aka tombstoning). I will reiterate that I think we need more info from the OP. I will elaborate on why we need more info. What he's describing about losing info on normal navigation sounds very much like a WinRT issue. If that's the case, you do not want to use IsolatedStorage. Instead you can use the SuspensionManager page state mechanism to store the path of the file to retrieve the StorageFile again once you renavigate to the page. WinRT vs SL is very important here since I believe one avoids having to write to local storage a copy of the file. In SL8.1 and WinRT, you only need to store the path of the original as a string.
 

branedge

New member
Jul 9, 2009
29
0
0
Visit site
Thanks guys!, sorry wasn't getting emails that the post had been responded to. anyway it is a wp8 Silverlight app. I would like to used a second page because the text being enter has options for color and font. so I though it would be easier/best to do this on second page.

dipaers will give your code a shot asap..and let you know how it goes.

thanks!
 

Members online

Forum statistics

Threads
326,611
Messages
2,248,633
Members
428,522
Latest member
BarkerJarrod