Saving WPF WIndow and Position

What would be the best way to keep the position and size of the window in a WPF application?

I am currently saving the window size and position of the WPF application. Here are the events I'm processing:

  • SourceInitialized: saved information is loaded into the window
  • WindowClosing: current information is stored in the repository

(I copied this from an example).

The problem is that when the window is minimized and restored, the settings are retrieved from the last window.

Now the StateChanged fire AFTER the window has minimized, so it seems this is not what I need.

thank

+3
source share
5 answers

LocationChanged SizeChanged, . , , , , (... ... ...)

, WindowState == Normal . , .

, , , InitializeComponent Initialized. SourceInitialized, - HWND , .

+9

WindowInteropHelper, Screen.FromHandle , . , .

- , , . SourceInitialized

+1

? . UserConfig.xml, . , . Application.xaml, , , XPaths XML. xml . , , .

, , . , .

0

, , . . , , "" . , if(state == normal)

DataContext Config. System.Windows.Interactivity.

public class MainWindowSaveStateBehavior : Behavior<Window>
{
    public Config Config
    {
        get { return (Config)GetValue(ConfigProperty); }
        set { SetValue(ConfigProperty, value); }
    }

    public static readonly DependencyProperty ConfigProperty =
        DependencyProperty.Register("Config", typeof(Config), typeof(MainWindowSaveStateBehavior), new PropertyMetadata(Config_Changed));

    private static void Config_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var b = d as MainWindowSaveStateBehavior;
        if(e.NewValue != null) b.LoadSettings();
    }

    protected override void OnAttached()
    {
        base.OnAttached();
        AssociatedObject.SizeChanged += Window_SizeChanged;
        AssociatedObject.StateChanged += Window_StateChanged;
        LoadSettings();
    }

    bool _initialized = false;

    private void Window_StateChanged(object sender, EventArgs e)
    {
        SaveSettings();
    }

    private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
    {
        SaveSettings();
    }

    private void LoadSettings()
    {
        if (Config == null) return;

        AssociatedObject.Width = Config.WindowWidth;
        AssociatedObject.Height = Config.WindowHeight;
        AssociatedObject.WindowState = Config.WindowState;

        _initialized = true;
    }

    private void SaveSettings()
    {
        if (Config == null || !_initialized) return;

        Config.WindowState = AssociatedObject.WindowState;
        if(AssociatedObject.WindowState == WindowState.Normal)
        {
            Config.WindowWidth = AssociatedObject.Width;
            Config.WindowHeight = AssociatedObject.Height;
        }
    }
}

Xaml ,

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:b="<the namespace your behavior lives in>"

<i:Interaction.Behaviors>
    <b:MainWindowSaveStateBehavior Config="{Binding Config}" />
</i:Interaction.Behaviors>

DataContext / .

0

WPF: https://github.com/GabrielZalisz/WindowPositionSaver

It uses custom properties and saves and restores the position of the window. It works independently for 3 different monitor configurations.

0
source

Source: https://habr.com/ru/post/1745725/


All Articles