为什么由具有动态绘制形状的单个窗口组成的WPF应用程序会占用300 mb的内存?

问题描述

我正在开发一个用于整理桌面的应用程序。我希望能够选择一个窗口(给定一些特殊的键组合,例如win + shift +鼠标单击等),然后将窗口拖动到自定义的正方形,然后应用程序会将窗口移到该正方形的边界。

所有这些都很好-但我还需要在屏幕上绘制自定义区域。我现在一直在使用p / invoke并将调试报告吐出到控制台-应用程序本身+控制台调试需要几百千字节的内存,没什么大不了的。我以为我会尝试使用WPF渲染区域,因为它可以很好地处理透明度,并且我认为透明度会很好-除此之外,我可以使用canvas对象在矩形上轻松绘制矩形。

不幸的是,当我放下WPF视觉效果时,它从〜700k变为〜300,000k,这并不理想。对于这样一个简单的WPF应用程序,这种用法正常吗?忽略应用程序代码,仅关注下面的WPF代码,我应该做些限制内存消耗的事情吗?我猜想我在WPFGrid构造函数中做错了什么,但不知道它可能是什么。

using System.Collections.Generic;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
using GridRenderer;

namespace WPFGridRenderer
{
    public class MainWindow : Window
    {

    }

    public class WPFGrid : IGridRender
    {
        private Application WinApp;
        private Window MainWindow;
        private Canvas Canvas;
        private Brush ActiveBrush;
        private Brush InactiveBrush;

        public Dictionary<RenderedGrid,Rectangle> Rectangles = new Dictionary<RenderedGrid,Rectangle>();

        public WPFGrid(int width,int height,IEnumerable<RenderedGrid> grids)
        {
            var t = new Thread(() => {
                //Allow application to bootup
                //TODO: Replace with semaphore or other time agnostic thread guarding mechanism
                Thread.Sleep(1000);


                //TODO: Allow for custom configuration via config file? 
                ActiveBrush = new SolidColorBrush(Color.FromArgb(195,255,255));
                InactiveBrush = new SolidColorBrush(Color.FromArgb(125,0));

                WinApp = new Application();
                MainWindow = new MainWindow();
                
                //Generate Window
                MainWindow.WindowStyle = WindowStyle.None;
                MainWindow.AllowsTransparency = true;
                MainWindow.Background = Brushes.Transparent;
                MainWindow.Topmost = true;
                MainWindow.Left = 0;
                MainWindow.Top = 0;
                //Can be pretty big,the size of n monitors (For my testing this is 3600x1200)
                MainWindow.Width = ScreenInfo.GetDisplays().MaxWidth;
                MainWindow.Height = ScreenInfo.GetDisplays().MaxHeight;
                MainWindow.Content = (Canvas = new Canvas());

                updateGrids(grids);
                WinApp.Run(MainWindow); // note: blocking call
            });
            t.SetApartmentState(ApartmentState.STA);
            t.Start();

        }

        public void HideGrid()
        {
            WinApp.Dispatcher.Invoke(() =>
            {
                MainWindow.Background = Brushes.Transparent;
                MainWindow.Hide();
            });
        }

        public void RenderGrid()
        {
            WinApp?.Dispatcher.Invoke(() =>
            {
                MainWindow.Background = InactiveBrush;
                MainWindow.Show();
            });
        }

        public void ActivateSector(RenderedGrid grid)
        {
            WinApp?.Dispatcher.Invoke(() =>
            {
                foreach (var rect in Rectangles)
                {
                    rect.Value.Fill = rect.Key == grid ?
                        ActiveBrush :
                        Brushes.Transparent;
                }
            });
        }

        public void UpdateGrids(IEnumerable<RenderedGrid> grids)
        {
            WinApp.Dispatcher.Invoke(() =>
            {
                updateGrids(grids);
            });
        }

        private void updateGrids(IEnumerable<RenderedGrid> grids)
        {
            Canvas.Children.Clear();
            Rectangles.Clear();
            foreach (var grid in grids)
            {
                var rect = new System.Windows.Shapes.Rectangle();
                rect.StrokeThickness = 2;
                rect.Stroke = Brushes.Black;
                Canvas.SetLeft(rect,grid.TargetLeft);
                Canvas.SetTop(rect,grid.TargetTop);
                rect.Width = grid.TargetRight;
                rect.Height = grid.TargetBottom;
                Canvas.Children.Add(rect);
                Rectangles[grid] = rect;
            }
        }
    }
}```

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...