首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Windows 7(芒果)内存泄漏与ControlTemplate

Windows 7(芒果)内存泄漏与ControlTemplate
EN

Stack Overflow用户
提问于 2011-08-03 18:48:50
回答 1查看 763关注 0票数 2

我的WP7应用程序在使用ControlTemplate时出现了托管内存泄漏。

这可以在使用附加代码的普通WP7.1项目中再现。

请参阅下面的“取消评论以查看问题”.

只需在测试应用程序中向前和向后导航。我希望Page1.xaml是垃圾收集的,但是当定义了一个ControlTemplate时,它就不是.

没有ControlTemplate的输出:

外入外

使用ControlTemplate输出:

在里面

在这两种情况下,当applciation退出时,您将看到其余的调试"Out“语句打印。

有人知道为什么“ControlTemplate”的定义会导致页面留在内存中吗?

非常感谢你的帮助,

琼恩

MainPage.xaml

代码语言:javascript
复制
<phone:PhoneApplicationPage 
    x:Class="PhoneApp1.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
    SupportedOrientations="Portrait" Orientation="Portrait"
    shell:SystemTray.IsVisible="True">

    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <!--ContentPanel - place additional content here-->
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">            
        <Button Click="Button_Click">
        <TextBlock Text="Click me..." />
        </Button>          
    </Grid>
    </Grid>

</phone:PhoneApplicationPage>

MainPage.xaml.cs

代码语言:javascript
复制
using System;
using System.Windows;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
namespace PhoneApp1
{
    public partial class MainPage : PhoneApplicationPage
    {
    // Constructor
    public MainPage()
    {
        InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        NavigationService.Navigate(new Uri("/Page1.xaml", UriKind.RelativeOrAbsolute));
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);
        GC.Collect();
    }
    }
}

Page1.xaml

代码语言:javascript
复制
<phone:PhoneApplicationPage 
    x:Class="PhoneApp1.Page1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    shell:SystemTray.IsVisible="True" mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480">

    <phone:PhoneApplicationPage.Resources>
    <Style x:Key="CheckBoxButtonStyle" TargetType="CheckBox">
        <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="CheckBox">
            <!-- Uncomment the following to see the problem -->
            <!--<Grid Background="Transparent"></Grid>-->
            </ControlTemplate>
        </Setter.Value>
        </Setter>
    </Style>
    </phone:PhoneApplicationPage.Resources>

    <Grid x:Name="LayoutRoot" Background="Transparent"></Grid>
</phone:PhoneApplicationPage>

Page1.xaml.cs

代码语言:javascript
复制
using System.Diagnostics;
using Microsoft.Phone.Controls;

namespace PhoneApp1
{
    public partial class Page1 : PhoneApplicationPage
    {
    public Page1()
    {
        InitializeComponent();
        Debug.WriteLine("In");
    }

    ~Page1()
    {
        Debug.WriteLine("Out");
    }
    }
}
EN

回答 1

Stack Overflow用户

发布于 2011-08-03 20:09:41

这似乎是一个相当常见的内存泄漏,特别是因为标准的WP7应用程序(非XNA应用程序)与相同的基本Silverlight类一起工作。有一个完整的银灯论坛上的帖子专门针对这个问题。

最初,解决方案是使用内联模板,但泄漏仍然存在于我的测试用例中。如果将模板移动到App.xaml,泄漏就不再存在了。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6931631

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档