RinggoYao 发表于 2013-1-2 23:13:53

Silverlight:Memory Leak处理

<div id="cnblogs_post_body">         Silverlight开发中可能会遇到各种内存泄露的情形,由于CLR本身给我们提供了垃圾回收功能,所以在一般的情况下,我们也可能不是太重视内存是否得到有效的释放,
            本文试图还原自己遇到一个场景,给各位做Silverlight的朋友们一个参考.
            那么我处理的情形是在Silverlight自带的导航框架中遇到的,我将ViewModel绑定到对应的View中,唯一的区别是我的Viewmodel中包含了一个DispatcherTimer.
            看我的Viewmodel的代码:
<div id="codeSnippetWrapper"> <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet">       public class AboutViewModel : ViewModelBase,IDisposable   {   public static Lazy<DispatcherTimer> _dispatcher;   private string _currentDateTime;   public AboutViewModel()   {       _dispatcher = new Lazy<DispatcherTimer>();       _dispatcher.Value.Interval = TimeSpan.FromSeconds(1);       _dispatcher.Value.Tick += _dispatcher_Tick;       _dispatcher.Value.Start();   }    public string CurrentDateTime   {       get { return _currentDateTime; }       set { _currentDateTime = value;      Notify(()=>CurrentDateTime);      }   }   void _dispatcher_Tick(object sender, EventArgs e)   {       _dispatcher.Value.Tick -= _dispatcher_Tick;       CurrentDateTime = DateTime.Now.ToLongTimeString();       _dispatcher.Value.Tick += _dispatcher_Tick;   }    public void Dispose()   {       _dispatcher.Value.Stop();       _dispatcher = null;   }
页: [1]
查看完整版本: Silverlight:Memory Leak处理