尝试使用服务来打开一个对话框理解 [英] Trying to understand using a service to open a dialog

查看:142
本文介绍了尝试使用服务来打开一个对话框理解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经通过使用MVVM模式打开一个对话框,讨论阅读。我见过几个例子是说使用一个服务,但我不理解所有的部分是如何结合在一起的。我张贴这个问题,在我读了什么,以便更好地了解我缺少什么要求指导。我会后我下面,和它的工作,但是从我所看到的这些职位我不这样做是正确的,或者不这样做有效。我看到那里创建了一个对话界面,然后使用该接口的类来完成实际的工作。然后在构造函数中通过此界面视图模型,这是一个真正让我困惑,不知道的传球它,没有足够的信息来连接点,部分不确定我错过了什么。



这里有一个帖子我看了看: http://stackoverflow.com/a/1044304 / 4593652 我有种看到他们在暗示什么,但它没有足够的信息的人学习。我不要求任何人来写这对我来说,只是希望,我应该读什么了就明白了这些部分是如何结合在一起的一些建议。



在我的代码我有一个类是这样的:(这工作,我只是觉得我从我阅读其他职位是什么缺少的东西)

 公共类OpenDialogService 
{
公共字符串GetOpenDialog(字符串名称)
{
CommonOpenFileDialog DLG =新CommonOpenFileDialog();
dlg.Title =称号;
dlg.IsFolderPicker = TRUE;
dlg.AddToMostRecentlyUsedList = TRUE;
dlg.AllowNonFileSystemItems = FALSE;
dlg.EnsureFileExists = FALSE;
dlg.EnsurePathExists = TRUE;
dlg.EnsureReadOnly = FALSE;
dlg.EnsureValidNames = TRUE;
dlg.Multiselect = FALSE;
dlg.ShowPlacesList = TRUE;

如果(dlg.ShowDialog()== CommonFileDialogResult.Ok)
{
返回dlg.FileName;
}
返回NULL;
}
}



然后我用这在我的ViewModel当我的命令。叫

 路径=新OpenDialogService()GetOpenDialog(...)。 


解决方案

瓦尔特是basicly正确的。



另外发帖称,他可能会改变他通过IOC容器服务实现。国际奥委会表示控制反转-pattern,你可以阅读的这个文章获得一个基本的概况。我认为,服务一词也是从那个模式。另一个流行语是依赖注入你可以看一下。


I've read through discussions on opening a dialog using mvvm pattern. I've seen several examples that say to use a service, but I'm not understanding how all the pieces fit together. I'm posting this question asking for guidance in what I should read up on to better understand what I'm missing. I'll post what I have below, and it does work, but from what I'm seeing in these posts i'm not doing it right, or maybe not doing efficiently. I see where a dialog interface is created, then a class that uses the interface to do the actual work. Then in the ViewModel a constructor passes in this interface, this is the part that really confuses me, not sure what's passing it in, don't have enough information to connect the dots and not sure what i'm missing.

Here is one post I looked at: http://stackoverflow.com/a/1044304/4593652 I kind of see what they are suggesting, but it's not enough information for someone learning. I'm not asking anyone to write this for me, just hoping for some advice on what I should read up on to understand how these pieces fit together.

In my code I have a class like this: (This works, I just feel like i'm missing something from what I'm reading on other posts)

public class OpenDialogService
    {
    public string GetOpenDialog(string title)
    {
        CommonOpenFileDialog dlg = new CommonOpenFileDialog();
        dlg.Title = title;
        dlg.IsFolderPicker = true;
        dlg.AddToMostRecentlyUsedList = true;
        dlg.AllowNonFileSystemItems = false;
        dlg.EnsureFileExists = false;
        dlg.EnsurePathExists = true;
        dlg.EnsureReadOnly = false;
        dlg.EnsureValidNames = true;
        dlg.Multiselect = false;
        dlg.ShowPlacesList = true;

        if (dlg.ShowDialog() == CommonFileDialogResult.Ok)
        {
            return dlg.FileName;
        }
        return null;
    }
}

Then I use this in my ViewModel when my command is called.

path = new OpenDialogService().GetOpenDialog("...");

解决方案

Walther is basicly right.

The other post says that he could change his service implementation via IOC Container. IOC means the "Inversion of Control"-Pattern, you could read this article to get a basic overview. I think "Service" comes also from that Pattern. Another buzzword is Dependency Injection you can look for.

这篇关于尝试使用服务来打开一个对话框理解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆