C# ListBox中的MediaElement导致内存泄漏

C# ListBox中的MediaElement导致内存泄漏,c#,wpf,memory-leaks,C#,Wpf,Memory Leaks,我有一个虚拟化的列表框,里面有很多从HDD加载的GIF,它们在一个循环中播放 我正在使用网格,因为我计划添加更多的控件,所以请坚持使用它 LongFileName是一个完整路径 public class cThumbnail3 : System.Windows.Controls.Grid { public string LongFileName { get { return (string)GetValue(LongFileNameProperty); }

我有一个虚拟化的列表框,里面有很多从HDD加载的GIF,它们在一个循环中播放

我正在使用网格,因为我计划添加更多的控件,所以请坚持使用它

LongFileName是一个完整路径

public class cThumbnail3 : System.Windows.Controls.Grid
{
   public string LongFileName
    {
        get { return (string)GetValue(LongFileNameProperty); }
        set { SetValue(LongFileNameProperty, value); }
    }
    public static readonly DependencyProperty LongFileNameProperty =
        DependencyProperty.Register("LongFileName", typeof(string), typeof(cThumbnail3), new PropertyMetadata(OnLongFileNameChanged));


    static void OnLongFileNameChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
    {
        cThumbnail3 t = obj as cThumbnail3;
        t.LoadAsGif();
    }

    private MediaElement ME;
    public void LoadAsGif()
    {
        ME = new MediaElement();
        ME.UnloadedBehavior = MediaState.Manual;
        Uri uri = new Uri(@"file://" + LongFileName);
        ME.Source = uri;
        ME.MediaEnded += (o, e) =>
            {
                ME.Position = new TimeSpan(0, 0, 1);
                ME.Play();
            };
        this.Children.Clear();
        this.Children.Add(ME);
    }
}
xaml现在很简单

<local:cThumbnail3 LongFileName="{Binding FullPath}" />

您可以尝试为
MediaEnded
事件编写完整的方法,并在类的析构函数中为事件编写
-=
。我猜这件事是罪魁祸首,它可能会导致物品无法得到妥善处理


您还可以为列表框设置
virtualizengstackpanel.isvirtualization=“true”
,以提高性能。请在ME.UnloadedBehavior=MediaState上查找有关它的详细信息。手动操作看起来可疑。文档对此有何说明?这是控制所必需的,否则我会得到
附加信息:除非LoadedBehavior或UnloadedBehavior设置为手动,否则无法控制媒体。
不清楚您在做什么。我建议你把一个非常简单的测试项目放在一起,它可以重现你目前遇到的问题。我试过了,请看编辑。IsVirtualization已设置为True
private bool IsVisible { get; set; }

public void LoadAsGif()
{
   ME = new MediaElement();
   ME.UnloadedBehavior = MediaState.Manual;
   Uri uri = new Uri(@"file://" + LongFileName);
   ME.Source = uri;
   ME.MediaEnded += ME_MediaEnded;
}
private void ME_MediaEnded(object sender, RoutedEventArgs e)
{
  if (!IsVisible) return;
  ME.Position = new TimeSpan(0, 0, 1);
  ME.Play();
}

private PropertyChangedEventHandler propertyChanged;

public event PropertyChangedEventHandler PropertyChanged
{
    add
    {
        var wasAttached = propertyChanged != null;
        propertyChanged += value;
        var isAttached = propertyChanged != null;

        if (!wasAttached && isAttached)
            OnPropertyChangedAttached();
    }
    remove
    {
        var wasAttached = propertyChanged != null;
        propertyChanged -= value;
        var isAttached = propertyChanged != null;

        if (wasAttached && !isAttached)
        {
            OnPropertyChangedDetached();
        }
    }
}

void OnPropertyChangedAttached()
{
    IsVisible = true;
    if (ME != null)
        ME.Play();
}

void OnPropertyChangedDetached()
{
    IsVisible = false;
    if (ME != null)
    {
        ME.MediaEnded -= ME_MediaEnded;
        ME.Stop();
        ME.Close();
        ME.Source = null;
        ME = null;
    }
}