My app has a 'open file' button. Before launching the OpenFileDialog, it asks whether the user wants to save the current file, and if they do, it launches a SaveFileDialog. It then launches the OpenFileDialog. Pretty standard stuff.
My problem is that silverlight then sees the OpenFileDialog.ShowDialog() method as not user-initiated, and I get a SecurityException.
Is there any known reasonable way to avoid this exception? Surely this is a pretty standard scenario?
The app is within a browser.
Any ideas welcome
EDIT:
Sorry, not allowed to release actual code :( The logic is pretty simple though: In psuedocode the 'OpenFile" button press event calls a method something like:
*Launch a new SL message asking whether to save first.
*On message window yes/no clicked: -If No, Go to Load -If Yes, Launch SaveFileDialog.ShowDialog(), go to Load
*Load: Launch An Open File Dialog
EDIT 2: Mini programme...
XML content for the main page:
<Grid x:Name="LayoutRoot" Background="White">
<Button Content="Open" Click="Button_Click"/>
</Grid>
Code:
using System.Windows;
using System.Windows.Controls;
namespace SilverlightApplication15
{
public partial class MainPage : UserControl
{
AskWindow aw = new AskWindow();
public MainPage()
{
InitializeComponent();
aw.Closed += new System.EventHandler(aw_Closed);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
aw.Show();
}
private void aw_Closed(object sender, System.EventArgs e)
{
if (aw.DialogResult == true)
{
SaveFileDialog svd = new SaveFileDialog();
svd.ShowDialog();
}
OpenFileDialog ofd = new OpenFileDialog();
ofd.ShowDialog();//Causes security exception
}
}
public class AskWindow : ChildWindow
{
public AskWindow()
{
Button b = new System.Windows.Controls.Button();
b.Click += new System.Windows.RoutedEventHandler(b_Click);
b.Content = "Yes, save it";
this.Content = b;
}
private void b_Click(object sender, System.Windows.RoutedEventArgs e)
{
this.DialogResult = true;
}
}
}