开发者

Saving WPF WIndow and Position

开发者 https://www.devze.com 2022-12-30 06:43 出处:网络
What would be the best way to save the window position and size in a WPF app? Currently, I\'m saving the window size and position of a WPF App. Here are the events I handle:

What would be the best way to save the window position and size in a WPF app?

Currently, I'm saving the window size and position of a WPF App. Here are the events I handle:

  1. SourceInitialized : The saved info is loaded on to the window
  2. WindowClosing : The current info is saved to the backing store

(I copied this from an example).

The problem i开发者_运维技巧s, when the window is minimized and restored, the settings from the last WindowClosing is retrieved.

Now, the StateChanged event fire AFTER the window has minimized, so it does not seem to be what i need.

Thanks


Do yourself and your users a favor and use the LocationChanged event and the SizeChanged event to save the settings at that time. There's nothing more annoying than an application that gets amnesia if the process exits abnormally and settings don't get saved (cough...explorer...cough...)

Then just check to make sure the WindowState == Normal before saving the settings. Obviously its pointless to save the position of a minimized or maximized window.

As for when to load the settings, well that you can just do in the constructor after the InitializeComponent call or you can use the Initialized event. No real reason to use the SourceInitialized event unless you are doing something with the HWND directly which shouldn't be necessary.


Use the WindowInteropHelper object to get the window handle and use Screen.FromHandle method to get the actual screen the window is on. When saving make sure to also save the screen bounds just in case it does not exist any more.

One caveat when restoring the screen to its former state is it has to be done after the window handle is created so can't do it in the constructor else won't work properly in multiple monitor situations. Try doing it on the SourceInitialized callback


Are you doing this via databinding? That is the way I do my window sizing and position. I typically have a UserConfig.xml file that is saved in the Users Profile. Then I create elements in there as I databind them in the program. I have the Application.xaml resource dictionary refer to that file, and all of the settings I want set to XPaths inf the XML. Then I just save the in memory xml document on exit. Only one event to handle, no mess, no fuss.

And you can expand it to encompass as many settings as you like in regard to the UI. Adding plumbing settings is a little more difficult, but not terribly so.


I have a solution for saving Size and State, you can extend it to also save the Position. It's done using a Behavior. Simply Binding the Width and Height did not work as expected, because it would overwrite the "Normal" state's size with the maximized sizes. That's why there are some extra checks like if(state == normal)

There is a Config Property on my Window's DataContext. You'll need a reference to System.Windows.Interactivity to do that.

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;
        }
    }
}

In Xaml use the behavior by adding the namespaces

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

And then attach the Behavior

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

You then just have to Load and Save the Config in your DataContext on startup/shutdown.


I liked CodeWarriors answer. I use TwoWay binding to apps settings:

Height="{Binding Source={x:Static p:Settings.Default}, Path=WindowHeight, Mode=TwoWay}"
Width="{Binding Source={x:Static p:Settings.Default}, Path=WindowWidth, Mode=TwoWay}"
Top="{Binding Source={x:Static p:Settings.Default}, Path=WindowTop, Mode=TwoWay}"
Left="{Binding Source={x:Static p:Settings.Default}, Path=WindowLeft, Mode=TwoWay}"

where p - project's properties namespace.

0

精彩评论

暂无评论...
验证码 换一张
取 消