Windows phone 7在隔离存储中保存图像状态

本文关键字:保存 图像 状态 存储 隔离 phone Windows | 更新日期: 2023-09-27 17:49:48

朋友们好,我是Windows phone的新手,正在开发一款学习应用

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

mainpage . example .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. xml .cs file:
 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;
        }
    }

我正在保存获取图像源,但无法将该源设置为我的图像。我想我遗漏了什么,或者请建议另一种正确的方法

thanks in advance

Windows phone 7在隔离存储中保存图像状态

在状态下,我已经保存了图像名称而不是图像来源,然后给出了该图像的路径以显示

mainpage . example .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_close时将状态保存在隔离存储中,在应用程序启动时从隔离存储中检索状态

 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
        }
    }