示例#1
0
        public static void LoadWindowSize(this Window window, bool restoreWindowState)
        {
            Argument.IsNotNull(() => window);

            var windowName = window.GetType().Name;

            Log.Debug($"Loading window size for '{windowName}'");

            var storageFile = GetWindowStorageFile(window);

            if (!File.Exists(storageFile))
            {
                Log.Debug($"Window size file '{storageFile}' does not exist, cannot restore window size");
                return;
            }

            try
            {
                var sizeText = File.ReadAllText(storageFile);
                if (string.IsNullOrWhiteSpace(sizeText))
                {
                    Log.Warning($"Size text for window is empty, cannot restore window size");
                    return;
                }

                var splitted = sizeText.Split(new[] { SizeSeparator }, StringSplitOptions.RemoveEmptyEntries);
                if (splitted.Length < 2)
                {
                    Log.Warning($"Size text for window could not be splitted correctly, cannot restore window size");
                    return;
                }

                var culture = CultureInfo.InvariantCulture;

                var width  = StringToObjectHelper.ToDouble(splitted[0], culture);
                var height = StringToObjectHelper.ToDouble(splitted[1], culture);

                Log.Debug($"Setting window size for '{windowName}' to '{width} x {height}'");

                window.SetCurrentValue(FrameworkElement.WidthProperty, width);
                window.SetCurrentValue(FrameworkElement.HeightProperty, height);

                if (restoreWindowState && splitted.Length > 2)
                {
                    var windowState = Enum <WindowState> .Parse(splitted[2]);

                    if (windowState != window.WindowState)
                    {
                        Log.Debug($"Restoring window state for '{windowName}' to '{windowState}'");

                        window.SetCurrentValue(Window.WindowStateProperty, windowState);
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Warning(ex, $"Failed to load window size from file '{storageFile}'");
            }
        }
        public override void DeserializeMember(ISerializationContext context, MemberValue memberValue)
        {
            base.DeserializeMember(context, memberValue);

            if (!_serializeUsingParse)
            {
                if (memberValue.Name == "Vector")
                {
                    var vectorString = (string)memberValue.Value;
                    var parsedValues = vectorString.Split(new[] { "|" }, StringSplitOptions.RemoveEmptyEntries);

                    memberValue.Value = new Vector(StringToObjectHelper.ToDouble(parsedValues[0]), StringToObjectHelper.ToDouble(parsedValues[1]),
                                                   StringToObjectHelper.ToDouble(parsedValues[2]));
                }
            }
        }
        public static void LoadWindowSize(this Window window, string tag = null, bool restoreWindowState = false, bool restoreWindowPosition = true)
        {
            Argument.IsNotNull(() => window);

            var windowName = window.GetType().Name;

            Log.Debug($"Loading window size for '{windowName}'");

            var storageFile = GetWindowStorageFile(window, tag);

            if (!File.Exists(storageFile))
            {
                Log.Debug($"Window size file '{storageFile}' does not exist, cannot restore window size");
                return;
            }

            try
            {
                var sizeText = File.ReadAllText(storageFile);
                if (string.IsNullOrWhiteSpace(sizeText))
                {
                    Log.Warning($"Size text for window is empty, cannot restore window size");
                    return;
                }

                var splitted = sizeText.Split(new[] { SizeSeparator }, StringSplitOptions.RemoveEmptyEntries);
                if (splitted.Length < 2)
                {
                    Log.Warning($"Size text for window could not be splitted correctly, cannot restore window size");
                    return;
                }

                var culture = CultureInfo.InvariantCulture;

                var width  = StringToObjectHelper.ToDouble(splitted[0], culture);
                var height = StringToObjectHelper.ToDouble(splitted[1], culture);

                Log.Debug($"Setting window size for '{windowName}' to '{width} x {height}'");

                window.SetCurrentValue(FrameworkElement.WidthProperty, width);
                window.SetCurrentValue(FrameworkElement.HeightProperty, height);

                if (restoreWindowState && splitted.Length > 2)
                {
                    var windowState = Enum <WindowState> .Parse(splitted[2]);

                    if (windowState != window.WindowState)
                    {
                        Log.Debug($"Restoring window state for '{windowName}' to '{windowState}'");

                        window.SetCurrentValue(Window.WindowStateProperty, windowState);
                    }
                }

                if (restoreWindowPosition && splitted.Length > 3)
                {
                    var left = StringToObjectHelper.ToDouble(splitted[3], culture);
                    var top  = StringToObjectHelper.ToDouble(splitted[4], culture);

                    Log.Debug($"Restoring window position for '{windowName}' to '{left} (x) / {top} (y)'");

                    var virtualScreenLeft   = SystemParameters.VirtualScreenLeft;
                    var virtualScreenTop    = SystemParameters.VirtualScreenTop;
                    var virtualScreenWidth  = SystemParameters.VirtualScreenWidth;
                    var virtualScreenHeight = SystemParameters.VirtualScreenHeight;

                    if (left < virtualScreenLeft ||
                        left + width > virtualScreenWidth ||
                        top < virtualScreenTop ||
                        top + height > virtualScreenHeight)
                    {
                        window.CenterWindowToParent();
                        return;
                    }

                    window.SetCurrentValue(Window.LeftProperty, left);
                    window.SetCurrentValue(Window.TopProperty, top);
                }
            }
            catch (Exception ex)
            {
                Log.Warning(ex, $"Failed to load window size from file '{storageFile}'");
            }
        }