//_pp is a user control with the map on it I print private void btnOK_Click(Object sender, RoutedEventArgs e) { _LayerComplete = 0; _LayerCount = 0; //get map and legend prep'd _pp = _Parent.Printpage; _pp.setMap(_Map); _lgnd = new Legend(); _lgnd.ShowOnlyVisibleLayers = true; _lgnd.LayerItemsMode = Legend.Mode.Tree; _lgnd.Map = _pp.mapPrint; (_pp.LayoutRoot.FindName("txtTitle") as TextBlock).Text = txtTitle.Text; (_pp.LayoutRoot.FindName("txtDate") as TextBlock).Text = "Created: " + DateTime.Now.ToShortDateString(); //drop tiled services since they dont have a legend with symbology List<string> layIDs = new List<string>(); foreach (var lyr in _pp.mapPrint.Layers) { if (!(lyr is ArcGISTiledMapServiceLayer)) { layIDs.Add(lyr.ID); } } layIDs.Reverse(); _lgnd.LayerIDs = layIDs.ToArray(); foreach (LayerItemViewModel livm in _lgnd.LayerItems) { livm.PropertyChanged += livm_PropertyChanged; //never happens LayerItems is empty } } void livm_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) { MessageBox.Show(e.PropertyName + " changed"); //real code should see which prop changed..if B }
private void btnOK_Click(Object sender, RoutedEventArgs e) { _LayerComplete = 0; _LayerCount = 0; //get map and legend prep'd _pp = _Parent.Printpage; _pp.setMap(_Map); _pp.Visibility = Visibility.Visible; (_pp.LayoutRoot.FindName("txtTitle") as TextBlock).Text = txtTitle.Text; (_pp.LayoutRoot.FindName("txtDate") as TextBlock).Text = "Created: " + DateTime.Now.ToShortDateString(); _lgnd = new Legend(); _lgnd.ShowOnlyVisibleLayers = true; _lgnd.LayerItemsMode = Legend.Mode.Tree; _lgnd.Map = _pp.mapPrint; _lgnd.Refreshed += _lgnd_Refreshed; _doc = new PrintDocument(); _doc.PrintPage += doc_PrintPage1; _doc.EndPrint += doc_EndPrint; _doc.Print("AweMap_Export"); } void _lgnd_Refreshed(object sender, Legend.RefreshedEventArgs e) { e.LayerItem.PropertyChanged += (s,evnt) => { if (!(s as LayerItemViewModel).IsBusy) { System.Diagnostics.Debug.WriteLine((s as LayerItemViewModel).Layer.ID + " is no longer busy"); _LayerComplete++; } else { System.Diagnostics.Debug.WriteLine((s as LayerItemViewModel).Layer.ID + " is busy"); } }; //have also tried this with no luck - //if (!e.LayerItem.IsBusy) //{ // System.Diagnostics.Debug.WriteLine(e.LayerItem.Layer.ID + " is no longer busy"); // _LayerComplete++; //} //else //{ // System.Diagnostics.Debug.WriteLine(e.LayerItem.Layer.ID + " is busy"); //} } void doc_PrintPage1(object sender, PrintPageEventArgs args) { System.Diagnostics.Debug.WriteLine("start of page1 print)"); _pp.Width = args.PrintableArea.Height; _pp.Height = args.PrintableArea.Width; _pp.UpdateLayout(); if (rbLand.IsChecked.GetValueOrDefault(true)) { //rotate to landscape TransformGroup transformGroup = new TransformGroup(); transformGroup.Children.Add(new RotateTransform() { Angle = 90 }); transformGroup.Children.Add(new TranslateTransform() { X = args.PrintableArea.Width }); _pp.RenderTransform = transformGroup; } //swap handler for 2nd page w/ legend control _doc.PrintPage -= doc_PrintPage1; _doc.PrintPage += doc_PrintPage2; args.PageVisual = _pp; args.HasMorePages = true; } void doc_PrintPage2(object sender, PrintPageEventArgs args) { System.Diagnostics.Debug.WriteLine("start of page2 print)"); //drop tiled services since they dont have a legend with symbology List<string> layIDs = new List<string>(); foreach (Layer lyr in _pp.mapPrint.Layers.Where(t => !(t is ArcGISTiledMapServiceLayer))) { layIDs.Add(lyr.ID); _LayerCount++; } layIDs.Reverse(); _lgnd.LayerIDs = layIDs.ToArray(); //code below doesn't seem to change outcome, although if I don't set _lgnd.LayerIDs above the LayerItems will be null in the loop below.. //foreach (Layer lyr in _pp.mapPrint.Layers.Where(t => !(t is ArcGISTiledMapServiceLayer))) //{ // _LayerCount++; // LayerItemViewModel livm = new LayerItemViewModel(lyr); // // livm.PropertyChanged += livm_PropertyChanged; // _lgnd.LayerItems.Add(livm); //} int lcnt = 0; int lcomp = -1; while (lcomp < lcnt) { // this results in an endless loop as _LayerComplete is never updated lcnt = _LayerCount; lcomp = _LayerComplete; } args.PageVisual = _lgnd; args.HasMorePages = false; }
private void btnOK_Click(Object sender, RoutedEventArgs e) { busy.IsBusy = true; _LayerComplete = 0; _LayerCount = 0; //get map and legend prep'd _pp = _Parent.Printpage; _pp.setMap(_Map); _pp.Visibility = Visibility.Visible; (_pp.LayoutRoot.FindName("txtTitle") as TextBlock).Text = txtTitle.Text; (_pp.LayoutRoot.FindName("txtDate") as TextBlock).Text = "Created: " + DateTime.Now.ToShortDateString(); _lgnd = new Legend(); _ppl = _Parent.PrintLegend; _ppl.LayoutRoot.Children.Add(_lgnd); _lgnd.ShowOnlyVisibleLayers = true; _lgnd.LayerItemsMode = Legend.Mode.Tree; _lgnd.Map = _pp.mapPrint; _lgnd.Refreshed += _lgnd_Refreshed; _lgnd.Background = new SolidColorBrush(Colors.White); //overwrite default gradient - smaller output size //drop tiled services since they dont have a legend with symbology List<string> layIDs = new List<string>(); foreach (Layer lyr in _pp.mapPrint.Layers.Where(t => !(t is ArcGISTiledMapServiceLayer))) { layIDs.Add(lyr.ID); _LayerCount++; } layIDs.Reverse(); _lgnd.LayerIDs = layIDs.ToArray(); _doc = new PrintDocument(); _doc.PrintPage += doc_PrintPage1; _doc.EndPrint += doc_EndPrint; _doc.Print("AweMap_" + txtTitle.Text); } void _lgnd_Refreshed(object sender, Legend.RefreshedEventArgs e) { //see if LIVM that fired refreshed is no longer busy; increment layer complete counter e.LayerItem.PropertyChanged += (s,evnt) => { if ( (!(s as LayerItemViewModel).IsBusy) && evnt.PropertyName.ToLower() == "isbusy") { _LayerComplete++; } }; } void doc_PrintPage1(object sender, PrintPageEventArgs args) { _pp.Width = args.PrintableArea.Height; _pp.Height = args.PrintableArea.Width; _pp.UpdateLayout(); if (rbLand.IsChecked.GetValueOrDefault(true)) { //rotate to landscape TransformGroup transformGroup = new TransformGroup(); transformGroup.Children.Add(new RotateTransform() { Angle = 90 }); transformGroup.Children.Add(new TranslateTransform() { X = args.PrintableArea.Width }); _pp.RenderTransform = transformGroup; } args.PageVisual = _pp; //see if user selected to include legend:D if (chkLegend.IsChecked.GetValueOrDefault(true)) { //swap handler for 2nd page w/ legend control _doc.PrintPage -= doc_PrintPage1; _doc.PrintPage += doc_PrintPage2; args.HasMorePages = true; } else { args.HasMorePages = false; } } void doc_PrintPage2(object sender, PrintPageEventArgs args) { _pp.Width = args.PrintableArea.Height; _pp.Height = args.PrintableArea.Width; _lgnd.Width = _ppl.Width - 50; _lgnd.Height = _ppl.Height - 50; if (_LayerComplete < _LayerCount) { System.Diagnostics.Debug.WriteLine("Waiting for Legend..."); System.Threading.Thread.Sleep(1000); args.PageVisual = null; args.HasMorePages = true; } else { args.PageVisual = _lgnd; args.HasMorePages = false; } }