2013-12-18 53 views
1

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

回答

4

您应该使用Unloaded事件UserControl。在那个解除所有事件,取消<Image /> &集合ItemsSourcenull如果您正在使用的任何集合控制。

如何使图像无效?

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

+0

难道图像被GC反正释放(只是可能需要几秒钟)? – fex

+1

不,我在Windows Phone 8中测试过。它有高端的背景图片。 – Xyroid