任何规避“对话框必须由用户启动”的方法都可以。例外?

发布于 2024-12-13 19:10:52 字数 1836 浏览 1 评论 0原文

我的应用程序有一个“打开文件”按钮。在启动 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

空‖城人不在 2024-12-20 19:10:53

这可能是桌面应用程序的标准场景,但您并不是在创建桌面应用程序。您正在创建一个在安全沙箱中运行的组件,并且出于充分的理由该组件带有一些相当严格的限制。

没有巧妙的方法来处理这种情况。您可以提供关闭“文档”功能,该功能将弹出一个确认框,警告继续操作将丢失工作。

在当前未保存的情况下尝试打开另一个“文档”时,您所能做的就是显示消息,指示用户进行选择,关闭当前“文档”并放弃其更改或选择保存。用户将必须手动执行这些操作,然后再次选择打开“文档”。

如果您的应用程序支持多个“打开”文档,您可能可以稍微改进一下,至少用户不会因打开“文档”而被征税。

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".

不弃不离 2024-12-20 19:10:53

您应该立即在鼠标 Click 处理程序中显示 SaveFileDialog
在您的情况下,它将位于 private void b_Click 中。

在鼠标单击和 SaveFileDialog 之间放置一些障碍是行不通的。

You should be showing SaveFileDialog in mouse Click handler immediately.
In your case that would be in private void b_Click.

Putting some obstacles between mouse click and SaveFileDialog won't work.

仙女山的月亮 2024-12-20 19:10:52

您的问题的简短答案是“否”,因为第二个对话框不再是用户为 silverlight 运行时启动的。如果在打开对话框之前显示消息框,情况也是如此。

以下是来自 MSDN 关于对话框的安全限制

出于安全目的,如果 Silverlight 应用程序是沙盒应用程序,则文件和打印对话框必须由用户启动。这意味着您必须通过用户启动的操作(例如按钮的单击事件处理程序)显示它们。如果您尝试从非用户启动的代码显示对话框,则会发生 SecurityException。此外,用户启动对话框和显示对话框之间允许的时间也有限制。如果超过这些操作之间的时间限制,就会发生异常。当您将 Visual Studio 调试器与对话框一起使用时,如果您在对话框创建和显示对话框之间设置断点,则会引发 SecurityException。由于用户发起的限制,这是预期的行为。如果在调用 ShowDialog 之后设置断点,则不会抛出异常。

如果您尝试通过 KeyDown 事件处理程序和对应用程序代码的其他同步调用(例如 LayoutUpdated 或 SizeChanged 事件处理程序)显示对话框,则会引发异常。但是,当应用程序托管在 Internet Explorer 中并在保护模式下运行时,不会引发异常。

当插件处于全屏模式时,Silverlight 插件对对话框的支持有限。在大多数情况下,以全屏模式显示对话框将导致插件恢复为嵌入模式。但是,为了避免在某些浏览器上出现问题,您应该在使用这些类之前退出全屏模式。在浏览器外部运行的 Silverlight 应用程序中,您可以向用户显示提示以在全屏模式下启用对话框。此外,对于受信任的应用程序,用户启动限制也放宽了。有关详细信息,请参阅受信任的应用程序。

可以使用以下代码轻松测试时间限制:

private void OpenDialog(object sender, RoutedEventArgs e) {
  MessageBox.Show("test");

  OpenFileDialog fileDialog = new OpenFileDialog();
  var result = fileDialog.ShowDialog();
}

当显示 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

For security purposes, if a Silverlight application is a sandboxed application, file and print dialog boxes must be user-initiated. This means you must show them from a user-initiated action, such as the click event handler for a button. If you attempt to show a dialog box from non-user initiated code, a SecurityException will occur. In addition, there is a limit on the time allowed between when the user initiates the dialog and when the dialog is shown. If the time limit between these actions is exceeded, an exception will occur. When you are using the Visual Studio debugger with a dialog box, a SecurityException will be thrown if you set a breakpoint between dialog box creation and showing the dialog box. Because of the user-initiated restrictions, this is the expected behavior. If you set a breakpoint after the call to ShowDialog, no exception will be thrown.

If you attempt to show the dialog box from KeyDown event handlers and other synchronous calls to application code, such as LayoutUpdated or SizeChanged event handlers, an exception will be thrown. However, an exception will not be thrown when the application is hosted in Internet Explorer, running in protected mode.

The Silverlight plug-in has limited support for dialog boxes when the plug-in is in full-screen mode. In most cases, displaying the dialog box in full-screen mode will cause the plug-in to revert to embedded mode. However, to avoid issues on some browsers, you should exit full-screen mode before using these classes. In Silverlight applications that run outside the browser, you can display a prompt to the user to enable dialog boxes in full-screen mode. Also, the user initiation restrictions are relaxed for trusted applications. For more information, see Trusted Applications.

The time limit, could be easily tested with following code:

private void OpenDialog(object sender, RoutedEventArgs e) {
  MessageBox.Show("test");

  OpenFileDialog fileDialog = new OpenFileDialog();
  var result = fileDialog.ShowDialog();
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文