-5

このメソッドのコールバックで SaveFileDialog を開くために必要な非同期メソッドを呼び出すボタンがあります。セキュリティ例外が発生することはわかっています

しかし、回避策として、保存ダイアログでパラメーターが使用されることを知る上でコールバックに依存しています

たとえば、ファイルが存在するかどうかを確認するために webservice を呼び出します

Word バージョンが存在する場合は、savefiledilaoge のプロパティを word などに設定します。

チェックのために webservice を呼び出すのは非同期なので、コールバックで知りたい情報をすべて取得します

4

2 に答える 2

1

MVVM Light などのフレームワークを使用している場合は、コールバックから UI スレッドにメッセージを送信できます (保存に必要なパラメーターを渡します)。

編集。追加例

ほとんどの場合、コード ビハインド ページで、送信するカスタム メッセージのリスナーをセットアップする必要があります。onload または onnavigateto メソッドでこれを行うことをお勧めします。CustomObjectWithParamsコールバックは、db から取得するというカスタム オブジェクトをロードすると仮定します。

あなたの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");

        }
于 2012-12-18T13:38:27.367 に答える
0

これをいじって、小さなテストプロジェクトでこれを試しました。おそらく、あなたはこのようなことをしたいと思っているでしょう..関連するコードを投稿することを拒否しているので、他の人だけでなく私自身も私たちの心を読むスキルに頼るようにしてください.

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
    }
}
于 2012-12-18T13:48:10.050 に答える