繁体   English   中英

使用Prism MVVM的OpenFileDialog

[英]OpenFileDialog using Prism MVVM

我正在使用Prism 6.1来实现MVVM模式。 对于通知/确认对话,我正在使用InteractionRequest和InteractionRequest,详见msdn上的使用Prism Library页面的高级MVVM方案。 我的问题是如何在MVVM中开始使用OpenFileDialog。 Prism没有为Save and Open对话提供类似的东西。 能否请给我一个代码示例,该代码应该包含在View&ViewModel中。 谢谢。

您只需创建一个对话框服务即可。

public interface IDialogService
{
    void Show();
}

public class DialogService : IDialogService
{
    public void Show()
    {
        //logic to show your dialogs
    }
}

确保将其注册到您的容器:

Conatiner.Register<IDialogService, DialogService>( do you want a singletone?);

然后在你的ctor中询问它

public ViewAViewModel(IDialogService ds) { ... }

现在随时打电话给你。

我一个人使用专用的TriggerAction。

public class SelectFileAction : TriggerAction<FrameworkElement>
{
    protected override void Invoke( object parameter )
    {
        ISelectFilePayload payload = notification.Content as ISelectFilePayload;

        // Configure the open file dialog
        Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
        dlg.Filter = payload.Filter;

        // Show the dialog 
        Nullable<bool> result = dlg.ShowDialog();

        // Process dialog result
        if (result == true)
        {
            // Return the name of the selected file in the payload
            payload.Path = dlg.FileName;

            // The Callback property, if set, contains the invoker's callback method
            var callback = args.Callback;

            // Call the invoker's callback if any
            if (callback != null)
            {
                callback();
            }
        }
    }
}

暂无
暂无

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

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