téléphone Windows 8 :Le contrôle utilisateur ne peut pas être supprimé lorsqu'il a été supprimé de son panneau parent ?

StackOverflow https://stackoverflow.com//questions/23002391

Question

J'ai une simple page d'application avec un panneau de contenu et j'ajoute un contrôle utilisateur dans le panneau de contenu.Lorsque je clique sur un bouton pour le supprimer, le destructeur de userControl ne s'est pas exécuté.Pourquoi?

Voici ma page principale :

<Grid x:Name="LayoutRoot" Background="Transparent">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Button Grid.Row="0" Content="AddOrRemove" Click="Button_Click"/>
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">

    </Grid>
</Grid>

l'événement de clic sur le bouton est :

private void Button_Click(object sender, RoutedEventArgs e)
    {
        if (this.ContentPanel.Children.Count > 0)
        {
            this.ContentPanel.Children.Clear();
            return;
        }

        page = new PromptPage();
        this.ContentPanel.Children.Add(page);
    }

PromptPage.xaml :

<Grid x:Name="LayoutRoot" Background="{StaticResource PhoneChromeBrush}">
    <StackPanel>
        <TextBlock Text="balabalaabcdefghijklmnop1"></TextBlock>
        <TextBlock Text="balabalaabcdefghijklmnop2"></TextBlock>
        <TextBlock Text="balabalaabcdefghijklmnop3"></TextBlock>
        <TextBlock Text="balabalaabcdefghijklmnop4"></TextBlock>
        <TextBlock Text="balabalaabcdefghijklmnop5"></TextBlock>
        <TextBlock Text="balabalaabcdefghijklmnop6"></TextBlock>
        <TextBlock Text="balabalaabcdefghijklmnop7"></TextBlock>
        <TextBlock Text="balabalaabcdefghijklmnop8"></TextBlock>
        <TextBlock Text="balabalaabcdefghijklmnop9"></TextBlock>
        <TextBlock Text="balabalaabcdefghijklmnop10"></TextBlock>
        <TextBlock Text="balabalaabcdefghijklmnop11"></TextBlock>
        <TextBlock Text="balabalaabcdefghijklmnop12"></TextBlock>
        <TextBlock Text="balabalaabcdefghijklmnop13"></TextBlock>
    </StackPanel>
</Grid>

PromptPage.xaml.cs :

    public PromptPage()
    {
        InitializeComponent();
    }


    ~PromptPage()
    {
        System.Diagnostics.Debug.WriteLine("disposed!");
    }
Était-ce utile?

La solution

D'après votre code, je peux bien sûr dire qu'il ne sera pas supprimé car votre MainPage détient toujours la référence à page.Essayez de le définir sur null après la suppression :

page = null;

Mais il ne suffit pas de supprimer immédiatement le contrôle.Vous devez attendre le processus GC ou l'appeler directement plus tard :

GC.Collect();
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top