2012-04-25 43 views
0

在ShellViewModel我有以下命令結合打開一個新窗口「遠程查看」WPF:團結:重用窗口已經關閉後

public ICommand RemoteViewCommand 
{ 
    get { return new RelayCommand(RemoteViewExecute, CanRemoteViewExecute); } 
} 

private void RemoteViewExecute() 
{ 
    if (!CanRemoteViewExecute()) 
    { 
     return; 
    } 


    var shellRemoteView = Application._Container.Resolve<ShellRemoteView>(); 
    if (_ShellRemoteView.DataContext==null) 
     _ShellRemoteView.DataContext = Application._Container.Resolve<ShellRemoteViewModel>();   

    shellRemoteView.Show(); 
} 

在我已經註冊了兩個「ShellRemoteView」和「ShellRemoteViewModel啓動「使用終身管理者來實現單例實例。

_Container.RegisterType<ShellRemoteView>(new ContainerControlledLifetimeManager()); 
_Container.RegisterType<ShellRemoteViewModel>(new ContainerControlledLifetimeManager()); 

當shellRemoteView.Show()執行,我關閉窗體,然後重新打開調用shellRemoteView.Show()我得到無效操作excepton:不能設置可見或調用Show,ShowDialog的,或WindowInteropHelper。在窗口關閉後確保處理。

在Unity 中是否有任何解決方法可以在窗口實例關閉的情況下再次獲取窗口實例

回答

0

這條線是你的問題:

return new RelayCommand(RemoteViewExecute, CanRemoteViewExecute); 

基本上你正在創建每次調用Get命令時的新觀點。解決這個問題的方法是在GET語句之外放置一個變量,該變量的作用域是ViewModel級別。讓它存儲對視圖的引用並返回該引用,而不是每次都創建一個新引用。請看Singleton pattern,瞭解如何做到這一點。

+0

你的意思是? private ICommand _remoteViewCommand; public ICommand RemoteViewCommand { get {return _remoteViewCommand (_remoteViewCommand = new RelayCommand(RemoteViewExecute,CanRemoteViewExecute)); } } }這沒有幫助。我實際上是在尋找一種通過團結來實現的方式。 – Anees 2012-04-25 12:36:01

+0

@Andy - 不確定你的意思是「通過Unity」(不是很熟悉Unity的大部分)。這個鏈接有幫助嗎? http://answers.unity3d.com/questions/20949/singleton-implementation.html – IAmTimCorey 2012-04-25 12:45:21

+0

非常感謝你的幫助。 統一我的意思是:http://stackoverflow.com/questions/608585/can-someone-explain-microsoft-unity – Anees 2012-04-25 13:16:23