简体   繁体   中英

Save image (page state)

I have an app that ask the user to select and image from his photo library then allows him to add text or other object on to the image. but once the user picks the main image and navigates to a secondary page when he navigates back the image is gone. I can't figure this out. I hope it's something simple I've overlooked.

thanks

below is a quick sample that I can test on. I t consist of a main image and 2 buttons one to choose the pic the other to navigate to the second page and the second page just has a button to navigate back to the main page.

 public partial class MainPage : PhoneApplicationPage
{
    // Constructor
    PhotoChooserTask photoChooserTask;


    public MainPage()
    {
        InitializeComponent();

        // Sample code to localize the ApplicationBar
        //BuildLocalizedApplicationBar();
    }

    private void btnAdd_Click(object sender, RoutedEventArgs e)
    {
        photoChooserTask = new PhotoChooserTask();
        photoChooserTask.Completed += new EventHandler<PhotoResult>(photoChooserTask_Completed);
        photoChooserTask.Show();
    }

    void photoChooserTask_Completed(object sender, PhotoResult e)
    {
        //Code to display the photo on the page in an image control named myImage.

        BitmapImage pic = new BitmapImage();
        if (pic != null)
        {
            pic.SetSource(e.ChosenPhoto);
            e.ChosenPhoto.Position = 0;
        }
        else
        {
        }

        imgMain.Source = pic;
        //  appsettings.Add("_imgMain", pic);

    }

    private void btnPage_Click(object sender, RoutedEventArgs e)
    {
        NavigationService.Navigate(new Uri("/Page1.xaml", UriKind.Relative));
    }

second page

 public Page1()
    {
        InitializeComponent();
    }

    private void btnGoBack_Click(object sender, RoutedEventArgs e)
    {
        NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
    }

Here is your problem:

 private void btnGoBack_Click(object sender, RoutedEventArgs e)
 {
    // <<<<< Creates new page >>>>>>>>
    NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
 }

When the back button is pressed you navigate to a new main page, effectively instantiating a new page with new state and controls.

You create this back stack by navigating:

   Main => Page1 => Main

Remove that navigation code, the windows phone os will handle the back navigation to the already created main page for you.

@thumbmunkeys already stated your problem. You must not create any cycle in your navigation stack, better make them in a tree like structure. If your are still willing to use an extra back button use the following code:

private void btnGoBack_Click(object sender, RoutedEventArgs e)
{
    //your additional code here
    NavigationService.GoBack();
}

This will remove one entry from the back stack. But still, there is a chance user hits the hardware back key, in that case your additional codes won't be executed. A better option will be to use PhoneApplicationPage.OnBackKeyPress method insted of a back button.

protected override void OnBackKeyPress(CancelEventArgs e)
{
    base.OnBackKeyPress(e);
    //your additional code here  
}

System will handle the back stack, you will be able to accomplish what you are looking for.

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