简体   繁体   English

保存图像(页面状态)

[英]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. 它由一个主图像和2个按钮组成,一个按钮选择图片,另一个按钮导航到第二页,第二个页面只有一个按钮导航回主页。

 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. 删除该导航代码,Windows Phone OS将为您处理向后导航到已经创建的主页。

@thumbmunkeys already stated your problem. @thumbmunkeys已经说明您的问题。 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. 更好的选择是使用安装有后退按钮的PhoneApplicationPage.OnBackKeyPress方法

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. 系统将处理后退堆栈,您将能够完成所需的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM