public static WindowPosition FromWindow(Window window) { var wp = new WindowPosition(); if (window.WindowState == WindowState.Normal) { wp._left = window.Left; wp._top = window.Top; wp._width = window.Width; wp._height = window.Height; wp._maximized = false; } else { wp._left = window.RestoreBounds.Left; wp._top = window.RestoreBounds.Top; wp._width = window.RestoreBounds.Width; wp._height = window.RestoreBounds.Height; wp._maximized = true; } return wp; }
public bool Load(string path) { // if the settings file doesn't exist, let's just return 'true' and use the default values as created by the class' constructor. if (!File.Exists(path)) return true; XmlReader xml = null; bool result; try { xml = XmlReader.Create(path); while (xml.Read()) { if (xml.NodeType == XmlNodeType.Element) { var name = xml.Name; if (name == XmlNodes.RememberWindowPositionAndSize) { _rememberWindowPositionAndSize = XmlConvert.ToBoolean(xml.ReadString()); } else if (name == XmlNodes.WindowPositionAndSize) { var wp = _ReadWindowPositionFromXml(xml.ReadSubtree()); if (wp != null) { _mainWindowPosition = wp; } } } } result = true; } catch (Exception e) // catch all { Trace.WriteLine("Exception: " + e.Message); result = false; } finally { xml?.Dispose(); xml?.Close(); } // final fallback return result; }
private WindowPosition _ReadWindowPositionFromXml(XmlReader xml) { var wp = new WindowPosition(); try { while (xml.Read()) { if (xml.NodeType == XmlNodeType.Element) { var name = xml.Name; if (name == XmlNodes.WindowPositionLeft) { var s = xml.ReadString(); var d = XmlConvert.ToDouble(s); wp.Left = d; } else if (name == XmlNodes.WindowPositionTop) wp.Top = XmlConvert.ToDouble(xml.ReadString()); else if (name == XmlNodes.WindowPositionWidth) wp.Width = XmlConvert.ToDouble(xml.ReadString()); else if (name == XmlNodes.WindowPositionHeight) wp.Height = XmlConvert.ToDouble(xml.ReadString()); else if (name == XmlNodes.WindowPositionIsMaximized) wp.IsMaximized = XmlConvert.ToBoolean(xml.ReadString()); } } } catch { wp = null; } finally { xml?.Close(); xml?.Dispose(); } return wp; }