任何规避“对话框必须由用户启动”的方法都可以。例外?
我的应用程序有一个“打开文件”按钮。在启动 OpenFileDialog 之前,它会询问用户是否要保存当前文件,如果需要,它会启动 SaveFileDialog。然后它启动 OpenFileDialog。相当标准的东西。
我的问题是 silverlight 然后将 OpenFileDialog.ShowDialog() 方法视为不是用户启动的,并且我收到 SecurityException。
有没有已知的合理方法来避免这种异常?这肯定是一个非常标准的场景吗?
该应用程序位于浏览器内。
欢迎任何想法
编辑:
抱歉,不允许发布实际代码:(逻辑非常简单:在 psuedocode 中,“OpenFile”按钮按下事件调用类似的方法:
*启动一条新的 SL 消息,询问是否先保存。
*在消息窗口中单击“是/否”: - 如果否,则转到加载 -如果是,启动 SaveFileDialog.ShowDialog(),转到 Load
*Load: 启动打开文件对话框
编辑2: 小程序...
主页XML内容:
<Grid x:Name="LayoutRoot" Background="White">
<Button Content="Open" Click="Button_Click"/>
</Grid>
代码:
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;
}
}
}
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;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这可能是桌面应用程序的标准场景,但您并不是在创建桌面应用程序。您正在创建一个在安全沙箱中运行的组件,并且出于充分的理由该组件带有一些相当严格的限制。
没有巧妙的方法来处理这种情况。您可以提供关闭“文档”功能,该功能将弹出一个确认框,警告继续操作将丢失工作。
在当前未保存的情况下尝试打开另一个“文档”时,您所能做的就是显示消息,指示用户进行选择,关闭当前“文档”并放弃其更改或选择保存。用户将必须手动执行这些操作,然后再次选择打开“文档”。
如果您的应用程序支持多个“打开”文档,您可能可以稍微改进一下,至少用户不会因打开“文档”而被征税。
It may be a standard scenario for a desktop application but you aren't creating a desktop application. You are creating a component that runs in a secure sandbox and that comes with some fairly stringent restrictions for good reasons.
There is no slick way to handle this scenario. You could provide a close "document" feature which will popup a confirmation box warning the continuing will loses work.
In an attempt to open another "document" whilst the current is unsaved all you can do is display message instructing the user on their choices, either close the current "document" and abandon its changes or to choose to save. The user will have to manually perform these actions and then choose again to open a "document".
You might be able to improve things a bit if your app supported multiple "open" documents, at least the user wouldn't be taxed for openning a "document".
您应该立即在鼠标
Click
处理程序中显示SaveFileDialog
。在您的情况下,它将位于
private void b_Click
中。在鼠标单击和 SaveFileDialog 之间放置一些障碍是行不通的。
You should be showing
SaveFileDialog
in mouseClick
handler immediately.In your case that would be in
private void b_Click
.Putting some obstacles between mouse click and
SaveFileDialog
won't work.您的问题的简短答案是“否”,因为第二个对话框不再是用户为 silverlight 运行时启动的。如果在打开对话框之前显示消息框,情况也是如此。
以下是来自 MSDN 关于对话框的安全限制
可以使用以下代码轻松测试时间限制:
当显示 MessageBox 时真正快速按下“Enter”键时,随后会显示 OpenFileDialog,不会引发安全异常。但是,如果您将 MessageBox 打开一小段时间,则在关闭 MessageBox 后会引发 SecurityException。
我认为时间限制是相继显示两个对话框的问题,因为时间限制将超过显示第一个对话框。
A short answer to your question is "NO", cause the second Dialog isn´t user-initiated anymore for the silverlight runtime. The same is true if you display a MessageBox before opening a Dialog.
Here´s some information from MSDN about security restrictions on Dialogs
The time limit, could be easily tested with following code:
When you really hit fast the "Enter"-Key when the MessageBox gets displayed, the OpenFileDialog gets displayed afterwards, raising no security exception. But if you leave the MessageBox open for a small amount of time, after closing the MessageBox a SecurityException is raised.
I think the time limit is the problem showing two Dialogs one after another, cause the time limit will exceed showing the first one.