3

编辑:

新信息,刚刚设法让一个记录器工作(老实说,我不知道 cm 有一个!)我在尝试使用TryClose().

TryClose requires a parent IConductor or a view with a Close method or IsOpen property

我已经坚持了好几天了,研究结果为零,我之前尝试过发布一个关于这个问题的问题,但没有收到任何答案,所以我认为我的措辞不正确。

我有一个视图和视图模型 ContentView/Model,其中包含以下代码:

内容视图

<MenuItem Header="New Project" x:Name="OpenProject" cal:Message.Attach="[Event Click] = [Action NewProject()]"/>

内容视图模型

public void NewProject()
    {
        NewProjectViewModel viewModel = new NewProjectViewModel(_projectManager);
        _windowManager.ShowWindow(viewModel);
        //If the result is true, we have a new project, otherwise they cancelled the window.
        if (viewModel.Result)
        {
            Project newP = new Project(0, viewModel.ProjectNo, viewModel.ProjectName, 0, 0);
            _projectManager.Insert(newP);
        }
    }

并且视图模型 NewProjectViewModel具有以下内容:

 public void Create()
    {
        this.Result = true;
        TryClose(true);
    }

它的调用方式与之前使用对话框OK按钮上的 message.attach 相同。

但是问题是TryClose()始终无法关闭对话框,并且由于我没有 caliburn.micro 的来源,因此我无法在内部进行调试,但是由于总是返回 null ,TryClose()因此执行(GetView() As Window).Close()也失败了。GetView()

我完全不知道如何关闭此对话框,因此将不胜感激任何帮助或建议。

编辑: 由于我似乎没有得到任何答案,就像以前的问题一样,我假设我缺少信息。为了理解这个问题,我认为它可能与使用视图优先方法有关。

NewProjectView我有以下内容:

             xmlns:cal="http://www.caliburnproject.org"
         cal:Bind.Model="ShippingClient.ViewModels.NewProjectViewModel"

这是用来绑定视图模型的,而不是通常使用的自动方式,也许这就是为什么GetView()返回null?

4

1 回答 1

4

You are going to absolutely kick yourself:

Remove the cal:Bind.Model and cal:View.Model bindings...

If you are working ViewModel-First (i.e. you are creating a viewmodel and showing it using WindowManager or in a conductor) all the binding stuff that glues the viewmodel to the view is done for you by CM.

In this case you shouldn't use any View-First bindings. What you are essentially doing is newing up another instance of your VM and binding that to your view... so you have two viewmodels in the background, one wired up nicely but not bound any more, and a non-wired up instance which is bound to your view but doesn't know about the conductor.

Just remove any bindings to the viewmodel in your view and it will all work!

于 2013-08-15T11:25:52.617 回答