[英]Caliburn.Micro view not destroyed in Popup
I've run across an issue when I close a popup created with Caliburn micro : The view doesn't seem to be destroyed. 当我关闭用Caliburn micro创建的弹出窗口时,我遇到了一个问题:该视图似乎没有被破坏。
I use Caliburn.Micro 2.0.1 with MEF, you can see my exemple here : https://github.com/louisfish/BaseCaliburn 我将Caliburn.Micro 2.0.1与MEF一起使用,您可以在这里看到我的示例: https : //github.com/louisfish/BaseCaliburn
Basically, I create a window with a button inside. 基本上,我创建一个带有按钮的窗口。 When you click on this button, a new window is opened with the ShowWindow function of WindowManager.
当您单击此按钮时,将使用WindowManager的ShowWindow功能打开一个新窗口。 In this popup, I created a message with a binding.
在此弹出窗口中,我创建了带有绑定的消息。 I put output traces when my message is get in my ViewModel.
当我的消息进入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;
}
}
}
Each time I open and close my window, the old binding stays there. 每次我打开和关闭窗口时,旧的绑定都留在那里。 So after 5 open/close, I have 5 call to my get Message in my ViewModel.
因此,在5次打开/关闭之后,我对ViewModel中的get Message进行了5次调用。
So I receive the binding of the old views: 因此,我收到了旧视图的绑定:
Message is get
---Window is activated---
Message is get
Message is get
Message is get
Message is get
Message is get
HomeViewModel
. HomeViewModel
实例。 You're having your main window bound to the HomeViewModel
, so every time you click StartApp
, the method is called on the same instance. HomeViewModel
,因此每次单击StartApp
,都会在同一实例上调用该方法。 PopupViewModel
property, which is initialized by your dependency injection container as soon as the HomeViewModel
gets created. PopupViewModel
属性,一旦创建HomeViewModel
就会由您的依赖项注入容器初始化该属性。 Then, it stays the same. StartApp
method gets the value of the property, the same instance of PopupViewModel
is returned. StartApp
方法获取该属性的值时,都会返回PopupViewModel
的相同实例。 What you actually want is a new PopupViewModel
instance every time you call StartApp
. 每次调用
StartApp
时,您真正想要的是一个新的PopupViewModel
实例。 You need a factory. 您需要一家工厂。 In MEF, you can import
ExportFactory<T>
to create instances on demand: 在MEF中,您可以导入
ExportFactory<T>
以根据需要创建实例:
[Import] public ExportFactory<PopupViewModel> PopupViewModelFactory { get; set; } public void StartApp() { Application.Current.ShutdownMode = ShutdownMode.OnMainWindowClose; PopupViewModel newPopup = this.PopupViewModelFactory.CreateExport().Value; IoC.Get<IWindowManager>().ShowWindow(newPopup); }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.