Son Of A-

Son Of A-

Grrrr.  I love wasting time.  Take today for example.  I just spent the last 30 minutes trying to figure out why DataContext on the yellow break point line below was null:

XAML To XPS To The Printer

The following was created from Feng Yuan's blog post and a ton of great info at this guy's blog.

OK.  Start a new WPF Application.  By default you start with a window so I'll use that for the example.  Open up Window1.xaml in the designer and use this xaml for the window:


    
        
    

1
2
3
4
5
6
7
8
9
10
11
12

You should see something like this in the designer:

Now create a FlowDocument, this will be VERY simple. Let's add a new item, and select Flow Document:

Here's the XAML we'll use:


    
        
        	
        
    

1
2
3
4
5
6
7
8

Now we're gonna wire up the PrintButton_Click method. We'll first need to add references to ReachFramework and System.Printing.  Here's the code for the PrintButton_Click method:

//LOAD XAML
var pathToFlowDoc = @"C:\Users\Chris\Documents\Visual Studio 2008\"
+ @"Projects\XamlXpsPrinting\XamlXpsPrinting\FlowDocument1.xaml";

var xamlDoc = XamlReader.Load(File.OpenRead(pathToFlowDoc));

//CREATE XPS
var xpsOutputFile = @"C:\HelloXPS.xps";
using (var container = Package.Open(xpsOutputFile, FileMode.Create))
using (var xpsDoc = new XpsDocument(container, CompressionOption.Maximum))
{
  var policy = new XpsPackagingPolicy(xpsDoc);
  var xsm = new XpsSerializationManager(policy, false);
  var paginator = ((IDocumentPaginatorSource)xamlDoc).DocumentPaginator;
  xsm.SaveAsXaml(paginator);
}

//PRINT XPS
var printServer = new LocalPrintServer();
var defaultQueue = LocalPrintServer.GetDefaultPrintQueue();
defaultQueue.AddJob("Hello XPS", xpsOutputFile, false);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

At this point we can hit F5, click the Print button and see the document print.  However, I'm on week 2 of Windows 7, making the leap from XP, so when I hit the Print button I got this:

I forgot about all the Run As Administrator crap that's not in XP, so I had to run Visual Studio as administrator before this would work:

To change it so VS always runs as administrator you can right click the short cut, select properties, on the Shortcut tab click the Advanced... button and check the "Run As Administrator" checkbox:

Now with that nonsense out of the way, run the app hit the print button and it should send the doc to your default printer.  Here's the VS2008 Solution.

Registry Hack for XPSViewer and Windows XP SP3

I've always wondered why, whenever I try opening an XPS document, that Firefox opens and I'm prompted with the Save As dialog, selecting instead to open with the XPSViewer, all it does is prompt me to save, and the loop continues.  Since I almost never have a need to open an XPS document I've ignored the issue.  Today however, I'm neck deep in XPS and needed to fix the prob.  It has to do with Windows XP SP3.  Apparently if you've already installed the .NET frameworks through 3.5 SP1, the only fix is the following change to the registry:

Karl Snooks comment on an XPS Team Blog post about the issue showed me the way.  Previously, the value of the command node was the full path to XPSViewer.exe located in %WINDIR%\system32\XPSViewer folder.  Thanks Karl!

For an amazingly detailed explanation of the core problem and a fix(if you don't already have it installed), see Rafael Rivera Jr's post: Microsoft: I fixed the XPS Essentials Pack for you. It took 10 minutes.

Database Diagram Support Objects Cannot Be Installed...

This is mostly a reminder to myself.  Ever get this after restoring a sql server backup and trying to create a diagram:

I got a script from here that looks like this(if your database is named pantera):

 

EXEC sp_dbcmptlevel 'pantera', '90';
go
ALTER AUTHORIZATION ON DATABASE::pantera TO "sa"
go
use [pantera]
go
EXECUTE AS USER = N'dbo' REVERT
go
1
2
3
4
5
6
7
8

After running the script I can now add diagrams.

What Is So Bad About Html That We Have To Create a RedirectButton Control?

Don't get me wrong, I like Scott Mitchell, and I've beed subscribed to his blog for some time.  However, a recent article of Scott's on 4guysfromrolla goes overboard with abstracting a simple html concept and making it way more complicated than it needs to be by masking html simplicity with asp.net complexity.

The RedirectButton Control

Essentially it writes out the html needed to generate an input button that uses location.href to redirect one's browser to a different url.  Here's the asp.net code that uses the control:


1
2
3

And when the page is rendered, the client is delivered this html:


1
2

Redirecting With Old School Html

The same code can be written using POH(Plain Ol' Html) like this:

 
Author: , 0000-00-00