简体   繁体   English

Caliburn.Micro - ShowDialog()如何关闭对话框?

[英]Caliburn.Micro - ShowDialog() how to close the dialog?

EDIT: 编辑:

New Information, just managed to get a logger working (I honestly had no idea cm had one!) and i'm given this message when attempting to use TryClose() . 新信息,只是设法让记录器工作(我真的不知道cm有一个!)并且我在尝试使用TryClose()时给出了此消息。

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

I have been stuck on this for a number of days now, and research has turned up zero, I tried posting a question previously about this issue but it received no answers so i assume I didn't word it correctly. 我已经被困在这几天了,研究已经变为零,我试着先前发布了一个关于这个问题的问题,但没有得到答案,所以我假设我没有正确说出来。

I have a view and viewmodel ContentView/Model which has the following code in them: 我有一个视图和viewmodel ContentView / Model ,其中包含以下代码:

ContentView : ContentView

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

ContentViewModel : ContentViewModel

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

and the viewmodel NewProjectViewModel has the following: 并且视图模型NewProjectViewModel具有以下内容:

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

which is called in the same was as previously using an message.attach on the OK button of the dialog. 与之前在对话框的“ 确定”按钮上使用message.attach一样调用。

However the issue is that TryClose() always fails to close the dialog, and as i don't have the source of caliburn.micro i can't debug inside TryClose() however doing (GetView() As Window).Close() also fails because GetView() always returns null. 然而问题是TryClose()总是无法关闭对话框,因为我没有caliburn.micro的源代码我无法在TryClose()调试但是做(GetView() As Window).Close()也失败因为GetView()总是返回null。

I'm at a complete loss as to how i can close this dialog, so any help or suggestions would be greatly appreciated. 关于如何关闭此对话框我完全不知所措,所以任何帮助或建议将不胜感激。

EDIT: Since i seem to be getting no answers on this, where as previous questions do, I'll assume i have information missing. 编辑:因为我似乎没有得到这方面的答案,就像以前的问题一样,我会假设我缺少信息。 In an attempt to understand the issue i think it may have something to with using the view first approach. 为了理解这个问题,我认为它可能与使用视图第一种方法有关。

In the NewProjectView i have the following: NewProjectView中我有以下内容:

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

This is used to bind the viewmodel rather than the automatic way that is usually used, perhaps this is why GetView() returns null? 这用于绑定viewmodel而不是通常使用的自动方式,也许这就是GetView()返回null的原因?

You are going to absolutely kick yourself: 你绝对会踢自己:

Remove the cal:Bind.Model and cal:View.Model bindings... 删除cal:Bind.Modelcal:View.Model绑定...

If you are working ViewModel-First (ie 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. 如果您正在使用ViewModel-First(即您正在创建一个视图模型并使用WindowManager或在指挥中显示它),那么将视图模型粘合到视图的所有绑定内容都由CM完成。

In this case you shouldn't use any View-First bindings. 在这种情况下,您不应使用任何View-First绑定。 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. 你实际上正在做的是新建你的VM的另一个实例并将其绑定到你的视图...所以你在后台有两个视图模型,一个很好地连接但不再绑定,一个非有线的实例是绑定到您的视图,但不知道指挥。

Just remove any bindings to the viewmodel in your view and it will all work! 只需删除视图中对viewmodel的任何绑定,它都可以正常工作!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM