Silverlight problem solving: the dialog box must be enabled by the user securityexception: dialogs must be user-initiated

Source: Internet
Author: User
From: http://www.dotblogs.com.tw/junegoat/archive/2010/10/07/securityexception-dialogs-must-be-user-initiated.aspx

[Silverlight] a minor problem occurs when savefiledialog-securityexception: dialogs must be user-initiated

This is a simple example...

You can write the text in textbox to the local file...
However, "securityexception: dialogs must be user-initiated" appears (the dialog box must be started by the user)
And occasionally...

First introduce the case:

Input text in txtcontext... and then press btnsave to archive...

 

XAML code:

    usercontrol   X: class   =" savefiledialogmemo. mainpage " 
  xmlns   =" http://schemas.microsoft.com/winfx/2006/xaml/presentation " 
  xmlns: x   =" http://schemas.microsoft.com/winfx/2006/xaml " 
  xmlns: d   =" http://schemas.microsoft.com/expression/blend/2008 " 
  xmlns: Mc   =" http://schemas.openxmlformats.org/markup-compatibility/2006 " 
  MC: ignorable   =" D " 
  D: designheight   =" 300 "  D: designwidth   =" 400 " > 
 
<Grid X: Name= "Layoutroot" Background= "White">
         <  Textbox   Height  = "192"  Horizontalalignment  = "Left"   Margin  ="   Name  = "Txtcontext"   Verticalalignment  = "TOP"   Width  = "368"   Fontsize  = "14"   Acceptsreturn  = "True"   Textwrapping  = "Wrap"  Text  = "If you wait until the window is opened, you can quickly press it to save it successfully"   /> 
         <  Button   Content  = "Memory"   Height  = "32"   Horizontalalignment  = "Left"   Margin  = "296,218"   Name  = "Btnsave"   Verticalalignment = "TOP"   Width  = "84"   Fontsize  = "14"   Click  = "Btnsave_click"   /> 
 
</Grid>
</Usercontrol>

 

Next let's take a look at C # code in btnsave:
Private void btnsave_click (Object sender, routedeventargs E)
{
Try {
Savefiledialog SFD = new savefiledialog ();
SFD. defaultext = ". txt ";
SFD. Filter = "text files | *. txt | log files | *. log | all files | *.*";
// It will be normal to annotate everything in the next line
MessageBox. Show ("it will be wrong if you press it for a moment. If you press it faster, it will not be wrong !! ");

If (SFD. showdialog () = true)
{

Using (Stream stream = SFD. openfile ())
{
Using (streamwriter writer = new streamwriter (stream, system. Text. unicodeencoding. Unicode ))
{
Writer.write(this.txt context. Text );
Writer. Close ();
}
Stream. Close ();
}
}
}
Catch (exception ex)
{
MessageBox. Show (ex. stacktrace );
}
}

When you click bntsave...
Before SFD. showdialog (), I will name a MessageBox. Show ("if you stop and press it, it will be wrong. If you press it faster, it will not be wrong !! ");
If you wait... And then press... Exception will occur. If you press a little faster, it won't...
It took me some time to fix this bug, because sometimes it will be wrong and sometimes it will not be wrong ....
I found one sentence mentioned by msdn.
The showdialog method can only be called from user-initiated code, such as a button click event. if showdialog is called from code that is not user-initiated, such as a pageloaded event, a securityexception is thrown. 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.
A simple translation:

This showdialog can only be started by this user for the call, such as a button click event. If showdialog is not called by the user, such as the pageloaded event, a securityexception is thrown. In addition,There is a allowed time limit,The dialog box is displayed. If the time limit between these actions exceeds the upper limit, an exception occurs.

Pretty handsome... This occasional bug is too difficult to find...

So I learned an experience...When you want to call savefiledialog. showdialog (), remember to keep the front clean (Savefiledialog. showdialog () placed in the callCodeThe beginning). Do not engage in other interactions with users... With time restrictions...


Sample source code:

Https://skydrive.live.com/self.aspx/blogsource/SaveFileDialogMemo.rar? Cid = 19ad32092ea01e0d & SC = documents

 

 

 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.