Archive

Posts Tagged ‘File Save Dialog Silverlight’

Open a ‘File Save Dialog’ window in Silverlight – During Asynchronous Call

October 13, 2011 2 comments

Generally its not possible to open a SaveFileDialog window in Synchronous complete method like,

Wrong Method :

private void Method(Object param)
{
Client consoleClient = new Client(“”);
consoleClient.XXXCompleted+=new EventHandler<XXXCompletedEventArgs>(Method_Completed);
XXXRequest request = new XXXRequest();
request.p_Parameters = param;
consoleClient.XXXAsync(request);
}

void Method_Completed(object sender, XXXCompletedEventArgs e)
{
var result = (YYY)e.Result.XXXResult;
UIThread.Run(delegate() {
SaveFileDialog dlg = new SaveFileDialog();      // This will throw a Exception
dlg.DefaultExt = “.zip”;                        // Default file extension
dlg.Filter = “Zip Archive (.zip)|*.zip”;       // Filter files by extension
if (dlg.ShowDialog() == true)
{

}
}
}

Right Method:

private void Method(Object param)
{
Client consoleClient = new Client(“”);
consoleClient.XXXCompleted+=new EventHandler<XXXCompletedEventArgs>(Method_Completed);
XXXRequest request = new XXXRequest();
request.p_Parameters = param;
consoleClient.XXXAsync(request);
}

void Method_Completed(object sender, XXXCompletedEventArgs e)
{
var result = (YYY)e.Result.XXXResult;
ChildWindowDlg _confirmationWnd = new ChildWindowDlg ();
_confirmationWnd.Show();
}

and inside that ChildWindowDlg  class create a method and call SaveDialog window, like

void Save_ButtonClick(object sender, routedEventArg e)
{
SaveFileDialog dlg = new SaveFileDialog();
dlg.DefaultExt = “.zip”;
dlg.Filter = “Zip Archive (.zip)|*.zip”;
if (dlg.ShowDialog() == true)
{
using(Stream s = (Stream)dlg.OpenFile())
{
s.Write(data, index, length);
s.Close();
}
}
}

Categories: WPF Tags:
%d bloggers like this: