处理在 xaml 中创建的控件

fex*_*fex 2 c# xaml windows-runtime

如何在 WinRT 中处置由 XAML 代码创建的控件?我创建了ImageSlideShow使用SempahoreSlimand DispatcherTimer(我使用timer.Tick事件)的UserControl。我应该将 .Unloaded 事件处理程序添加到ImageSlideShow用户控件或实现 IDisposable (xaml 是否负责调用 .Dispose?)以释放资源和事件处理程序 - 或者我是否需要像在代码隐藏中创建的控件一样手动处理它?

Xyr*_*oid 5

您应该使用Unloaded的事件UserControl。在取消所有事件的挂钩中,如果您正在使用任何集合控件,则将<Image />& 设置ItemsSource为无效。null

如何使图像无效?

BitmapImage bitmapImage = image.Source as BitmapImage;
bitmapImage.UriSource = null;
image.Source = null;
Run Code Online (Sandbox Code Playgroud)

image是对象<Image />

还要实现IDisposable接口。然后,您需要浏览完整的 UI 元素树,搜索<Image />并调用Dispose所有元素。

有关更多信息,请查看此(也适用于 W8):How to debug most common memoryleaks on WP8