Saving the State of a WPF Application Page

后端 未结 1 1858
夕颜
夕颜 2021-01-23 09:51

I am creating a software in WPF, and, in the software, the User can load an image, and configure a map.

Basically, once the Image (of a map) is loaded the user can add

相关标签:
1条回答
  • 2021-01-23 10:26

    Perhaps you could save the Windows xaml on exit and reload on application start.

        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 讨论(0)
提交回复
热议问题