【问题标题】:open SaveFileDialog [closed]打开 SaveFileDialog [关闭]
【发布时间】:2012-12-18 13:20:50
【问题描述】:

我有按钮调用异步方法,我想在这个方法的回调中打开 SaveFileDialog,我知道它会引发安全异常

但是任何解决方法,我都依赖回调来知道参数将在保存对话框中使用

例如我调用webservice来检查文件是否存在

如果存在且word版本我将savefiledilaoge的属性设置为word等

调用 web 服务进行检查是异步的,所以在回调中我得到了我想知道的所有信息

【问题讨论】:

  • 你有什么代码......你的确切问题是什么......??
  • 请出示您拥有的代码..我们不是这里的读者..

标签: c# silverlight-4.0


【解决方案1】:

如果您使用像 MVVM Light 这样的框架,您可以从回调中向您的 UI 线程发送消息(传递保存所需的参数)。

编辑。添加示例

很可能在您的代码隐藏页面中,您需要为我们将发送的自定义消息设置一个侦听器。在您的 onload 或 onnavigateto 方法中执行此操作是一种很好的做法。我假设您的回调将加载您从 db 获取的名为 CustomObjectWithParams 的自定义对象。

在你的 xaml 代码后面:

protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        // the "MyViewModel.OpenSaveDialogRequest" can be any string.. it just needs to synch up with what was sent from the viewmodel below
        Messenger.Default.Register<CustomObjectWithParams>(this, "MyViewModel.OpenSaveDialogRequest", objParams => ShowSaveDialog(objParams));
        base.OnNavigatedTo(e);
    }

    private void ShowSaveDialog(CustomObjectWithParams obj) {
        // do your  open save here in the UI thread
    }

    // be smart, make sure you unregister this listener when you navigate away!
    protected override void OnNavigatedFrom(NavigationEventArgs e)
    {

        Messenger.Default.Unregister<CustomObjectWithParams>(this);
        base.OnNavigatedFrom(e);
    }

现在在您的视图模型回调函数中(未在 UI 线程上运行)...您想要发送您获得异步的参数。你可以这样做:

protected void your_asynch_callbackfunction(your args) {
            CustomObjectWithParams objParams = new CustomObjectWithParams();
            // fill up this object here....

            // now send the object to the UI to do something with it
            Messenger.Default.Send<CustomObjectWithParams>(objParams, "MyViewModel.OpenSaveDialogRequest");

        }

【讨论】:

  • 是的,我用它怎么做呢
  • 非常感谢,因为我在不直接与后面的视图代码交互的视图模型中工作
  • 视图模型在它上面用于将数据检索到网格,点击它时我有按钮应该开始保存过程
  • 从视图模型中调用对话框不是一个好习惯。这违反了关注点分离。我希望有一些与此相关的观点?
  • 示例:按钮调用 ViewModel.StartSave()。然后 ViewModel.SaveComplete() 完成。在 SaveComplete() 中,使用上面的示例发送消息。在包含您的按钮的视图中,添加上面的代码以侦听消息。
【解决方案2】:

搞砸这个我在一个小型测试项目中尝试过这个, 也许你想做这样的事情.. 既然您拒绝发布相关代码,请让别人和我自己依赖我们的 读心术

public class AsyncSaveTester : 'put yourclassIheritance Here'
{
    private SaveFileDialog asyncSaveDialog;

    public SaveFileDialog AsyncSaveDialog
    {
        get { return asyncSaveDialog; }
        set { asyncSaveDialog = value; }
    }

    private void Button_Click(object sender, EventArgs e)
    {
        asyncSaveDialog = new SaveFileDialog();
        //Write your code to Show the Dialog here
    }

    // where is the handler for your webservice call...find that and call the Save method from there
    private void Save(string fileToSave)
    {
        Stream fileStream = asyncSaveDialog.OpenFile();
        // If you choose to use Streaming, then you would write the code here to do the file Streaming from 
        // the web Service call
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-06
    • 2022-11-14
    • 1970-01-01
    相关资源
    最近更新 更多