用caliburn显微镜打开另一个视图

本文关键字:另一个 视图 caliburn 显微镜 | 更新日期: 2023-09-27 18:05:29

我用的是Caliburn。Micro和I有2个View和相对2个ViewModel:

  • MainView (MainViewModel)
  • BView (BViewModel)

在BView我有一个DataGrid和在BView一个方法来填充DataGrid。在MainView中有一个按钮,我想让你点击按钮打开窗口BView并调用方法来填充数据网格(方法名称是:AllArticles)。

所以当我点击按钮(在mainview)将打开BView与DataGrid填充。

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");
    }    
}

我该怎么办?

用caliburn显微镜打开另一个视图

考虑使用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);
        }    
    }

另外,我注意到你有导出(IShell)属性在你的MainViewModel类-这看起来不正确,因为屏幕不是IShell。