admin管理员组文章数量:1335434
I'm working on a TikTok-like video scrolling experience using a CollectionView
in .NET MAUI. Each item in the CollectionView
contains a video element (specifically a MediaElement
) that plays when visible. The page will only ever display four videos at a time, so memory usage is not a significant concern for me.
However, I've encountered a couple of issues due to the default recycling behavior of the CollectionView
:
Problems:
State preservation issues: When a video item is recycled, its state is not preserved. To work around this, I've been manually saving the video state (like playback position) in a dictionary and restoring it using SeekTo
method when the video reappears. This workaround, however, makes the scrolling experience feel less smooth and negatively impacts the UX.
Resource cleanup issues: The MediaElement documentation mentions that resources should be released using DisconnectHandler
to avoid memory leaks. But when I attempt to call DisconnectHandler
on recycled MediaElements, I get an ObjectDisposedException
. I suspect this is because the CollectionView
has already disposed of the MediaElement
but didn't call DisconnectHandler
beforehand, leading to resource leaks.
What I've Tried:
I attempted to replace the DataTemplate
with a list of ContentView
instances (each containing a MediaElement
) in the ItemsSource
, hoping this would prevent recycling. Unfortunately, this had no effect.
What I Need:
I'm looking for a way to:
Disable or prevent item recycling in the CollectionView
entirely (even if it impacts performance slightly, as I only have four items).
Ensure proper cleanup of MediaElement
resources when the items are removed or disposed.
Is there a way to achieve this in .NET MAUI, or should I consider an alternative approach to implement this kind of scrolling video UI?
EDIT
I tried to use the SfListView
since (compared to the MAUI CollectionView
) it still has the CachingStrategy
property. The problem is that even with this property set on CreateNewTemplate
the MediaElements
get reset.
本文标签: cHow to prevent item recycling in a CollectionView in NET MAUIStack Overflow
版权声明:本文标题:c# - How to prevent item recycling in a CollectionView in .NET MAUI? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742366227a2461321.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论