WPF简单的用户控制消耗超过1.4 GB的内存
我开始学习WPF C#,并通过用户控制制作了一个简单的应用程序,但注意到此简单应用程序从内存中消耗了超过1.4 GB。 那是正常的
<UserControl x:Class="newShooting.user_controls.roundButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:newShooting.user_controls"
mc:Ignorable="d" d:DesignWidth="100" Height="230">
<Grid Height="230" >
<Border Background="#FF53A5ED" ></Border>
<StackPanel Background="{Binding bkground}">
<!--<StackPanel.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Black"/>
<GradientStop Color="#FF715757" Offset="1"/>
</LinearGradientBrush>
</StackPanel.Background>-->
<Label HorizontalAlignment="Center" Foreground="AntiqueWhite" >
<Label.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Black"/>
<GradientStop Color="#FF20A3DA" Offset="1"/>
</LinearGradientBrush>
</Label.Background> 1
</Label>
<Image x:Name="imageBody" Height="100" Width="100" Source="/user_controls/png-clipart-shooting-target-graphy-bullet-shooting-range-others-miscellaneous-human.png"/>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="4*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock x:Name="bullets" Grid.Column="1" FontSize="72" FontWeight="Bold" Foreground="#FFDA2020" FontFamily="Arial Rounded MT Bold" HorizontalAlignment="Center" VerticalAlignment="Stretch" FontStretch="UltraExpanded">1</TextBlock>
<Grid Grid.Column="2">
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="2*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Image x:Name="light" Source="/user_controls/Lovepik_com-400229766-light-bulb.png" Grid.Row="0"></Image>
<Ellipse x:Name="connected" Height="15" Width="15" StrokeThickness="5" Fill="#FFF70202" Grid.Row="2" />
</Grid>
</Grid>
<ComboBox x:Name="cb_shooters"></ComboBox>
</StackPanel>
</Grid>
this is the user control and the follwoing will be the main window .<Window x:Class="newShooting.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:newShooting"
xmlns:uc="clr-namespace:newShooting.user_controls"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800" WindowState="Maximized"
Background="Transparent" WindowStyle="None"
>
<Grid >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="2*"></RowDefinition>
<RowDefinition Height="2*"></RowDefinition>
<RowDefinition Height="2*"></RowDefinition>
</Grid.RowDefinitions>
<Border Background="DimGray" Grid.ColumnSpan="10" Grid.RowSpan="3">
</Border>
<Button Content="Button" Grid.Column="1" Grid.Row="2" Click="Button_Click"/>
<StackPanel VerticalAlignment="Center" Orientation="Vertical" Grid.Column="0" Grid.Row="1">
<uc:roundButton x:Name="UC1" VerticalAlignment="Center" bkground="#FF1E598C"></uc:roundButton>
</StackPanel>
<StackPanel VerticalAlignment="Center" Orientation="Vertical" Grid.Column="1" Grid.Row="1">
<uc:roundButton x:Name="UC2" VerticalAlignment="Center"></uc:roundButton>
</StackPanel>
<StackPanel VerticalAlignment="Center" Orientation="Vertical" Grid.Column="2" Grid.Row="1">
<uc:roundButton x:Name="UC3" VerticalAlignment="Center" bkground="#FF1E598C" ></uc:roundButton>
</StackPanel>
<StackPanel VerticalAlignment="Center" Orientation="Vertical" Grid.Column="3" Grid.Row="1">
<uc:roundButton x:Name="UC4" VerticalAlignment="Center"></uc:roundButton>
</StackPanel>
<StackPanel VerticalAlignment="Center" Orientation="Vertical" Grid.Column="4" Grid.Row="1">
<uc:roundButton x:Name="UC5" VerticalAlignment="Center" bkground="#FF1E598C"></uc:roundButton>
</StackPanel>
<StackPanel VerticalAlignment="Center" Orientation="Vertical" Grid.Column="5" Grid.Row="1">
<uc:roundButton x:Name="UC6" VerticalAlignment="Center"></uc:roundButton>
</StackPanel>
<StackPanel VerticalAlignment="Center" Orientation="Vertical" Grid.Column="6" Grid.Row="1">
<uc:roundButton x:Name="UC7" VerticalAlignment="Center" bkground="#FF1E598C"></uc:roundButton>
</StackPanel>
<StackPanel VerticalAlignment="Center" Orientation="Vertical" Grid.Column="7" Grid.Row="1">
<uc:roundButton x:Name="UC8" VerticalAlignment="Center"></uc:roundButton>
</StackPanel>
<StackPanel VerticalAlignment="Center" Orientation="Vertical" Grid.Column="8" Grid.Row="1">
<uc:roundButton x:Name="UC9" VerticalAlignment="Center" bkground="#FF1E598C"></uc:roundButton>
</StackPanel>
<StackPanel VerticalAlignment="Center" Orientation="Vertical" Grid.Column="9" Grid.Row="1">
<uc:roundButton x:Name="UC10" VerticalAlignment="Center"></uc:roundButton>
</StackPanel>
</Grid>
,那么内存的用法是:
是正常的,是否有一种减少内存消耗的方法? 笔记: 我尝试添加gc.collect(),但不起作用。 我应该怎么办 。
i 'm starting to learn wpf c# and made a simple application with user control but noticed that this simple application consumes more than 1.4 GB from memory .
is that normal
<UserControl x:Class="newShooting.user_controls.roundButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:newShooting.user_controls"
mc:Ignorable="d" d:DesignWidth="100" Height="230">
<Grid Height="230" >
<Border Background="#FF53A5ED" ></Border>
<StackPanel Background="{Binding bkground}">
<!--<StackPanel.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Black"/>
<GradientStop Color="#FF715757" Offset="1"/>
</LinearGradientBrush>
</StackPanel.Background>-->
<Label HorizontalAlignment="Center" Foreground="AntiqueWhite" >
<Label.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Black"/>
<GradientStop Color="#FF20A3DA" Offset="1"/>
</LinearGradientBrush>
</Label.Background> 1
</Label>
<Image x:Name="imageBody" Height="100" Width="100" Source="/user_controls/png-clipart-shooting-target-graphy-bullet-shooting-range-others-miscellaneous-human.png"/>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="4*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock x:Name="bullets" Grid.Column="1" FontSize="72" FontWeight="Bold" Foreground="#FFDA2020" FontFamily="Arial Rounded MT Bold" HorizontalAlignment="Center" VerticalAlignment="Stretch" FontStretch="UltraExpanded">1</TextBlock>
<Grid Grid.Column="2">
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="2*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Image x:Name="light" Source="/user_controls/Lovepik_com-400229766-light-bulb.png" Grid.Row="0"></Image>
<Ellipse x:Name="connected" Height="15" Width="15" StrokeThickness="5" Fill="#FFF70202" Grid.Row="2" />
</Grid>
</Grid>
<ComboBox x:Name="cb_shooters"></ComboBox>
</StackPanel>
</Grid>
this is the user control and the follwoing will be the main window .
<Window x:Class="newShooting.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:newShooting"
xmlns:uc="clr-namespace:newShooting.user_controls"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800" WindowState="Maximized"
Background="Transparent" WindowStyle="None"
>
<Grid >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="2*"></RowDefinition>
<RowDefinition Height="2*"></RowDefinition>
<RowDefinition Height="2*"></RowDefinition>
</Grid.RowDefinitions>
<Border Background="DimGray" Grid.ColumnSpan="10" Grid.RowSpan="3">
</Border>
<Button Content="Button" Grid.Column="1" Grid.Row="2" Click="Button_Click"/>
<StackPanel VerticalAlignment="Center" Orientation="Vertical" Grid.Column="0" Grid.Row="1">
<uc:roundButton x:Name="UC1" VerticalAlignment="Center" bkground="#FF1E598C"></uc:roundButton>
</StackPanel>
<StackPanel VerticalAlignment="Center" Orientation="Vertical" Grid.Column="1" Grid.Row="1">
<uc:roundButton x:Name="UC2" VerticalAlignment="Center"></uc:roundButton>
</StackPanel>
<StackPanel VerticalAlignment="Center" Orientation="Vertical" Grid.Column="2" Grid.Row="1">
<uc:roundButton x:Name="UC3" VerticalAlignment="Center" bkground="#FF1E598C" ></uc:roundButton>
</StackPanel>
<StackPanel VerticalAlignment="Center" Orientation="Vertical" Grid.Column="3" Grid.Row="1">
<uc:roundButton x:Name="UC4" VerticalAlignment="Center"></uc:roundButton>
</StackPanel>
<StackPanel VerticalAlignment="Center" Orientation="Vertical" Grid.Column="4" Grid.Row="1">
<uc:roundButton x:Name="UC5" VerticalAlignment="Center" bkground="#FF1E598C"></uc:roundButton>
</StackPanel>
<StackPanel VerticalAlignment="Center" Orientation="Vertical" Grid.Column="5" Grid.Row="1">
<uc:roundButton x:Name="UC6" VerticalAlignment="Center"></uc:roundButton>
</StackPanel>
<StackPanel VerticalAlignment="Center" Orientation="Vertical" Grid.Column="6" Grid.Row="1">
<uc:roundButton x:Name="UC7" VerticalAlignment="Center" bkground="#FF1E598C"></uc:roundButton>
</StackPanel>
<StackPanel VerticalAlignment="Center" Orientation="Vertical" Grid.Column="7" Grid.Row="1">
<uc:roundButton x:Name="UC8" VerticalAlignment="Center"></uc:roundButton>
</StackPanel>
<StackPanel VerticalAlignment="Center" Orientation="Vertical" Grid.Column="8" Grid.Row="1">
<uc:roundButton x:Name="UC9" VerticalAlignment="Center" bkground="#FF1E598C"></uc:roundButton>
</StackPanel>
<StackPanel VerticalAlignment="Center" Orientation="Vertical" Grid.Column="9" Grid.Row="1">
<uc:roundButton x:Name="UC10" VerticalAlignment="Center"></uc:roundButton>
</StackPanel>
</Grid>
then the memory usage is :
is that normal and are there a way to reduce the memory consumption ?
Note:
i tried to add GC.collect() but doesn't work .
what should i do .
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论