0

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

这是一个带有一组图像的单页应用程序,并且正在基于Image_tap()显示图像,它运行良好。现在我想在Application_closure时保存图像状态(图像源)并且我想检索状态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;
        }
    }

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

提前致谢

4

1 回答 1

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 文件中

这里在应用程序关闭时将状态保存在隔离存储中,并在应用程序启动时检索状态形式隔离存储

 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
        }
    }
于 2012-12-28T07:54:40.297 回答