在切换TabItem时释放内存

时间:2018-11-24 07:40:33

标签: c# wpf

我在一个带有Frame的窗口中有很多TabItem,并在单击TabItem时在Frame中加载UserControls,如下图所示: enter image description here

问题:

  1. 单击第一个TabItem并加载“销售UserControl”时,它将占用内存 121Mb

  2. 切换第二个TabItem时需要 128Mb

  3. 然后再次切换回第一个TabItem,它将占用 142Mb 内存。其他TabItem的情况相同,每个开关都占用内存,而不释放最后一个TabItem。

  4. 我切换了两个TabItem 10次,内存消耗似乎高达 308MB ,这令人震惊。

我认为这种增加的内存是针对内存泄漏的。但我不知道从哪里来。请帮助

诊断工具结果:

enter image description here

代码:

<Window
        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:Inventory_Control"
        xmlns:view="clr-namespace:Inventory_Control.UserControls"
        x:Class="Inventory_Control.MainWindow"
        mc:Ignorable="d"
        Title="MainWindow" WindowState="Maximized" WindowStartupLocation="CenterScreen" Icon="Images/app_icon.png">
    <Grid>

        <Grid.Resources>
            <Style TargetType="{x:Type TabItem}">
                <Setter Property="Margin" Value="5"/>
            </Style>
            <Style TargetType="{x:Type StackPanel}">
                <Setter Property="Orientation" Value="Vertical"/>
                <Setter Property="MinWidth" Value="160"/>
                <Setter Property="MinHeight" Value="90"/>
            </Style>
            <Style TargetType="{x:Type Image}">
                <Setter Property="Margin" Value="10,10,5,0"/>
                <Setter Property="Width" Value="40"/>
                <Setter Property="Height" Value="40"/>
                <Setter Property="HorizontalAlignment" Value="Left"/>
            </Style>
            <Style TargetType="{x:Type TextBlock}">
                <Setter Property="VerticalAlignment" Value="Center"/>
                <Setter Property="Margin" Value="10,3,0,5"/>
                <Setter Property="Foreground" Value="White"/>
                <Setter Property="FontSize" Value="18"/>
            </Style>
        </Grid.Resources>

        <Grid.RowDefinitions>
            <RowDefinition Height="0.079*" />
            <RowDefinition Height="1*" />
        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1*" />
            <!--<ColumnDefinition Width="0.2*" />-->
        </Grid.ColumnDefinitions>

        <Viewbox HorizontalAlignment="Left">
            <TabControl x:Name="TabControl" Padding="0" MouseUp="TabControl_MouseUp">

                <TabItem MouseUp="TabItem_MouseUp">
                    <TabItem.Header>
                        <StackPanel Orientation="Vertical" Background="#3e7038">
                            <Image Source="Images/sale.png"/>
                            <TextBlock Text="Sale" />
                        </StackPanel>
                    </TabItem.Header>
                </TabItem>

                <TabItem x:Name="ItemsTab" MouseUp="ItemsTab_MouseUp">
                    <TabItem.Header>
                        <StackPanel Background="#0073c4">
                            <Image Source="Images/product.png"/>
                            <TextBlock Text="Items"  />
                        </StackPanel>
                    </TabItem.Header>
                </TabItem>
                <TabItem x:Name="CategoryTab" MouseUp="CategoryTab_MouseUp">
                    <TabItem.Header>
                        <StackPanel  Background="#404040">
                            <Image Source="Images/category.png" />
                            <TextBlock Text="Category"  />
                        </StackPanel>
                    </TabItem.Header>
                </TabItem>
                <TabItem x:Name="TypeTab" MouseUp="TypeTab_MouseUp">
                    <TabItem.Header>
                        <StackPanel  Background="#404040">
                            <Image Source="Images/type.png" />
                            <TextBlock Text="Type"  />
                        </StackPanel>
                    </TabItem.Header>
                </TabItem>
                <TabItem x:Name="LocationTab" MouseUp="LocationTab_MouseUp">
                    <TabItem.Header>
                        <StackPanel  Background="#404040">
                            <Image Source="Images/location.png" />
                            <TextBlock Text="Location"  />
                        </StackPanel>
                    </TabItem.Header>
                </TabItem>
                <TabItem x:Name="CustomerTab" MouseUp="CustomerTab_MouseUp">
                    <TabItem.Header>
                        <StackPanel  Background="#FF364834">
                            <Image Source="Images/customer.png"/>
                            <TextBlock Text="Customer"  />
                        </StackPanel>
                    </TabItem.Header>
                </TabItem>
                <TabItem x:Name="SupplierTab" MouseUp="SupplierTab_MouseUp">
                    <TabItem.Header>
                        <StackPanel  Background="#FF364834">
                            <Image Source="Images/supplier.png"/>
                            <TextBlock Text="Supplier"  />
                        </StackPanel>
                    </TabItem.Header>
                </TabItem>
                <TabItem Name="LedgerTab">
                    <TabItem.Header>
                        <StackPanel  Background="#FF7F7C7C">
                            <Image Source="Images/ledger.png" />
                            <TextBlock Text="Ledger"  />
                        </StackPanel>
                    </TabItem.Header>
                </TabItem>


                <TabItem x:Name="UserTab" MouseUp="UserTab_MouseUp">
                    <TabItem.Header>
                        <StackPanel  Background="#FFB17369">
                            <Image Source="Images/user.png" />
                            <TextBlock Text="User" />
                        </StackPanel>
                    </TabItem.Header>
                </TabItem>
                <TabItem x:Name="ExpenseTab" MouseUp="AddExpenses_MouseUp">
                    <TabItem.Header>
                        <StackPanel  Background="#cc6d00">
                            <Image Source="Images/expense.png" />
                            <TextBlock Text="Expense" />
                        </StackPanel>
                    </TabItem.Header>
                </TabItem>
                <TabItem Name="ReportTab" MouseUp="TabItem_MouseUp_2">
                    <TabItem.Header>
                        <StackPanel  Background="#00879c">
                            <Image Source="Images/report.png" />
                            <TextBlock Text="Report"  />
                        </StackPanel>
                    </TabItem.Header>
                </TabItem>

                <TabItem x:Name="HistoryTab" MouseUp="HistoryTab_MouseUp">
                    <TabItem.Header>
                        <StackPanel  Background="#FF324F54">
                            <Image Source="Images/history.png" />
                            <TextBlock Text="History"  />
                        </StackPanel>
                    </TabItem.Header>
                </TabItem>

                <TabItem x:Name="InventoryTrailTab" MouseUp="InventoryTrailTab_MouseUp">
                    <TabItem.Header>
                        <StackPanel  Background="Tomato">
                            <Image Source="Images/history.png" />
                            <TextBlock Text="Inventory Trail"  />
                        </StackPanel>
                    </TabItem.Header>
                </TabItem>

            </TabControl>

        </Viewbox> 

        <!--<Button Name="LogoutBtn" Height="auto" MinWidth="50" Width="50" Content="Logout" Grid.Column="1"/>-->

        <Frame Grid.Row="1" Grid.ColumnSpan="2" x:Name="mainFrame" NavigationUIVisibility="Hidden"/>
    </Grid>


</Window>

cs:

using Inventory_Control.Classes;

namespace Inventory_Control
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        int selectedTab = 0;

        string userType = Util.GetUserType();

        public MainWindow()
        {
            InitializeComponent();
            mainFrame.Content = new FileTab();


            if (userType.Equals(Util.editor))
            {
                EditorPrivilidges();
            }

            if (userType.Equals(Util.salesman))
            {
                SalesmanPrevilidges();
            }
        }

        void SalesmanPrevilidges()
        {
            EditorPrivilidges();
            ItemsTab.Visibility = Visibility.Collapsed;
            CategoryTab.Visibility = Visibility.Collapsed;
            TypeTab.Visibility = Visibility.Collapsed;
            LocationTab.Visibility = Visibility.Collapsed;
            CustomerTab.Visibility = Visibility.Collapsed;
            SupplierTab.Visibility = Visibility.Collapsed;

        }

        void EditorPrivilidges()
        {
            ReportTab.Visibility = Visibility.Collapsed;
            HistoryTab.Visibility = Visibility.Collapsed;
            UserTab.Visibility = Visibility.Collapsed;
            ExpenseTab.Visibility = Visibility.Collapsed;
            LedgerTab.Visibility = Visibility.Collapsed;
        }

        private void TabItem_MouseUp(object sender, MouseButtonEventArgs e)
        {
            if (selectedTab != 0)
            {
                mainFrame.Content = new FileTab();
            }
        }

        private void SaleTab_MouseUp(object sender, MouseButtonEventArgs e)
        {

        }

        private void ItemsTab_MouseUp(object sender, MouseButtonEventArgs e)
        {
            if (selectedTab != 1)
            {
                mainFrame.Content = new ItemsTab();
            }
        }

        private void TabItem_MouseUp_1(object sender, MouseButtonEventArgs e)
        {

        }

        private void CategoryTab_MouseUp(object sender, MouseButtonEventArgs e)
        {
            if (selectedTab != 2)
            {
                mainFrame.Content = null;
                mainFrame.Content = new CategoryTab();
            }
        }

        private void TypeTab_MouseUp(object sender, MouseButtonEventArgs e)
        {
            if (selectedTab != 3)
            {
                mainFrame.Content = new TypeTab();
            }
        }

        private void LocationTab_MouseUp(object sender, MouseButtonEventArgs e)
        {
            if (selectedTab != 4)
            {
                mainFrame.Content = new LocationTab();
            }
        }

        private void CustomerTab_MouseUp(object sender, MouseButtonEventArgs e)
        {
            if (selectedTab != 5)
            {
                mainFrame.Content = new CustomerTab();
            }
        }

        private void SupplierTab_MouseUp(object sender, MouseButtonEventArgs e)
        {
            if (selectedTab != 6)
            {
                mainFrame.Content = new SupplierTab();
            }
        }

        private void UserTab_MouseUp(object sender, MouseButtonEventArgs e)
        {
            if (selectedTab != 8)
            {
                mainFrame.Content = new UserTab();
            }
        }

        private void TabItem_MouseUp_2(object sender, MouseButtonEventArgs e)
        {
            if (selectedTab != 10)
            {
                mainFrame.Content = new ReportTab();
            }
        }

        private void HistoryTab_MouseUp(object sender, MouseButtonEventArgs e)
        {
            if (selectedTab != 11)
            {
                mainFrame.Content = new HistoryTab();
            }
        }

        private void InventoryTrailTab_MouseUp(object sender, MouseButtonEventArgs e)
        {
            if (selectedTab != 12)
            {
                mainFrame.Content = new InventoryTrail();
            }
        }

        private void SalesmanTab_MouseUp(object sender, MouseButtonEventArgs e)
        {
            if (mainFrame.Content != new SalesmanTab())
            {
                mainFrame.Content = new SalesmanTab();
            }
        }



        private void AddExpenses_MouseUp(object sender, MouseButtonEventArgs e)
        {
            if (mainFrame.Content != new AddExpenses())
            {
                mainFrame.Content = new AddExpenses();
            }
        }

        private void TabControl_MouseUp(object sender, MouseButtonEventArgs e)
        {
            selectedTab = TabControl.SelectedIndex;
        }
    }
}

0 个答案:

没有答案