WPF - Przywrócenie danych sprzed zamknięciem.

0

Witam, mam problem z aplikacją w WPF, otóż aplikacja miała się otwierać w takim samym stanie tzn rozmiar okna oraz pozycja taka jak przed zamknięciem.
Wszystko działa do momentu kiedy nie zamknie sie aplikacji 'zmaksowanej' wtedy wywala i nie da sie jej ponownie uruchomić.
main.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;

namespace WpfApp1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            
            Color kolor = Ustawienia.Czytaj();
            rectangle.Fill = new SolidColorBrush(kolor);
            sliderR.Value = kolor.R;
            sliderG.Value = kolor.G;
            sliderB.Value = kolor.B;

            // Ustala na początku rozmiar
            this.Top = Properties.Settings.Default.Top;
            this.Left = Properties.Settings.Default.Left;
            this.Height = Properties.Settings.Default.Height;
            this.Width = Properties.Settings.Default.Width;
            if (Properties.Settings.Default.Maximized)
                WindowState = WindowState.Maximized;
        }

        private void SliderR_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            Color kolor = Color.FromRgb(
                (byte)sliderR.Value,
                (byte)sliderG.Value,
                (byte)sliderB.Value);
            KolorProstokata = kolor;
        }

        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Escape) Close();
        }

        private Color KolorProstokata
        {
            get
            {
                return (rectangle.Fill as SolidColorBrush).Color;
            }
            set
            {
                (rectangle.Fill as SolidColorBrush).Color = value;
            }
        }
        private void Window_Closed_1(object sender, EventArgs e)
        {
            Ustawienia.Zapisz(KolorProstokata);
            // Uruchamia w rozmiarze przed zamknięciem
            if (WindowState == WindowState.Maximized)
            {
                // Use the RestoreBounds as the current values will be 0, 0 and the size of the screen
                Properties.Settings.Default.Top = RestoreBounds.Top;
                Properties.Settings.Default.Left = RestoreBounds.Left;
                Properties.Settings.Default.Height = RestoreBounds.Height;
                Properties.Settings.Default.Width = RestoreBounds.Width;
                Properties.Settings.Default.Maximized = true;
            }
            else
            {
                Properties.Settings.Default.Top = this.Top;
                Properties.Settings.Default.Left = this.Left;
                Properties.Settings.Default.Height = this.Height;
                Properties.Settings.Default.Width = this.Width;
                Properties.Settings.Default.Maximized = false;
            }

            Properties.Settings.Default.Save();
        }

    }
}

main.xaml

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        KeyDown="Window_KeyDown"
        Closed="Window_Closed_1"
        Title="MainWindow" Height="450" Width="800" MinHeight="450" MinWidth="800">
    <Grid HorizontalAlignment="Center" Width="800">
        <Rectangle x:Name="rectangle" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="200" Margin="15,10,0,0" Stroke="Black" VerticalAlignment="Top" Width="770"/>
        <Slider x:Name="sliderR" Margin="15,260,15,129" Maximum="255" ValueChanged="SliderR_ValueChanged"/>
        <Slider x:Name="sliderG" Margin="15,310,15,79" Maximum="255" ValueChanged="SliderR_ValueChanged"/>
        <Slider x:Name="sliderB" Margin="15,360,15,29" Maximum="255" ValueChanged="SliderR_ValueChanged"/>

    </Grid>
</Window>

Settings
image.png

Błąd:
problem.png

0

https://docs.microsoft.com/en-us/dotnet/api/system.windows.window.restorebounds?view=netframework-4.7.2

If you query RestoreBounds before the window has been shown or after it has been closed, Empty is returned.

https://docs.microsoft.com/en-us/dotnet/api/system.windows.rect.empty?view=netframework-4.7.2

The empty rectangle, which has X and Y property values of PositiveInfinity, and has Width and Height property values of NegativeInfinity.

1 użytkowników online, w tym zalogowanych: 0, gości: 1