2012-12-02 54 views
0

我正在WPF中創建一個軟件,並且在軟件中,用戶可以加載圖像並配置一個映射。保存WPF應用程序的狀態頁面

基本上,一旦加載圖像(地圖),用戶可以添加其他圖像(如寶藏或怪物的圖片等),將它們拖放到地圖的圖像中。

當用戶關閉軟件並重新打開軟件時,我想要在關閉應用程序之前打開最後一張圖像,並將用戶界面元素添加到同一位置,例如用戶設置的方式。

我可以想到的一種方式是將圖像文件存儲在字節數組中,或者保存文件位置,另一個UI元素位於文本文件中並在每次加載應用程序時檢查它。

但是,有沒有更簡單的方法來解決這個問題?有沒有一種方法可以存儲WPF應用程序頁面的狀態?

回答

0

也許你可以在退出時保存Windows xaml並在應用程序啓動時重新加載。

public MainWindow() 
    { 
     InitializeComponent(); 
     Loaded += new RoutedEventHandler(MainWindow_Loaded); 
     Closing += new CancelEventHandler(MainWindow_Closing); 
    } 


    void MainWindow_Loaded(object sender, RoutedEventArgs e) 
    { 
     LoadExternalXaml(); 
    } 

    void MainWindow_Closing(object sender, CancelEventArgs e) 
    { 
     SaveExternalXaml(); 
    } 


    public void LoadExternalXaml() 
    { 
     if (File.Exists(@"C:\Test.xaml")) 
     { 
      using (FileStream stream = new FileStream(@"C:\Test.xaml", FileMode.Open)) 
      { 
       this.Content = XamlReader.Load(stream); 
      } 
     } 
    } 

    public void SaveExternalXaml() 
    { 
     using (FileStream stream = new FileStream(@"C:\Test.xaml", FileMode.Create)) 
     { 
      XamlWriter.Save(this.Content, stream); 
     } 
    } 
+0

我試過這段代碼,但是,當我嘗試加載XAML時,它停止調試。但它確實保存了xaml。 –

+0

把一個Try/Catch放在負載上,並檢查是什麼導致異常,我一直在使用這個沒有問題多年 –