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

时间:2014-07-03 05:23:31

标签: c# wpf memory-leaks

我正在使用数据绑定int以及 DependencyProperty ,因为它在打开新表单时无法释放内存,并且在重新打开相同的表单时会占用内存大小通过形式增加。

.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>

.CS 文件属性为

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);
    }

0 个答案:

没有答案