2013-03-24 42 views
0

我使用Caliburn.Micro和我有2查看和相對2視圖模型:打開另一視圖與卡利微

  • 的MainView(MainViewModel)
  • BView(BViewModel)

在BView我有一個DataGrid和BView中的一個方法來填充DataGrid。 在MainView中有一個Botton,我想讓你單擊按鈕打開窗口BView並調用methot來填充DataGrid(方法名稱爲:AllArticles)。

所以當我點擊按鈕(在MainWiew中)將打開填充DataGrid的BView。

的MainViewModel代碼:

[Export(typeof(IShell))] 
public class MainViewModel : Screen 
{ 
    public string Path{ get; set; } 

    public void Open() 
    { 
     OpenFileDialog fd = new OpenFileDialog(); 
     fd.Filter = "Text|*.txt|All|*.*"; 
     fd.FilterIndex = 1; 

     fd.ShowDialog(); 

     Path= fd.FileName; 
     NotifyOfPropertyChange("Path"); 
    } 

} 

的BViewModel代碼:

public class BViewModel : Screen 
{ 
    public List<Article> List { get; private set; } 

    public void AllArticles() 
    { 
     Recover recover = new Recover(); 
     List = recover.Impor().Articles; 
     NotifyOfPropertyChange("List"); 
    }  
} 

我該怎麼辦?

回答

2

考慮使用Caliburn的WindowManager。在主視圖模型的代碼可能是這樣的:

[Export(typeof(IShell))] 
    public class MainViewModel : Screen 
    { 
     public string Path{ get; set; } 

     [Import] 
     IWindowManager WindowManager {get; set;} 

     public void Open() 
     { 
      OpenFileDialog fd = new OpenFileDialog(); 
      fd.Filter = "Text|*.txt|All|*.*"; 
      fd.FilterIndex = 1; 

      fd.ShowDialog(); 

      Path= fd.FileName; 
      NotifyOfPropertyChange("Path"); 

      WindowManager.ShowWindow(new BViewModel(), null, null); 
     }  
    } 

另外,我注意到你有你的MainViewModel類出口(IShell的)屬性 - 這看起來不正確的,因爲屏幕不是IShell的。

+0

你的屏幕,我屏幕我們'屏幕''IScreen'。 (對不起,但是像沃爾瑪指出的那樣'Screen'是'IScreen'而不是'IShell'!) – Charleh 2013-03-25 11:32:22

+0

謝謝,我只是不明白我在聲明這兩個類時所做的:view principal [Export (typeof(IShell))]和第二個(BViewModel)把[Export(typeof(IScreen))]權利? – puti26 2013-03-25 14:07:18

+0

這取決於...在你的情況下,也許只是[導出] - 沒有參數。它將把你的視圖模型放到DI容器中。 – Volma 2013-03-25 17:11:46