本文介绍了你为什么不能绑定一个窗口的大小构成对的applicationSettings?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 更新:解决,用代码 的我得到了它的工作,请参见下面的代码我的答案... 原贴 由于Tundey中的他的回答以我的最后一个问题,可以绑定几乎所有有关Windows窗体控件到的applicationSettings相当费力。那么,有没有真的没有办法与形式尺寸做到这一点? 本教程说,你需要明确地处理大小,这样可以节省RestoreBounds,而不是大小如果窗口被最大化或最小化。不过,我希望我可以使用类似的属性: 公共面积RestoreSize {得到 {如果(this.WindowState == FormWindowState.Normal) {返回this.Size; } ,否则 {返回this.RestoreBounds.Size; } } 组 {:} } 但我不能看到在设计这个绑定(大小主要是从PropertyBinding列表丢失)的方式。 解决方案 我终于想出了解决这个,一劳永逸表单子类。要使用它: 继承RestorableForm而不是形式 添加在(的applicationSettings绑定) - >(PropertyBinding),以WindowRestoreState 通话Properties.Settings.Default.Save()当窗口即将关闭 。 现在窗口的位置和状态将会话之间被记住。继从下面其他海报的建议,我包括一个函数ConstrainToScreen是确保恢复自己当窗口非常适合可用的显示器。 代码 //考虑以下代码公共领域。如果你愿意,你甚至可以告诉 //你的老板,有魅力的女性,或在您的立方体其他家伙, //你写的。请享用! 使用系统;使用System.Windows.Forms的; 使用System.ComponentModel; 使用System.Drawing中; 命名空间实用程序 {公共类RestorableForm:形式,INotifyPropertyChanged的 { //我们调用时要更新绑定需要此事件。 公共事件PropertyChangedEventHandler的PropertyChanged; //这个存储最后窗口的位置和状态私人WindowRestoreStateInfo windowRestoreState; //现在我们定义,我们将绑定到我们设置的属性。 [可浏览(假)] //不显示它在属性列表 [SettingsBindable(真)] //但不要使绑定设置公共WindowRestoreStateInfo WindowRestoreState { {返回windowRestoreState; } 组 { windowRestoreState =价值; 如果(的PropertyChanged!= NULL) { //如果任何人的听力,让他们知道的 //绑定需要进行更新:的PropertyChanged(这一点, 新PropertyChangedEventArgs(WindowRestoreState)); } } } 保护覆盖无效OnClosing(CancelEventArgs E) { WindowRestoreState =新WindowRestoreStateInfo(); WindowRestoreState.Bounds =的WindowState == FormWindowState.Normal? 边界:RestoreBounds; WindowRestoreState.WindowState =的WindowState; base.OnClosing(E); } 保护覆盖无效的OnLoad(EventArgs的发送) { base.OnLoad(E); 如果(WindowRestoreState!= NULL) {边界= ConstrainToScreen(WindowRestoreState.Bounds); 的WindowState = WindowRestoreState.WindowState; } } //这个辅助类,同时存储位置和状态。 //这样,我们只需要设置一个绑定。 公共类WindowRestoreStateInfo {矩形范围; 公共矩形范围 { {返回界限; } 集合{=界限值; } } FormWindowState WINDOWSTATE; 公共FormWindowState的WindowState { {返回WINDOWSTATE; } 集合{WINDOWSTATE =价值; } } } 私人矩形ConstrainToScreen(矩形范围) {屏幕屏幕= Screen.FromRectangle(WindowRestoreState.Bounds); 矩形workingArea = screen.WorkingArea; INT宽度= Math.Min(bounds.Width,workingArea.Width); INT高= Math.Min(bounds.Height,workingArea.Height); //嗯....极小 INT左= Math.Min(workingArea.Right - 宽度, Math.Max(bounds.Left,workingArea.Left)) ; INT顶部= Math.Min(workingArea.Bottom - 高度, Math.Max(bounds.Top,workingArea.Top)); 返回新的Rectangle(左,上,宽度,高度); } } } 设置绑定参考 SettingsBindableAttribute INotifyPropertyChanged的 Update: Solved, with codeI got it working, see my answer below for the code...Original PostAs Tundey pointed out in his answer to my last question, you can bind nearly everything about a windows forms control to ApplicationSettings pretty effortlessly. So is there really no way to do this with form Size? This tutorial says you need to handle Size explicitly so you can save RestoreBounds instead of size if the window is maximized or minimized. However, I hoped I could just use a property like:public Size RestoreSize{ get { if (this.WindowState == FormWindowState.Normal) { return this.Size; } else { return this.RestoreBounds.Size; } } set { ... }}But I can't see a way to bind this in the designer (Size is notably missing from the PropertyBinding list). 解决方案 I finally came up with a Form subclass that solves this, once and for all. To use it:Inherit from RestorableForm instead of Form.Add a binding in (ApplicationSettings) -> (PropertyBinding) to WindowRestoreState.Call Properties.Settings.Default.Save() when the window is about to close.Now window position and state will be remembered between sessions. Following the suggestions from other posters below, I included a function ConstrainToScreen that makes sure the window fits nicely on the available displays when restoring itself.Code// Consider this code public domain. If you want, you can even tell// your boss, attractive women, or the other guy in your cube that// you wrote it. Enjoy!using System;using System.Windows.Forms;using System.ComponentModel;using System.Drawing;namespace Utilities{ public class RestorableForm : Form, INotifyPropertyChanged { // We invoke this event when the binding needs to be updated. public event PropertyChangedEventHandler PropertyChanged; // This stores the last window position and state private WindowRestoreStateInfo windowRestoreState; // Now we define the property that we will bind to our settings. [Browsable(false)] // Don't show it in the Properties list [SettingsBindable(true)] // But do enable binding to settings public WindowRestoreStateInfo WindowRestoreState { get { return windowRestoreState; } set { windowRestoreState = value; if (PropertyChanged != null) { // If anybody's listening, let them know the // binding needs to be updated: PropertyChanged(this, new PropertyChangedEventArgs("WindowRestoreState")); } } } protected override void OnClosing(CancelEventArgs e) { WindowRestoreState = new WindowRestoreStateInfo(); WindowRestoreState.Bounds = WindowState == FormWindowState.Normal ? Bounds : RestoreBounds; WindowRestoreState.WindowState = WindowState; base.OnClosing(e); } protected override void OnLoad(EventArgs e) { base.OnLoad(e); if (WindowRestoreState != null) { Bounds = ConstrainToScreen(WindowRestoreState.Bounds); WindowState = WindowRestoreState.WindowState; } } // This helper class stores both position and state. // That way, we only have to set one binding. public class WindowRestoreStateInfo { Rectangle bounds; public Rectangle Bounds { get { return bounds; } set { bounds = value; } } FormWindowState windowState; public FormWindowState WindowState { get { return windowState; } set { windowState = value; } } } private Rectangle ConstrainToScreen(Rectangle bounds) { Screen screen = Screen.FromRectangle(WindowRestoreState.Bounds); Rectangle workingArea = screen.WorkingArea; int width = Math.Min(bounds.Width, workingArea.Width); int height = Math.Min(bounds.Height, workingArea.Height); // mmm....minimax int left = Math.Min(workingArea.Right - width, Math.Max(bounds.Left, workingArea.Left)); int top = Math.Min(workingArea.Bottom - height, Math.Max(bounds.Top, workingArea.Top)); return new Rectangle(left, top, width, height); } }}Settings Bindings ReferencesSettingsBindableAttributeINotifyPropertyChanged 这篇关于你为什么不能绑定一个窗口的大小构成对的applicationSettings?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-28 15:46