2013-03-13 14 views
0

我的命令文檔打印在MVVM作爲:打印PrintDocument的使用MVVM提供錯誤:對話框必須由用戶啓動

private void OKButton_Click(object sender, RoutedEventArgs e) 
    { 
     PrintDocument doc = new PrintDocument(); 
     doc.PrintPage += new EventHandler<PrintPageEventArgs>(doc_PrintPage); 
     doc.Print("Payment Receipt"); 
     this.DialogResult = true; 
    } 

    void doc_PrintPage(object sender, PrintPageEventArgs e) 
    { 
     Grid pGrid = new Grid(); 
     pGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(30) }); 
     pGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(665, GridUnitType.Star) }); 
     pGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(30) }); 
     pGrid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(30) }); 
     pGrid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) }); 
     pGrid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(20) }); 
     // Stretch to the size of the printed page 
     pGrid.Width = e.PrintableArea.Width; 
     pGrid.Height = e.PrintableArea.Height; 

     // Assign the XAML element to be printed 
     Grid parentGrid = grdReceipt.Parent as Grid; 
     parentGrid.Children.Remove(grdReceipt); 
     pGrid.Children.Add(grdReceipt); 
     Grid.SetColumn(grdReceipt, 1); 
     Grid.SetRow(grdReceipt, 1); 

     // Stretch to the size of the printed page 
     pGrid.Width = e.PrintableArea.Width; 
     //grdReceipt.Height = e.PrintableArea.Height; 

     // Assign the XAML element to be printed 
     e.PageVisual = pGrid; 

     // Specify whether to call again for another page 
     e.HasMorePages = false; 
    } 

當執行doc.Print()它給了我錯誤的對話框必須由用戶啓動。 請幫助...

回答

1

http://msdn.microsoft.com/en-us/library/ff382752%28v=vs.95%29.aspx

爲了安全起見,如果Silverlight應用程序是一個沙盒 應用程序,文件和打印對話框必須是用戶啓動。這意味着您必須從用戶啓動的操作(如按鈕的單擊事件處理程序)中顯示它們。如果您嘗試顯示非用戶啓動的代碼的對話框,則會發生SecurityException。另外,用戶啓動對話和顯示對話框之間允許的時間是有限制的。

那麼OKButton_Click當用戶點擊一個按鈕時調用? 你也許在點擊和執行實際打印之間有一個調試點?

相關問題