我關閉用Caliburn micro創建的彈出窗口時遇到了問題:視圖似乎不被銷燬。Caliburn.Micro視圖在Popup中不被破壞
我用Caliburn.Micro 2.0.1 MEF的,你可以在這裏看到我爲例: https://github.com/louisfish/BaseCaliburn
基本上,我創建了一個窗口,裏面的按鈕。 當你點擊這個按鈕時,用WindowManager的ShowWindow函數打開一個新窗口。 在這個彈出窗口中,我創建了一個帶有綁定的消息。 當我的消息進入ViewModel時,我輸出了跟蹤信息。
using Caliburn.Micro;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BaseCaliburn.ViewModels
{
[Export]
public class PopupViewModel : Screen
{
private int _timesOpened;
private string _message;
public string Message
{
get
{
Debug.WriteLine("Message is get");
return _message;
}
set
{
if (value == _message) return;
_message = value;
NotifyOfPropertyChange(() => Message);
}
}
protected override void OnActivate()
{
Debug.WriteLine("---Window is activated---");
_timesOpened++;
Message = "Popup number : " + _timesOpened;
}
}
}
每次我打開和關閉窗口時,舊的綁定都會停留在那裏。 所以5打開/關閉後,我有5調用我的ViewModel中的消息。
所以我收到的舊觀點的結合:
Message is get
---Window is activated---
Message is get
Message is get
Message is get
Message is get
Message is get
兩件事情可能,1)你是不是在你的popupview關閉調用TryClose(真),這應該關閉視圖模型和「殺它「,因爲你正在使用MEF,有'''[PartCreationPolicy(CreationPolicy.NonShared)''',默認情況下,MEF用PartCreationPolicy = Shared創建所有部分,相當於」Singleton「 – mvermef 2014-12-11 09:20:39
我嘗試添加[PartCreationPolicy(CreationPolicy .NonShared)]到我的PopupViewModel和行爲是相同的。 – 2014-12-11 13:58:07