none
wpf在xaml定义的控件怎么析构释放内存 RRS feed

  • 问题

  • <UserControl x:Class="FlipCourse.Waiting.ImageWaiting"
                 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" 
                 mc:Ignorable="d" 
                 d:DesignHeight="300" d:DesignWidth="300" 
                 IsVisibleChanged="UserControl_IsVisibleChanged" Loaded="UserControl_Loaded">
        <UserControl.Resources>
            <Storyboard x:Key="Waiting" Name="waiting">
                <DoubleAnimation Storyboard.TargetName="SpinnerRotate" Storyboard.TargetProperty="(RotateTransform.Angle)" 
                                 From="0" To="359" Duration="0:0:02" RepeatBehavior="Forever" />
            </Storyboard>
        </UserControl.Resources>
        
        <Image Name="Image" Source="/FlipCourse;component/Images/ui/loading.png" RenderTransformOrigin="0.5,0.5" Stretch="None">
            <Image.RenderTransform>
                <RotateTransform x:Name="SpinnerRotate" Angle="0" />
            </Image.RenderTransform>
        </Image>
    </UserControl>
    <UserControl x:Class="FlipCourse.Waiting.LoadingWaiting"
                 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:waiting="clr-namespace:FlipCourse.Waiting"
                 mc:Ignorable="d" 
                 d:DesignHeight="300" d:DesignWidth="300">
        <Grid>
            <Border x:Name="WaitingBorder" CornerRadius="5" Background="#A0000000">
                <waiting:ImageWaiting/>
            </Border>
            <Button x:Name="LoadButton" Style="{DynamicResource TransparentButtonStyle}" Content="重新加载" FontSize="20" 
                    Foreground="Black" Visibility="Collapsed" Cursor="Hand" HorizontalAlignment="Center" VerticalAlignment="Center" Click="OnTryLoad"/>
        </Grid>
    </UserControl>
    这是我定义的LoadingWaiting,用来显示页面转圈。在别的控件是这么使用的
    <UserControl x:Class="FlipCourse.Pages.Common.QuestionContent"
                 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:converter="clr-namespace:FlipCourse.Converter"
                 xmlns:xaml="clr-namespace:FlipCourse.PictureLooker"
                 xmlns:waiting="clr-namespace:FlipCourse.Waiting"
                 mc:Ignorable="d" 
                 d:DesignHeight="600" d:DesignWidth="600">
    
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"></RowDefinition>
                <RowDefinition></RowDefinition>
            </Grid.RowDefinitions>
            <Grid Grid.Row="0" x:Name="QuestionTitleGrid" Height="39" Visibility="Collapsed">
                <Border Grid.Row="0" Grid.Column="0" BorderBrush="{DynamicResource CenterLineColor}" 
                        BorderThickness="0 1 0 1" VerticalAlignment="Stretch">
                    <StackPanel Orientation="Horizontal" Margin="8,8,9,9">
                        <Image Source="/FlipCourse;component/Images/ui/Teacher_TestManager_icon_content.png" Width="18" Height="18"></Image>
                        <TextBlock Text="题干" Style="{DynamicResource Level2TextBlockStyle}" Padding="8 0"/>
                    </StackPanel>
                </Border>
            </Grid>
            <Grid Grid.Row="1">
                <ScrollViewer x:Name="ContentScrollViewer" HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto">
                    <Grid Margin="13">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                        </Grid.RowDefinitions>
                        <Grid Grid.Row="0">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition/>
                            </Grid.RowDefinitions>
    
                            <TextBlock x:Name="StemTextBlock" Grid.Row="0" Margin="18" Text="{Binding Stem}" 
                                   TextWrapping="Wrap" Style="{DynamicResource Level3TextBlockStyle}"
                                   Visibility="{Binding TitleType, Converter={StaticResource TitleTypeToVisibilityConverter},ConverterParameter=文本}"/>
                            <Grid x:Name="PictureGrid" Grid.Row="1"
                                  Visibility="{Binding TitleType, Converter={StaticResource TitleTypeToVisibilityConverter}, ConverterParameter=图片}">
                                <xaml:PictureViewer x:Name="PictureViewer" ImageSource="{Binding StemImage.Image}"
                                                VerticalAlignment="Center"
                                                IsClickEnabled="{Binding StemImage.LoadSuccess}"/>
                                <waiting:LoadingWaiting Width="100" Height="100"
                                                    Visibility="{Binding StemImage.Image,Converter={StaticResource BitmapImageToReversalVisibilityConverter}}"/>
                            </Grid>
                        </Grid>
                        <ListBox Grid.Row="1" Background="Transparent" BorderThickness="0" Margin="0 10"
                             ScrollViewer.VerticalScrollBarVisibility="Disabled"
                             ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                             ItemTemplate="{DynamicResource OptionDataTemplate}"
                             ItemContainerStyle="{DynamicResource OptionListBoxItemStyle}"
                             ItemsSource="{Binding OptionTexts}" IsHitTestVisible="False"/>
                    </Grid>
                </ScrollViewer>
            </Grid>
        </Grid>
    </UserControl>
     我跟踪了下,在离开上面这个控件时,LoadingWaiting没有释放。请问该如何释放创建出来的控件对象?谢谢
    
    
    

    • 已编辑 skczjun 2015年8月17日 4:37
    2015年8月16日 6:45

答案

  • Hi skczjun,

    >>"请问该如何释放创建出来的控件对象?"

    你可以对你的UserControl实现IDispose接口,在Dispose方法中手动回收对象。或者可以订阅

    Dispatcher.ShutdownStarted事件,在事件的实现方法中回收对象。

    public partial class UserControl1 : UserControl,IDisposable
        {
            public UserControl1()
            {
                InitializeComponent();
                Dispatcher.ShutdownStarted += Dispatcher_ShutdownStarted;
            }
    
            void Dispatcher_ShutdownStarted(object sender, EventArgs e)
            {
                //TODO:something
            }
    
            public void Dispose()
            {
                //TODO:something
            }
        }
    更多的释放UserControl的信息,你可以看下这篇文章:Dispose of a WPF UserControl

    Best Regards,

    Xavier Eoro


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2015年8月17日 6:33
    版主

全部回复

  • Hi skczjun,

    >>"请问该如何释放创建出来的控件对象?"

    你可以对你的UserControl实现IDispose接口,在Dispose方法中手动回收对象。或者可以订阅

    Dispatcher.ShutdownStarted事件,在事件的实现方法中回收对象。

    public partial class UserControl1 : UserControl,IDisposable
        {
            public UserControl1()
            {
                InitializeComponent();
                Dispatcher.ShutdownStarted += Dispatcher_ShutdownStarted;
            }
    
            void Dispatcher_ShutdownStarted(object sender, EventArgs e)
            {
                //TODO:something
            }
    
            public void Dispose()
            {
                //TODO:something
            }
        }
    更多的释放UserControl的信息,你可以看下这篇文章:Dispose of a WPF UserControl

    Best Regards,

    Xavier Eoro


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2015年8月17日 6:33
    版主
  • Hi skczjun,

    >>"请问该如何释放创建出来的控件对象?"

    你可以对你的UserControl实现IDispose接口,在Dispose方法中手动回收对象。或者可以订阅

    Dispatcher.ShutdownStarted事件,在事件的实现方法中回收对象。

    public partial class UserControl1 : UserControl,IDisposable
        {
            public UserControl1()
            {
                InitializeComponent();
                Dispatcher.ShutdownStarted += Dispatcher_ShutdownStarted;
            }
    
            void Dispatcher_ShutdownStarted(object sender, EventArgs e)
            {
                //TODO:something
            }
    
            public void Dispose()
            {
                //TODO:something
            }
        }
    更多的释放UserControl的信息,你可以看下这篇文章:Dispose of a WPF UserControl

    Best Regards,

    Xavier Eoro


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    我想知道的是比如我有一个自定义控件,它的xaml里包含了几个Image控件。使用var control1 = new UserControl();生成,然后在释放的时候control1 =null;就可以释放了吗

    • 已编辑 skczjun 2015年8月18日 7:01
    2015年8月18日 7:01