如何在 WinRT 中处理由 XAML 代码创建的控件?我创建了 ImageSlideShow UserControl,它使用 SempahoreSlim 和 DispatcherTimer(我使用 timer.Tick 事件)。我应该将 .Unloaded 事件处理程序添加到 ImageSlideShow 用户控件还是实现 IDisposable(xaml 负责调用 .Dispose?)以释放资源和事件处理程序 - 或者我是否需要像创建控件一样手动处理它在代码隐藏中?
请您参考如下方法:
你应该使用 Unloaded UserControl的事件.在取消所有事件的过程中,取消 <Image /> & 设置 ItemsSource至 null如果您正在使用任何收集控制。
如何取消图片?
BitmapImage bitmapImage = image.Source as BitmapImage;
bitmapImage.UriSource = null;
image.Source = null;
在这里image是 <Image /> 的对象
同时执行 IDisposable界面。然后你需要浏览完整的 UI 元素树,搜索你的 <Image />并调用Dispose在所有这些上。
有关更多信息,请查看此内容(它也适用于 W8):How to debug most common memory leaks on WP8
