2015年6月17日水曜日

Silverlightアプリで、ファイル保存ボタン実行時に、エラーになる

SilverlightのWebサイト開発にて、アイコンクリックして、ファイル保存ダイアログ表示させるのに、以下のエラーが発生しました。

150616_145829:Severe:System.Security.SecurityException: [FileDialog_UserInitiated]
引数:
デバッグ用のリソース文字列は利用できません。キーや引数に関する情報が問題の診断に役立つ場合があります。 http://go.microsoft.com/fwlink/?linkid=106663&Version=5.1.40416.0&File=System.Windows.dll&Key=FileDialog_UserInitiated を参照してください。
   場所 System.Windows.Controls.SaveFileDialog.ShowDialogInternal(Window owner)

原因を調べると、下記の参考URLに回答がありました。
you have to open a SaveFileDialog or OpenFileDialog directly in response to a event that is caused by a user action (e.g. Click), which are only raised on the main thread. So, you cannot open these dialogs from your user code in a normal method, because user code is executed on the logic thread, which is 1) not the main thread, and 2) not necessarily executed in response to a event caused by a user action.

ファイル保存ボタンは、Clickなどの直接イベント関数に実装しなければなりません。ユーザ定義関数で行うとメインスレッドで行わないのが原因です

⇒ButtonのClick時に定義した関数内で、System.Windows.Controls.SaveFileDialogを実行するように変更で対応しました


【参考URL】
https://social.msdn.microsoft.com/Forums/vstudio/en-US/8c6529ab-8967-45e8-9e19-920589b060c1/how-to-avoid-the-dialogs-must-be-userinitiated-exception-when-trying-to-print-from-lightswitch?forum=lightswitch

0 件のコメント:

コメントを投稿