locked
FlowDocumentReader Expression and VS Problems/Questions RRS feed

  • Question

  • I have a WPF program with numerous FlowDocumentReaders.  The window I was working on today had one that was more elaborate than those I've been creating. 

    I started creating it in XAML but realized while it showed up fine in the reader, when I went to print it, it printed a blank page.

    So I decided to create it in code-behind which I've had success with on my other flowDocs, but this one needed images and come hell or high water I couldn't get the images to display.  I asked for help on the WPF forums, but didn't really get a solution.

    Here is the code-behind I was using...

    private void createFlowDoc(object sender, System.Windows.RoutedEventArgs e)
    		{
    			BlockUIContainer bc = new BlockUIContainer();
    			
    			Image myImg = new Image();
    			myImg.Source = new BitmapImage(new Uri(this.myImageSource.Text, UriKind.Relative));
    			myImg.Width = 50;
    			myImg.Height = 50;
    			myImg.Stretch = Stretch.Fill;
    			myImg.Margin = new Thickness(0, 0, 0, 0);
    			
    			Grid grid = new Grid();
    			grid.Width = 50;
    			grid.Height = 50;
    			grid.Background = new SolidColorBrush(Colors.Green);
    			grid.Children.Add(myImg);
    			
    			StackPanel sp = new StackPanel();
    			sp.Width = 200;
    			sp.Height = 200;
    			sp.Orientation = Orientation.Vertical;
    			sp.HorizontalAlignment = HorizontalAlignment.Left;
    			sp.Background = new SolidColorBrush(Colors.Red);
    			sp.Children.Add(grid);
    			
    			bc.Child = sp;
    			this.myFlowDoc.Blocks.Add(bc);
    		}
    
    		private void createImageOutsideFlow(object sender, System.Windows.RoutedEventArgs e)
    		{
    			Image myImg = new Image();
    			myImg.Source = new BitmapImage(new Uri(this.myImageSource.Text, UriKind.Relative));
    			myImg.Width = 33;
    			myImg.Height = 33;
    			myImg.Stretch = Stretch.Fill;
    			myImg.Margin = new Thickness(295, 120, 0, 0);
    			this.LayoutRoot.Children.Add(myImg);
    		}
    
    


    The second method was really just a tester to see if the image would render, which it did.  The grid and stackpanel show fine, the image just won't render.

    Anyways, I decided to go back to Xaml and spent hours adding one element at a time, running the project, trying to print until I found out what was causing the problem.  I couldn't find a thing until I opened the project in Viusal C# and built it before publishing.  Once built in VC# it went back to printing blank pages again. 

    So I have 2 questions...

    1.  How do I add an image to a FlowDocument from code-behind? 

    2.  What is it that Visual C# does to my project at run time that would be different from Expression and can I avoid it in the future?  I'm sort of addicted to switching between the two programs for designing and working with my database.

    ~Christine

    Tuesday, November 22, 2011 11:48 PM

Answers

  • You add an image in the exact way you're adding an image... sort of. :-)  My guess is the URI you're using doesn't map correctly in code-behind. For example, here's how my image source declaration looks:

    Image myImg = new Image();
    myImg.Source = new BitmapImage(new Uri("pack://siteoforigin:,,,/WP_000010.jpg", UriKind.Absolute));
    

    I grabbed an image from my phone and put it in the root directory of the application. I got the URI by recreating the layout in XAML first and copying it from there (cheating slightly, but it works).

    As for the differences between VS and Blend... they are many and varied. For example, Blend only lets you compile in Debug mode. There are tons of low-level details like caching that have more to do with how the programs operate in general than how they handle XAML specifically. If you have any specific questions, let me know and I'll get them to the right people.

    • Marked as answer by Christine L. _ Thursday, November 24, 2011 5:20 PM
    Wednesday, November 23, 2011 5:54 PM

All replies

  • You add an image in the exact way you're adding an image... sort of. :-)  My guess is the URI you're using doesn't map correctly in code-behind. For example, here's how my image source declaration looks:

    Image myImg = new Image();
    myImg.Source = new BitmapImage(new Uri("pack://siteoforigin:,,,/WP_000010.jpg", UriKind.Absolute));
    

    I grabbed an image from my phone and put it in the root directory of the application. I got the URI by recreating the layout in XAML first and copying it from there (cheating slightly, but it works).

    As for the differences between VS and Blend... they are many and varied. For example, Blend only lets you compile in Debug mode. There are tons of low-level details like caching that have more to do with how the programs operate in general than how they handle XAML specifically. If you have any specific questions, let me know and I'll get them to the right people.

    • Marked as answer by Christine L. _ Thursday, November 24, 2011 5:20 PM
    Wednesday, November 23, 2011 5:54 PM
  • You're a Star!!! Thank you.

    This worked for me...

    myImg.Source =

    new BitmapImage(new Uri("pack://application:,,,/" + this.myImageSource.Text, UriKind.Absolute));

    I was a bit thick on the Absolute vs. Relative thing for a few failed tries, but I finally got it.

     As for the printing blank pages when built in VC#... well I thought I had narrowed it down to that, but it still occurs sometimes in Blend. For the life of me I can't figure out why and it's gotten me madder than a wet cat trying to understand it. There really is no rhyme or reason for it. Sometimes it prints sometimes it's a blank page.

    I'm using the FlowDocumentReader.Print(); method to print. I've tested printing to XPS, Adobe PDF, my actual printer...

    My latest theory was unless you actually focused something on the actual FlowDocument it would print a blank page, but that is not always true either. So I'm stumped.

    For a temporary fix I'm using PrintDialog on the actaul FlowDocument. But perhaps you could tell me what I'm doing wrong or what I'm just not getting. I'm not all that bright ya know.

    ~Christine

     

    Wednesday, November 23, 2011 11:31 PM