AutoWindowPlacement.WPF
1.0.0.1
dotnet add package AutoWindowPlacement.WPF --version 1.0.0.1
NuGet\Install-Package AutoWindowPlacement.WPF -Version 1.0.0.1
<PackageReference Include="AutoWindowPlacement.WPF" Version="1.0.0.1" />
<PackageVersion Include="AutoWindowPlacement.WPF" Version="1.0.0.1" />
<PackageReference Include="AutoWindowPlacement.WPF" />
paket add AutoWindowPlacement.WPF --version 1.0.0.1
#r "nuget: AutoWindowPlacement.WPF, 1.0.0.1"
#:package AutoWindowPlacement.WPF@1.0.0.1
#addin nuget:?package=AutoWindowPlacement.WPF&version=1.0.0.1
#tool nuget:?package=AutoWindowPlacement.WPF&version=1.0.0.1
AutoWindowPlacement.WPF
A lightweight library that automatically saves and restores WPF window positions and states between application sessions.
Features
- Automatic window placement persistence - Save and restore window position, size, and state
- Registry-based storage - Built-in storage using Windows Registry
- Extensible storage - Implement custom storage strategies (JSON, XML, database, etc.)
- Simple XAML integration - Add with a single line of code
- Zero dependencies - Pure WPF implementation
Supported Frameworks
- .NET 5.0-windows, 6.0-windows, 7.0-windows, 8.0-windows
Installation
Install via NuGet Package Manager:
Install-Package AutoWindowPlacement.WPF
Or via .NET CLI:
dotnet add package AutoWindowPlacement.WPF
Quick Start
Add the namespace to your Window XAML:
<Window x:Class="YourApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:awp="https://github.com/nullsoftware/AutoWindowPlacement.WPF"
awp:WindowExtensions.PlacementStorageStrategy="{awp:RegistryStorage}"
Title="Main Window" Height="450" Width="800">
</Window>
That's it! The window position and state will now be automatically saved to the Windows Registry and restored on next launch.
Usage
Basic Usage with Registry Storage
The simplest way to use AutoWindowPlacement is with the built-in RegistryStorage:
<Window xmlns:awp="https://github.com/nullsoftware/AutoWindowPlacement.WPF"
awp:WindowExtensions.PlacementStorageStrategy="{awp:RegistryStorage}">
</Window>
By default, this stores window placement in:
HKEY_CURRENT_USER\SOFTWARE\{CompanyName}\{ProductName}\{WindowName}.Placement
Custom Registry Location
Customize the registry key and naming format:
<Window awp:WindowExtensions.PlacementStorageStrategy="{awp:RegistryStorage
Key='SOFTWARE\\MyCompany\\MyApp\\WindowSettings',
NameFormat='{0}_Position'}">
</Window>
Custom Storage Implementation
Implement the IWindowPlacementStorage interface for custom storage:
using System.Windows;
using NullSoftware.Windows;
public class JsonFileStorage : IWindowPlacementStorage
{
private readonly string _filePath;
public JsonFileStorage(string filePath)
{
_filePath = filePath;
}
public byte[]? LoadPlacement(Window window)
{
// Load from JSON file
// Return byte array or null if not found
}
public void SavePlacement(Window window, byte[] serializedPlacement)
{
// Save to JSON file
}
}
Then use it in code-behind:
public MainWindow()
{
InitializeComponent();
WindowExtensions.SetPlacementStorageStrategy(this, new JsonFileStorage("settings.json"));
}
API Reference
WindowExtensions
The main class providing attached properties for window placement.
Attached Properties
PlacementStorageStrategy- Gets or sets the storage strategy for window placement
WindowExtensions.SetPlacementStorageStrategy(window, storageInstance);
IWindowPlacementStorage storage = WindowExtensions.GetPlacementStorageStrategy(window);
IWindowPlacementStorage
Interface for implementing custom storage strategies.
public interface IWindowPlacementStorage
{
void SavePlacement(Window window, byte[] serializedPlacement);
byte[]? LoadPlacement(Window window);
}
RegistryStorage
Built-in implementation that stores window placement in Windows Registry.
Properties
Hive- Registry hive (default:RegistryHive.CurrentUser)Key- Registry key path (default:SOFTWARE\{Company}\{Product})NameFormat- Value name format (default:{0}.Placement)
Methods
GetSettingKey(Window)- Override to customize the registry value nameProvideDefaultHive()- Override to change default registry hiveProvideDefaultKey()- Override to change default registry key path
WindowPlacementManager
Low-level API for direct window placement manipulation.
// Get window placement
var placement = WindowPlacementManager.GetPlacement(window);
// Set window placement
WindowPlacementManager.SetPlacement(window, placement);
// Serialize placement to bytes
byte[] data = WindowPlacementManager.Serialize(placement);
// Deserialize placement from bytes
var placement = WindowPlacementManager.Deserialize(data);
Advanced Examples
Per-User Storage with Custom Key
<Window awp:WindowExtensions.PlacementStorageStrategy="{awp:RegistryStorage
Key='SOFTWARE\\MyApp\\Settings',
Hive='CurrentUser'}">
</Window>
Conditional Storage in Code-Behind
public MainWindow()
{
InitializeComponent();
if (Settings.Default.RememberWindowPosition)
{
WindowExtensions.SetPlacementStorageStrategy(this, new RegistryStorage());
}
}
Custom Storage with Inheritance
public class CustomRegistryStorage : RegistryStorage
{
protected override string GetSettingKey(Window window)
{
// Use window title instead of type name
return string.Format(NameFormat, window.Title.Replace(" ", "_"));
}
protected override string ProvideDefaultKey()
{
return @"SOFTWARE\MyCompany\MyApp\Windows";
}
}
How It Works
- When
PlacementStorageStrategyis set, the library hooks into window events - On
SourceInitialized, it loads the saved placement and applies it to the window - On
Closing, it captures the current placement and saves it via the storage strategy - Window placement includes: position, size, and state (normal/maximized/minimized)
Design-Time Support
The library automatically detects design-time mode and disables itself in the Visual Studio designer, ensuring a smooth design experience.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Credits
Developed by Null Software
Repository
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0-windows7.0 is compatible. net6.0-windows was computed. net6.0-windows7.0 is compatible. net7.0-windows was computed. net7.0-windows7.0 is compatible. net8.0-windows was computed. net8.0-windows7.0 is compatible. net9.0-windows was computed. net10.0-windows was computed. |
-
net5.0-windows7.0
- No dependencies.
-
net6.0-windows7.0
- No dependencies.
-
net7.0-windows7.0
- No dependencies.
-
net8.0-windows7.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.