怎样在小程序里实现标题的更改
880
2022-10-30
Jot 是一个用于持久化和管理 .NET 应用程序状态的库
Jot - a .NET library for state persistence
Introduction
Almost every application needs to keep track of its own state, regardless of what it otherwise does. This typically includes:
Sizes and locations of movable/resizable elements of the UI (forms, tool windows, draggable toolbars...)Last entered data (e.g. username, selected tab indexes, recently opened files...)Settings and user preferences
A common approach is to store this data in a .settings file and read and update it as needed. This involves writing a lot of boilerplate code to copy that data back and forth. This code is generally tedious, error-prone and no fun to write.
With Jot, you only need to declare which properties of which objects you want to track, and when to persist and apply data. This is a better abstraction for this requirement, resulting in more readable and concise code.
Installation
Jot is available on NuGet and can be installed from the package manager console:
install-package Jot
Example: Persisting the size and location of a Window
To illustrate the basic idea, let's compare two ways of dealing with this requirement: .settings file (Scenario A) versus Jot (Scenario B).
Scenario A (.settings file)
Step 1: Define settings
Step 2: Apply previously stored data
public MainWindow(){ InitializeComponent(); this.Left = MySettings.Default.MainWindowLeft; this.Top = MySettings.Default.MainWindowTop; this.Width = MySettings.Default.MainWindowWidth; this.Height = MySettings.Default.MainWindowHeight; this.WindowState = MySettings.Default.MainWindowWindowState;}
Step 3: Persist updated data before the window is closed
protected override void OnClosed(EventArgs e){ MySettings.Default.MainWindowLeft = this.Left; MySettings.Default.MainWindowTop = this.Top; MySettings.Default.MainWindowWidth = this.Width; MySettings.Default.MainWindowHeight = this.Height; MySettings.Default.MainWindowWindowState = this.WindowState; MySettings.Default.Save(); base.OnClosed(e);}
This is a lot of work, even for a single window. If there were 10 resizable/movable elements of the UI, the settings file would become a jungle of similarly named properties, making this code quite tedious and error prone to write.
Also notice that for each property of the window, we need to mention it in five places (in the settings file, twice in the constructor and twice in OnClosed).
Scenario B (Jot)
Step 1: Create and configure the tracker
// Expose services as static class to keep the example simple static class Services{ // expose the tracker instance public static Tracker Tracker = new Tracker(); static Services() { // tell Jot how to track Window objects Tracker.Configure
Step 2: Track the window instance
public MainWindow(){ InitializeComponent(); // Start tracking the Window instance. // This will apply any previously stored data and start listening for "WindowClosed" event to persist new data. Services.Tracker.Track(this);}
That's it. We've set up tracking for all window objects in one place, so that all we need to to is call tracker.Track(window) on each window instance to preserve it's size and location. It's concise, the intent is clear, and there's no repetition. Notice also that we've mentioned each property only once, and it would be trivial to track additional properties.
Real world form/window tracking
The above code (both scenarios) works but it doesn't account for a few things. The first one is multiple displays. Screens can be unplugged, and we never want to position a window onto a screen that's no longer there. We can get around this problem very easily if we make the screen resolution part of the identifier. Jot will then track the same window separately for each screen configuration.
WPF Window
Here's how to properly track a WPF window:
// 1. tell the tracker how to track Window objects (this goes in a startup class)tracker.Configure
The Id method has a params object [] parameter that can be used to define a namespace for the id. These parameters simply get ToString-ed and concatenated to the Id. By using the screen resolution as the namespace, we ensure that we maintain separate configurations for different resolutions.
Windows forms
Winforms have a few additional caveats:
Forms will return bogus size/location data for maximized/minimized forms, so we have to cancel persisting thoseTracking needs to be applied during OnLoad since Top and Left properties set in the constructor are ignored
Here's how to properly track (Windows) Forms:
// tell the tracker how to track Form objects (this goes in a startup class)tracker.Configure
发表评论
暂时没有评论,来抢沙发吧~