DemoWindow.xaml.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using FastReport;
  2. using FastReport.Utils;
  3. using System.Windows;
  4. namespace WpfDemo
  5. {
  6. public partial class DemoWindow : Window
  7. {
  8. private Report report;
  9. public string Version => Config.Version;
  10. public DemoReports DemoReports { get; }
  11. public DemoWindow()
  12. {
  13. InitializeComponent();
  14. // the designer will use embedded preview (same window as the designer)
  15. Config.DesignerSettings.EmbeddedPreview = true;
  16. // show how fast is FastReport
  17. Config.ReportSettings.ShowPerformance = true;
  18. // override these to not use standard progress form
  19. Config.ReportSettings.StartProgress += (s, e) => { };
  20. Config.ReportSettings.Progress += (s, e) => previewControl.ShowStatus(e.Message);
  21. Config.ReportSettings.FinishProgress += (s, e) => { };
  22. // warm up the Roslyn (used in net60 target)
  23. Config.CompilerWarmup();
  24. // these features are available in net6.0-windows target
  25. #if NETCOREAPP
  26. // do not compile a report if it has no user script (works faster)
  27. Config.CompilerSettings.ReflectionEmitCompiler = true;
  28. // use RoslynPad as a code editor
  29. FastReport.WPF.RoslynPad.SyntaxEditor.Register();
  30. #endif
  31. // shared folder for Map object
  32. FastReport.Map.MapObject.ShapefileFolder = Folders.MapsFolder;
  33. // populate reports list
  34. DemoReports = new DemoReports(Folders.ReportsFolder + "reports.xml");
  35. this.DataContext = DemoReports;
  36. this.Closing += (s, e) =>
  37. {
  38. if (previewControl.IsAsyncReportRunning)
  39. {
  40. // if async report is running, abort the report. Do not close the window
  41. previewControl.Report.Abort();
  42. e.Cancel = true;
  43. return;
  44. }
  45. DisposeReport();
  46. };
  47. }
  48. private void reportsTree_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
  49. {
  50. // previous report is not finished yet
  51. if (previewControl.IsAsyncReportRunning)
  52. return;
  53. var reportItem = reportsTree.SelectedItem as ReportItem;
  54. if (reportItem != null)
  55. {
  56. // in case we are in the UI layout phase, we need this call to be dispatched (case: expand the "Dialogs" category => Invalid Op exception)
  57. Dispatcher.BeginInvoke(() => RunReport(Folders.ReportsFolder + reportItem.FileName));
  58. }
  59. }
  60. private void RunReport(string fileName)
  61. {
  62. // dispose previous report
  63. DisposeReport();
  64. report = new Report();
  65. report.Load(fileName);
  66. SampleData.RegisterData(report);
  67. // show description
  68. description.SetDescription(report.ReportInfo.Description);
  69. // prepare report async way (you may use preview while report is preparing). A bit flickering though
  70. report.PrepareAsync(previewControl);
  71. // non-async way (prepare then show):
  72. //report.WpfPreview = previewControl;
  73. //report.Show();
  74. }
  75. private void DisposeReport()
  76. {
  77. // disposing a report may be necessary if it has dialog forms inside. These forms may prevent WPF app to close
  78. report?.Dispose();
  79. report = null;
  80. }
  81. }
  82. }