如何解决为什么由具有动态绘制形状的单个窗口组成的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 举报,一经查实,本站将立刻删除。