usercontrol wpf 靜態內存 (usercontrol wpf static memory)


問題描述

usercontrol wpf 靜態內存 (usercontrol wpf static memory)

I'm new to WPF and coming from a C++ background so maybe I'm worry about memory management too much here.

Anyways, I've got a UserControl (NewContact) that has a grid with 2 columns, upper column displays 3 radio buttons and depending on which is selected it loads the appropriate UserControl into the lower section of the grid.

private void newMilitaryContactRadioButton_Checked(object sender, RoutedEventArgs e)
    {
        UserControl NMC = new NewMilitaryContact();
        NewContactWindowGridDisplay.Children.Insert(1, NMC);
    }

    private void newMilitaryContactRadioButton_Unchecked(object sender, RoutedEventArgs e)
    {
        NewContactWindowGridDisplay.Children.RemoveAt(1);
    }

    private void newLegalContactRadioButton_Checked(object sender, RoutedEventArgs e)
    {
        UserControl NLC = new NewLegalContact();
        NewContactWindowGridDisplay.Children.Insert(1, NLC);
    }

    private void newLegalContactRadioButton_Unchecked(object sender, RoutedEventArgs e)
    {
        NewContactWindowGridDisplay.Children.RemoveAt(1);
    }

    private void newFirmContactRadioButton_Checked(object sender, RoutedEventArgs e)
    {
        UserControl NFC = new NewFirmContact();
        NewContactWindowGridDisplay.Children.Insert(1, NFC);
    }

    private void newFirmContactRadioButton_Unchecked(object sender, RoutedEventArgs e)
    {
        NewContactWindowGridDisplay.Children.RemoveAt(1);
    }

Now my question is whether I should be, and how to, unload the UserControls I create, when a radio button is unchecked.  I did some searching around MSDN documentation and saw that the using the remove method from the parent object would unload the usercontrol.  If that is the case is the code I'm using to in the various "unchecked" methods correct so as not to pile up a ton of NFC/NLC/NMC UserControl objects if someone were to click amongst the three radio buttons over and over and over again?

Much thanks to anyone to who can explain this to me :)

‑‑‑‑‑

參考解法

方法 1:

Actually you need to read more about .Net memory management and know how it works. In your case it depends on what your UserControls are doing? If they are using system resources it will good to dispose their references in UserControl unloaded events, otherwise GC will take care of them.

Read this article : Garbage Collection: Automatic Memory Management in the Microsoft .NET Framework

Also the way you are going is not so good, because soon you will find out you need to do more with your UserControl like setting its DataContextStyles handling events and etc... and this will hard to do with code.

(by user1588896saber)

參考文件

  1. usercontrol wpf static memory (CC BY‑SA 3.0/4.0)

#wpf #user-controls #C#






相關問題

usercontrol wpf 靜態內存 (usercontrol wpf static memory)

將圖像加載到面板 (Load image to panel)

我的 WPF 自定義控件的數據上下文正在取代父級的 (My WPF custom control's Data Context is superseding parent's)

是否可以更改 WPF 控件的父級 (Is it possible to change parent of WPF control)

WPF MVVM 鏈接視圖 (WPF MVVM Linked Views)

wpf 樹視圖 mvvm (wpf treeview mvvm)

應用啟動後更改應用圖標 (Change application icon after app launched)

在VB中過濾一個wpf collectionviewsource? (Filter a wpf collectionviewsource in VB?)

如何使 DockPanel 中的項目擴展以適應 WPF 中的所有可用空間? (How to make items in a DockPanel expand to fit all available space in WPF?)

使用 IsCheckable="True" 綁定 MenuItem 中的鍵時出現問題,為什麼? (Problems binding keys in MenuItem's with IsCheckable="True", why?)

如何將文本框的borderBrush屬性綁定到viewmodel中的屬性,類型轉換錯誤 (How to bind the borderBrush property of a textbox to a property in viewmodel, type conversion error)

Painter 應用程序的自定義控件以顯示不同的橡皮擦 (Custom Control for Painter application to show different erasers)







留言討論