2012-11-17 14 views
2

嗨,大家好,我一直在使用WPF C#工作和搜索報告,並發現了一個很好的報告,也很容易,發現這個link並使用它。所以我試圖使用它,請檢查我的代碼,指定的視覺已經是另一個視覺或組件目標的根子

在我EmployeeProfileWindow在打印按鈕,

private void btnprintviolation_Click(object sender, RoutedEventArgs e) 
{ 
    ReportViolationWindow NewReportViolationWindow = new ReportViolationWindow(); 
    //Windows.Add(NewReportViolationWindow); 
    GlobalVar.ViolationEmpNum = txtdispid.Text; 
    GlobalVar.ViolationRefNumToPrint.Clear(); 
    for (int i = 0; i < lvviolations.Items.Count; i++) 
    { 
     GlobalVar.ViolationRefNumToPrint.Add(((EmpViolationObject)lvviolations.Items[i]).VioRefNum); 
    } 
    NewReportViolationWindow.Show(); 
} 

所以,如果有的話我按一下按鈕,將出現一個新的窗口名稱NewReportViolationWindow。我將在Template文件夾中將其作爲開源示例中的內容進行復制或編輯。我創建了我的報告,名稱爲ReportViolation

現在,這是NewReportViolationWindow後面的代碼。

ReportDocument reportDocument = new ReportDocument(); 
string ats = new DirectoryInfo(Environment.CurrentDirectory).Parent.Parent.FullName; 
StreamReader reader = new StreamReader(new FileStream(ats.ToString() + @"\Template\ReportViolation.xaml", FileMode.Open, FileAccess.Read)); 
reportDocument.XamlData = reader.ReadToEnd(); 
reportDocument.XamlImagePath = Path.Combine(ats.ToString(), @"Template\"); 
reader.Close(); 


DateTime dateTimeStart = DateTime.Now; // start time measure here 
List<ReportData> listData = new List<ReportData>(); 
//foreach (string item in GlobalVar.ViolationRefNumToPrint) 
for (int i = 0; i < 5 ; i++) 
{ 
    ReportData data = new ReportData(); 

    data.ReportDocumentValues.Add("PrintDate", DateTime.Now); 
    data.ReportDocumentValues.Add("EmpIDNum", NewIDNumber.ToString()); 
    data.ReportDocumentValues.Add("EmpName", NewEmpName.ToString()); 
    data.ReportDocumentValues.Add("EmpPosition", NewPosition.ToString()); 

    //data.ReportDocumentValues.Add("VioRefCode", item.ToString()); 
    listData.Add(data); 
} 

XpsDocument xps = reportDocument.CreateXpsDocument(listData); 
documentViewer.Document = xps.GetFixedDocumentSequence(); 

// show the elapsed time in window title 
Title += " - generated in " + (DateTime.Now - dateTimeStart).TotalMilliseconds + "ms"; 

} 
catch (Exception ex) 
{ 
    // show exception 
    MessageBox.Show(ex.Message + "\r\n\r\n" + ex.GetType() + "\r\n" + ex.StackTrace, ex.GetType().ToString(), MessageBoxButton.OK, MessageBoxImage.Stop); 
} 

現在,當我運行我的應用程序,然後單擊打印按鈕。有時在首先它會打開NewReportViolationWindow,並沒有錯誤,但是當我嘗試關閉報表或再次單擊該按鈕,它會給出一個消息,

指定的可視已經是另一個視覺或根的孩子該組件目標

下面是錯誤的圖像,

enter image description here

我認爲這個問題是,當我打電話打印報告,打印後面的代碼按鈕,嗯,任何人都可以?請... :)

2ND編輯

關於你的問題:

  • 你說,該報告的窗口通常沒有錯誤打開了 第一次,但不是之後呢?

是正確的..

  • 是否存在一些在 ReportViolationWindow正在使用的任何共享資源?

對不起,我沒有任何想法因爲我只是跟着開源中的示例。

  • 你是如何處理/處理關閉的 ReportViolationWindow

到目前爲止,我還沒有正確關閉我的ReportViolationWindow的代碼。當我點擊關閉按鈕,就是這樣,對不起。 :(

  • 您保留任何其他引用該ReportViolationWindow 實例?

號我所知道的。

回答

1

在將視覺添加到新的父對象之前,您必須從視覺對象中「分離」視覺對象 - 原因主要是由於渲染和合成引擎的工作方式;如果原始父元素不知道它不再負責渲染該孩子,並且WPF允許您將其附加到另一個父級,則在最好的情況下,您將呈現重複的Visual元素,並且在最壞的情況下場景,你可能會陷入無限循環!

因爲責任是父元素上添加/刪除孩子,你就需要在父水平要麼 RemoveLogicalChildRemoveVisualChild(或理想調用處理這個問題,通常,從原來除去項目本身的ItemsSource並將其添加到新)

編輯:技術上,第一款是真的,但我不認爲第二個適用於你......通過源代碼在WpfReports on CodePlex尋找ReportPaginator課後,我注意到以下幾點:

  • 最新的版本與你的不同,我相信:#行不太匹配。可能這是一個已修復的錯誤?
  • (Nitpicky)我不是這個代碼(報表引擎)的結構迷... ArrayLists?單行if/else/return語句?

現在,你的實際問題:

  • 你說,該報告的窗口通常沒有錯誤打開了第一次,但不是之後呢?

  • ReportViolationWindow中是否有任何共享資源正在使用?

  • 您如何處理/處理ReportViolationWindow的關閉?

  • 你保持任何其他引用這個ReportViolationWindow實例?

有一件事我會嘗試,只是爲了看看,如果錯誤仍然存​​在,正在申報窗口NewReportViolationWindow型是創造它(EmployeeProfileWindow)一個成員變量,而不是這樣的:

private void btnprintviolation_Click(object sender, RoutedEventArgs e) 
{ 
    ReportViolationWindow NewReportViolationWindow = new ReportViolationWindow(); 

嘗試類似:

ReportViolationWindow _reportViolationWindow; 
private void btnprintviolation_Click(object sender, RoutedEventArgs e) 
{ 
    if(_reportViolationWindow != null) 
    { 
     _reportViolationWindow.Close(); 
     _reportViolationWindow = null; 
    } 
    _reportViolationWindow = new ReportViolationWindow(); 
+0

你怎麼能給我一個例子如何和我該做什麼。謝謝 –

+0

呃...我很抱歉,很有可能它不是*你的代碼 - 我現在只是通過「ReportPaginator」類的源代碼來尋找...... – JerKimball

+0

在這種情況下,很可能你正在處理報告中的一個錯誤發動機本身;既然它是開源的,我會建議抓住源代碼並調試到代碼中......或者詢問創建者。 – JerKimball

0

好吧,我可能有一個解決方案。如果我改變上述第二代碼片段,以

代碼塊

ContainerVisual smallerPage = new ContainerVisual(); 

DrawingVisual pageVisual = page.Visual as DrawingVisual; 

if (pageVisual != null && pageVisual.Parent != null) 

{ 

ContainerVisual parent = pageVisual.Parent as ContainerVisual; 

parent.Children.Remove(pageVisual); 

} 

smallerPage.Children.Add(page.Visual); 

看來工作。請給出意見。我很想知道是否有更好的方法。這看起來像一個黑客。

+0

在文件CodeReason.Reports.VS200 \ ReportPaginator.cs第472行 –