windows phone 7 - 在独立存储中保存图像状态

时间:2012-12-28 05:24:17

标签: c# image windows-phone-7

你好朋友我是Windows手机的新手正在开发一个用于学习的应用程序

这是一个带有图像组的单页应用程序,我正在显示基于 Image_tap()的图像,它完美运行。现在我想在 Application_closing 时保存图像状态(图像的来源),我想要检索状态 Application_launching

MainPage.xaml.cs 文件

中的

 PhoneApplicationService phoneAppservice = PhoneApplicationService.Current;     
 private void Image_Tap(object sender, GestureEventArgs e)
    {
        Image mybutton = (Image)sender;
        image1.Source = mybutton.Source;
        phoneAppservice.State["myValue"] = mybutton.Source;
    }

    private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
    {
        object value;
        if (phoneAppservice.State.TryGetValue("myValue", out value))
        {
            image1.Source = (System.Windows.Media.ImageSource)value;
        }
    }

app.xaml.cs 文件中:

 private void Application_Launching(object sender, LaunchingEventArgs e)
    {
        getSource();
    }

 private void Application_Closing(object sender, ClosingEventArgs e)
    {
        saveSource();
    }

private void saveSource()
    {
        PhoneApplicationService phoneAppservice = PhoneApplicationService.Current;
        IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;

        settings["myValue"] = phoneAppservice.State["myValue"];
    }

    private void getSource()
    {
        PhoneApplicationService phoneAppservice = PhoneApplicationService.Current;
        IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;

        object myValue;
        if(settings.TryGetValue<object>("myValue", out myValue))
        {
            phoneAppservice.State["myValue"] = myValue;
        }
    }

正在保存获取图像源,无法将该源设置为我的图像。我认为我遗漏了一些东西,或者请提出正确的方法

提前致谢

1 个答案:

答案 0 :(得分:0)

在状态中,我保存了图像名称而不是图像源,之后给出了显示该图像的路径

在MainPage.xaml.cs 文件中

我已经为多个图像使用了单个Image_Tap事件

 private void Image_Tap(object sender, GestureEventArgs e)
    {
        Image mybutton = (Image)sender; // calcifying image based on image taped  
        image1.Source = mybutton.Source; // setting tapped source to image 
        phoneAppservice.State["myValue"] = mybutton.Name; // here saving the name of the image 
    }

    private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
    {
        object value;
        if (phoneAppservice.State.TryGetValue("myValue", out value))
        {
            //retrieving the image name form state and creating new BitMapimage based on this name 
            BitmapImage newImg = new BitmapImage(new Uri("/Gallery;component/Images/"+value+".jpg", UriKind.Relative));
            image1.Source = newImg; 
        }
    }

在App.xaml.cs文件中

当application_closing并在应用程序启动时检索状态形式隔离存储时,将状态保存在独立存储中

 private void Application_Launching(object sender, LaunchingEventArgs e)
    {
        getSource();
    }

 private void Application_Closing(object sender, ClosingEventArgs e)
    {
        saveSource();
    }

 private void saveSource()
    {
        PhoneApplicationService phoneAppservice = PhoneApplicationService.Current;
        IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;

        settings["myValue"] = phoneAppservice.State["myValue"]; //storing the state (image name) to isolated storage

    }

    private void getSource()
    {
        PhoneApplicationService phoneAppservice = PhoneApplicationService.Current;
        IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;

        object myValue;
        if(settings.TryGetValue<object>("myValue", out myValue))
        {
            phoneAppservice.State["myValue"] = myValue; // saving the state from isolated storage
        }
    }