C# WPF UserControls中的数据绑定内存泄漏问题

C# WPF UserControls中的数据绑定内存泄漏问题,c#,wpf,memory-leaks,C#,Wpf,Memory Leaks,我使用的数据绑定方式如下:dependencProperty by it在打开新表单时无法释放内存,而在重新打开同一表单时,表单占用的内存大小会增加 .Xaml文件如下所示 <Border x:Name="brdMain" BorderThickness="10" BorderBrush="{Binding ElementName=UC, Path=BorderColor}" CornerRadius="5"> <Grid x:Name="imgGrid">

我使用的数据绑定方式如下:dependencProperty by it在打开新表单时无法释放内存,而在重新打开同一表单时,表单占用的内存大小会增加

.Xaml文件如下所示

<Border x:Name="brdMain" BorderThickness="10" BorderBrush="{Binding ElementName=UC, Path=BorderColor}" CornerRadius="5">
        <Grid x:Name="imgGrid">
            <Image x:Name="imgMain" Source="{Binding ElementName=UC, Path=ImgMain}" Stretch="{Binding ElementName=UC, Path=ImgStretch}"></Image>
            <InkCanvas EditingMode="Select" Background="#00000000" x:Name="inkCanvas" Width="{Binding ElementName=UC,Path=imgGrid}" Visibility="{Binding ElementName=UC, Path=InkCanvasVisibility}">
                <Image Name="imgVirtualProp" Stretch="Fill" Width="150" Height="100" Margin="5" HorizontalAlignment="Center" VerticalAlignment="Center"></Image>
            </InkCanvas>
        </Grid>
    </Border>

    <StackPanel Grid.Row="1">
        <Image x:Name="imgShadow" Grid.Row="1" Stretch="Fill" Source="/PhotoBoothAgile;component/Images/Assets/Shadow.png" Width="{Binding ElementName=imgMain, Path=Width}"></Image>
        <Label x:Name="labelText" Content="{Binding ElementName=UC, Path=LabelText}" HorizontalAlignment="Center" Foreground="Wheat"></Label>
    </StackPanel>

在.NET中,当不再引用对象时,不会立即释放内存。垃圾收集器通常在稍后的时间点释放未使用的内存。因此,卸载的UC_中的代码不会立即释放内存。对于测试,手动调用垃圾收集器以查看是否确实存在内存泄漏。通过GC.Collect,在释放内存时未发现任何更改。对于测试,应使用GC.Collect;GC.WaitForPendingFinalizers;收集;否则,将不会调用终结器。如果这不改变内存消耗:泄漏的.NET对象的数据类型是什么?您可以使用内存分析工具或WinDbg来判断问题。@fmunkert该问题已由GC.Collect解决;GC.WaitForPendingFinalizers;收集;谢谢但我不明白为什么要打电话给GC。强制收款。恐怕你误解了我的意思。调用GC只是为了测试;您永远不应该在程序的发布版本中这样做。如果通过调用GC内存泄漏消失,那么这意味着首先没有内存泄漏。请记住,.NET延迟释放内存,直到绝对必要时才释放;因此,.NET程序的内存消耗通常比同类本机程序大。但一旦操作系统需要更多内存,.NETGC将确保释放未使用的内存。
public ImageWithCanvasUC()
    {
        InitializeComponent();
    }

    public ImageSource ImgMain
    {
        get { return (ImageSource)GetValue(ImageProperty); }
        set { SetValue(ImageProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Image.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty ImageProperty =
        DependencyProperty.Register("ImgMain", typeof(ImageSource), typeof(ImageWithCanvasUC), new UIPropertyMetadata(null));

    public string LabelText
    {
        get { return (string)GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Text.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty TextProperty =
        DependencyProperty.Register("LabelText", typeof(string), typeof(ImageWithCanvasUC), new UIPropertyMetadata(""));

    //ImgStretch


    public static readonly DependencyProperty ImgStreacthProperty =
        DependencyProperty.Register("ImgStretch", typeof(Stretch), typeof(ImageWithCanvasUC), new UIPropertyMetadata(Stretch.Fill));

    public Stretch ImgStretch
    {
        get { return (Stretch)GetValue(ImgStreacthProperty); }
        set { SetValue(ImgStreacthProperty, value); }
    }

    //InkCanvVisibility

    public static readonly DependencyProperty ImgInkCanvasVisibiltyProperty =
        DependencyProperty.Register("InkCanvasVisibility", typeof(Visibility), typeof(ImageWithCanvasUC), new UIPropertyMetadata(Visibility.Hidden));

    public Visibility InkCanvasVisibility
    {
        get { return (Visibility)GetValue(ImgInkCanvasVisibiltyProperty); }
        set { SetValue(ImgInkCanvasVisibiltyProperty, value); }
    }

    public Brush BorderColor
    {
        get { return (Brush)GetValue(BorderColorProperty); }
        set { SetValue(BorderColorProperty, value); }
    }
    public static readonly DependencyProperty BorderColorProperty =
        DependencyProperty.Register("BorderColor", typeof(Brush), typeof(ImageWithCanvasUC), new UIPropertyMetadata(Brushes.BurlyWood));

    private void UC_Unloaded(object sender, RoutedEventArgs e)
    {
        imgGrid.ClearValue(Grid.DataContextProperty);
        imgMain.ClearValue(Image.SourceProperty);
        imgMain.ClearValue(Image.StretchProperty);
        labelText.ClearValue(Label.ContentProperty);
        brdMain.ClearValue(Border.BorderBrushProperty);
        imgVirtualProp.ClearValue(Image.StretchProperty);
        imgVirtualProp.ClearValue(Image.SourceProperty);
        imgShadow.ClearValue(Image.SourceProperty);
        imgShadow.ClearValue(Image.StretchProperty);
        inkCanvas.ClearValue(InkCanvas.DataContextProperty);

        BindingOperations.ClearAllBindings(imgGrid);
        BindingOperations.ClearAllBindings(imgMain);
        BindingOperations.ClearAllBindings(labelText);
        BindingOperations.ClearAllBindings(imgVirtualProp);
        BindingOperations.ClearAllBindings(imgShadow);
        BindingOperations.ClearAllBindings(brdMain);
        BindingOperations.ClearAllBindings(inkCanvas);
    }