【问题标题】:Dev Express Loading Decorator causing memory leak + run time exceptionDevexpress Loadingdecorator导致内存泄漏+运行时异常
【发布时间】:2018-04-12 11:53:36
【问题描述】:

1) 我在使用 dx:LoadingDecorator 时遇到了一些性能问题,以证明我创建了一个小型示例应用程序。所以问题是每次我显示加载启动屏幕时,都会在可视树中推送一个 LoadingDecorator 的新实例,在内存中分配它并且永远不会被释放。在下一次运行时,将创建一个新实例。这种行为会导致内存泄漏。

xaml 代码是:

<dx:DXWindow x:Class="DXApplication12.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
    xmlns:dxb="http://schemas.devexpress.com/winfx/2008/xaml/bars"
    Title="MainWindow" Height="800" Width="1525">
<Grid>
    <dx:SimpleButton Width="100"
                     Height="20"
                     Content="Command"
                     VerticalAlignment="Top"
                     HorizontalAlignment="Left" 
                     Command="{Binding SimpleButtonCommand}"/>
    <dx:LoadingDecorator
        VerticalAlignment="Center"
        HorizontalAlignment="Center"
        IsSplashScreenShown="{Binding IsBusy}">
        <dx:LoadingDecorator.SplashScreenTemplate>
            <DataTemplate>
                <StackPanel Orientation="Vertical">
                    <TextBlock Text="Loading ..." FontSize="30" Background="Transparent"/>
                </StackPanel>
            </DataTemplate>
        </dx:LoadingDecorator.SplashScreenTemplate>
    </dx:LoadingDecorator>
</Grid>

以及背后的代码:

public partial class MainWindow : DXWindow
{
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = new MainWindowViewModel();
        this.ContentRendered += (s, e) =>
          {
              for (int i = 0; i < 1000; i++)
              {
                  (this.DataContext as MainWindowViewModel).SimpleButtonCommand.Execute(null);
              }
          };
    }
}

public class MainWindowViewModel : ViewModelBase
{

    public ICommand SimpleButtonCommand => new DelegateCommand(() => {
        this.IsBusy = true;
        this.IsBusy = false;
    });

    private bool _isBusy;
    public bool IsBusy
    {
        get => this._isBusy;
        set => this.SetProperty(ref this._isBusy, value, nameof(this.IsBusy));
    }
}

2) 另一个方面是,应用程序随机引发运行时异常: PresentationFramework.dll 中出现“System.InvalidOperationException”类型的未处理异常 此 Visual 未连接到 PresentationSource。

只有在使用 Visual Studio 启动应用程序时才会发生这种情况,如果我禁用 XAML 的 UI 调试工具,问题就会消失,但我认为这是一种解决方法,我想知道根本原因 :)

我还附上了一张照片来描述我遇到的两个问题:https://ibb.co/iJ057H

谢谢。

【问题讨论】:

    标签: c# .net wpf devexpress desktop-application


    【解决方案1】:

    我遇到了这个问题,因为我面临着完全相同的问题。第二个问题(随机 InvalidOperationException)也在 DevExpress 支持论坛(例如 herehere)的多个线程中进行了讨论,但 DevExpress 建议的唯一解决方法是禁用 XAML 调试器。

    我通过在繁忙状态更改时不隐藏/显示 LoadingDecorator 来解决这些问题,而是通过设置 IsSplashScreenShown="True" 并改为更改 LoadingDecorator 内容的可见性来使其始终“可见”。您可以使用SplashScreenDataContext 属性将您的视图模型作为数据上下文传递给装饰器的内容,以访问您的IsBusy 属性。

    这样只有一个装饰器的永久实例,它解决了内存问题。它似乎也修复了随机异常,因为这只发生在加载装饰器时。

    所以在你的情况下,这看起来像这样:

    <Grid>
        <dx:SimpleButton .../>
            <dx:LoadingDecorator
                ...
                IsSplashScreenShown="True"
                SplashScreenDataContext="{Binding}">
                <dx:LoadingDecorator.SplashScreenTemplate>
                    <DataTemplate>
                        <StackPanel Visibility="{Binding IsBusy, Converter={StaticResource BooleanToVisibilityConverter}}" ...>
                            <TextBlock .../>
                        </StackPanel>
                    </DataTemplate>
                </dx:LoadingDecorator.SplashScreenTemplate>
            </dx:LoadingDecorator>
    </Grid>
    

    【讨论】:

      猜你喜欢
      • 2014-03-15
      • 1970-01-01
      • 2012-07-24
      • 1970-01-01
      • 2015-07-06
      • 2014-06-07
      • 2013-11-20
      • 2011-10-28
      • 2016-01-18
      相关资源
      最近更新 更多