2012-12-28 160 views
0

朋友你好是新的Windows手機正在開發一個應用學習的Windows Phone 7 - 保存圖像狀態在獨立存儲

這是一個單頁的應用程序與一組圖像和基於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 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; 
     } 
    } 

am saving the getting ima ge源,並且不能將該源設置爲我的圖像。我覺得我失去了一些東西或請建議花葯正確的方式提前

感謝

+0

什麼是'PhoneApplicationPage_Loaded'這裏保存在獨立存儲的狀態。它是MainPage的Loaded事件處理程序嗎?如果是這樣,在那裏放置一個斷點並告訴你在'value'字段中獲得了什麼值? – nkchandra

+0

感謝您的重播..!我只是休閒chanal9視頻,適用於textBox值。現在我以前解決了這個問題,我試圖在狀態保存圖像源,我認爲這是錯誤的 – kartheek

+0

如果你的問題解決了,然後發佈你的解決方案作爲答案。 – nkchandra

回答

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 
     } 
    } 
相關問題