【问题标题】:Open another view with caliburn micro使用 caliburn micro 打开另一个视图
【发布时间】:2013-03-24 18:01:24
【问题描述】:

我使用 Caliburn.Micro,我有 2 个 View 和相对的 2 个 ViewModel:

  • 主视图(MainViewModel)
  • BView (BViewModel)

在 BView 中有一个 DataGrid,在 BView 中有一个填充 DataGrid 的方法。 在MainView中有一个Botton,我要你点击按钮打开BView窗口并调用填充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");
    }    
}

我该怎么办?

【问题讨论】:

    标签: c# wpf view caliburn.micro


    【解决方案1】:

    考虑使用 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 类上有 Export(IShell) 属性 - 这看起来不正确,因为 Screen 不是 IShell。

    【讨论】:

    • 你筛选,我筛选我们所有人ScreenIScreen。 (很抱歉不得不这样做,但是正如 Volma 指出的那样,ScreenIScreen 而不是 IShell!)
    • 谢谢,我只是不明白我在两个类的声明中放了什么:视图主体 [Export(typeof(IShell))] 和第二个 (BViewModel) 放 [Export(typeof( IScreen))] 对吗?
    • 这取决于...在您的情况下,可能只是 [Export] - 没有参数。它会将您的视图模型放入 DI 容器中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-04
    • 1970-01-01
    • 2016-04-06
    相关资源
    最近更新 更多