Silverlight 的 SaveFileDialog 给出错误“对话框必须由用户启动”
Posted
技术标签:
【中文标题】Silverlight 的 SaveFileDialog 给出错误“对话框必须由用户启动”【英文标题】:SaveFileDialog of silverlight is giving error "Dialog must be user-initiated" 【发布时间】:2012-11-23 13:31:07 【问题描述】:我收到一个错误:
Dialog must be user-initiated
当我从 silverlight 应用程序打开保存文件对话框时,我正在使用以下代码
主按钮点击事件:
private void btnSaveAttach_Click(object sender, RoutedEventArgs e)
if (EditableGV.SelectedItems.Count == 1)
PositionAttachment posAtt = new PositionAttachment();
posAtt = (PositionAttachment)EditableGV.SelectedItems[0];
SaveFile(posAtt.File, posAtt.FileName);
else
RadWindow.Alert("Please select a attachment from the existing attachments list.", null);
return;
private void SaveFile(byte[] fileByte, string fileName)
try
byte[] fileBytes = fileByte;//your bytes here
//Show the dialog
SaveFileDialog dialog2 = new SaveFileDialog();
saveDialog = dialog2.ShowDialog();//This line is giving the error
if (saveDialog == true)
//Get the file stream
dialog2.DefaultFileName = fileName;
string fileExt = fileName.Substring(fileName.LastIndexOf('.'), fileName.Length);
dialog2.DefaultExt = "All Files|*.*|" + fileExt + "|*." + fileExt + "";
using (Stream fs = (Stream)dialog2.OpenFile())
fs.Write(fileBytes, 0, fileBytes.Length);
fs.Close();
//File successfully saved
catch
MessageBox.Show("Error in downloading file");
XMAL 代码:
<StackPanel x:Name="Layout">
<StackPanel x:Name="Messagepanel" Margin="2" Visibility="Collapsed">
<TextBlock x:Name="txtMessage" Text="1" Height="35"></TextBlock>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="4">
<sdk:Label Height="28" Content="Select File:" Name="lblSelectFile" Grid.Column="0" Grid.Row="0" Width="70" />
<TextBox Name="txtFileName" IsReadOnly="True" HorizontalAlignment="Left" VerticalAlignment="Center" Width="303" Grid.Column="1" Margin="12,1,0,0"></TextBox>
<Button Content="Browse" Name="btnBrows" Width="55" Height="22" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="16,1,0,0" Click="btnBrows_Click" Grid.Column="1" />
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="4">
<sdk:Label Height="28" Content="Description:" Name="lblFileDescription" Grid.Column="0" Grid.Row="1" Width="70" />
<TextBox Height="60" Name="txtComments" Grid.Column="1"
VerticalScrollBarVisibility="Auto" Width="301" Margin="13,0,85,2" Grid.Row="1" />
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="4">
<Button x:Name="OKButton" Content="Add" Click="OKButton_Click" Width="43" Margin="363,6,10,15" Grid.Row="2" Grid.Column="1" HorizontalAlignment="Left" />
<Button x:Name="CancelButton" Content="Cancel" Click="CancelButton_Click" Width="53" HorizontalAlignment="Right" Margin="0,6,200,15" Grid.Row="2" Grid.Column="1" />
</StackPanel>
<StackPanel Orientation="Vertical" Margin="4" Height="224">
<sdk:Label Content="Existing Attachment(s):" Height="20" Margin="15,7,324,10" Name="lblExistingAttachemnt" />
<telerikGrid:RadGridView x:FieldModifier="public"
x:Name="EditableGV"
AutoGenerateColumns="False"
ItemsSource="Binding PositionAttachemntCollection, Mode=TwoWay" Margin="0,0,0,7" Height="150">
<telerik:RadGridView.Columns>
<telerik:GridViewDataColumn DataMemberBinding="Binding FileName,Mode=TwoWay"></telerik:GridViewDataColumn>
<telerik:GridViewDataColumn DataMemberBinding="Binding FileDescription,Mode=TwoWay"></telerik:GridViewDataColumn>
<!--<telerik:GridViewColumn Header="" >
<telerik:GridViewColumn.CellTemplate>
<DataTemplate>
<telerik:RadButton Name="btnDownlaod" Content="Save" Click="HyperlinkButton_Click"></telerik:RadButton>
<<HyperlinkButton Click="HyperlinkButton_Click" Content="Binding FileName,Mode=OneWay" TargetName="_blank" NavigateUri="http://www.google.com" />
</DataTemplate>
</telerik:GridViewColumn.CellTemplate>
</telerik:GridViewColumn>-->
</telerik:RadGridView.Columns>
</telerikGrid:RadGridView>
<StackPanel Orientation="Horizontal" Margin="0">
<!--<Button Content="Save Attachment" Name="btnSaveAttach" Width="112" Margin="263,6,10,15" Click="btnSaveAttach_Click" />-->
<Button Content="Close" Name="btnClose" Width="75" Margin="3,6,10,15" Click="btnClose_Click" />
</StackPanel>
</StackPanel>
</StackPanel>
此函数在单击按钮时调用。我想知道我在同一页面上也有一个 openFiledialog,但是它成功地打开了对话框而没有任何错误。为什么那个保存文件对话框会导致错误。
下面是错误的堆栈轨迹:
at System.Windows.Controls.SaveFileDialog.ShowDialogInternal(Window owner)
at System.Windows.Controls.SaveFileDialog.ShowDialog()
at IPVWorkbench.Views.AddPositionAttachments.btnSaveAttach_Click(Object sender, RoutedEventArgs e)
at System.Windows.Controls.Primitives.ButtonBase.OnClick()
at System.Windows.Controls.Button.OnClick()
at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
at System.Windows.Controls.Control.OnMouseLeftButtonUp(Control ctrl, EventArgs e)
at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName, UInt32 flags)
【问题讨论】:
按钮点击和这个调用之间有什么代码?在用户操作和对话框必须出现之间有一个最短时间。如果您超过该时间,您将收到此错误。 我只是获取文件数据并将其存储到公共变量“fileByte”中 您从哪里获取数据?是否使用异步调用? 不,实际上我的页面上有一个显示文件名和文件描述的网格,当用户选择网格行并单击保存按钮时,我上面的函数将被调用,我只是在准备字节数组来自该函数之前的选定行。 嗯。那时不知道。它显然认为对话不是用户发起的。 【参考方案1】:这里给出答案:http://social.msdn.microsoft.com/Forums/en-US/lightswitch/thread/8c6529ab-8967-45e8-9e19-920589b060c1
这有点像“安全功能”:
基本上,您必须直接打开 SaveFileDialog 或 OpenFileDialog 以响应由用户操作(例如单击)引起的事件,这些操作仅在主线程上引发。
【讨论】:
【参考方案2】:您需要在 btnSaveAttach_Click 中显示 SaveFileDialog(这样它是用户启动的)。还有remove all your breakpoints,因为他们也会产生这个错误。
试试这个,看看它是否有效,然后基于它构建其余代码:
private void btnSaveAttach_Click(object sender, RoutedEventArgs e)
SaveFileDialog dialog2 = new SaveFileDialog();
saveDialog = dialog2.ShowDialog();
【讨论】:
不工作...同样的错误来了,我也删除了所有断点。 您使用的是什么版本的 SL? 我刚刚尝试了代码并且工作正常。尝试从方法中取出SaveFileDialog dialog2 = new SaveFileDialog();
行,所以你只留下了dialog2.ShowDialog();
【参考方案3】:
我找到了解决方法。我在按钮单击和确认框的“确定”事件上显示了一个确认框我打开了保存文件对话框,这样它就不会抛出任何异常。
【讨论】:
以上是关于Silverlight 的 SaveFileDialog 给出错误“对话框必须由用户启动”的主要内容,如果未能解决你的问题,请参考以下文章