Got an email from Kedra A.– a developer/admin in Austin who wanted to know … if the cXML validator I put up this weekend could be done… using WPF. Why Kedra wanted this in WPF I have no clue – but apparently all of their apps are now being done in… you got it – WPF. Kedra tried building the project by just grabbing the various functions and subs and copying them into a WPF app and she couldn’t get it to work
I told her I’d take a look at it and see why.
So here it is. Mission accomplished. All done.
Okay – I won’t be cruel .. because there are some code differences between any WPF and a normal .NET application.
Basically there are very very few things in this app that won’t work in WPF. But there are a few which, for various reasons need to be modified.
Primarilly this is the Richtextbox control and the OpenFileDialog/Showdialog, and these I found out after a bit of digging on the web … can be tricky.
So Kedra – don’t feel bad. This is intermediate level to upper level stuff and it involves a very very new (for most people) technology.
Lets take a look at the OpenFileDialog code in the original application and the wpf version here.
If you notice the OpenFileDialog in the new code needs to instantiate the OFDialog as a System.Windows.Forms OpenFileDialog instead of the default that comes with WPF. Why’d I do that?
'Set up the OpenFileDialog to get the name of the file for loading.
Dim OFDialog As New System.Windows.Forms.OpenFileDialog
With OFDialog
.FileName = vbNull 'Clear out previous filename
.FileName = "*.xml"
Simple … the OFDialog that comes with System.Windows.Forms.OpenFileDialog is an easier to use – and has some richer features than the one that’s baked into WPF which is Microsoft.Win32.OpenFileDialog and has all the functionality of a sockpuppet. Okay it’s not that bad… at least not until you try to use the ShowDialog to go with it.
So that’s why… you can see I did this:
If OFDialog.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
If .FileNames.GetUpperBound(iCount) < 1 Then
'File selected so put it in the ComboBox
In setting the OFDialog box to the Windows.Forms.OpenFileDialog in the top snippet – I can now use the System.Windows.Forms.DialogResult and it’s various fun toys. I also wind up with a much nicer OpenFileDialogbox.
I do need to use Import System.Windows.Forms; and yes… this does mean I’m refactoring but it’s a small price on code like this which is a quicky.
But … what other differences are there that you need to be aware of?
Microsoft.Win32 RichTextboxes differ greatly from their Windows.Forms brothers. As a rule most of the WPF controls are very similar – and at the same time very different from their cousins. System.Windows.Controls.Richtextbox and System.Windows.Forms.RichTextBox are close – but no cigar. If I want to change the text in a RichTextbox in WPF it’s a much different process than it’s Forms cousin. Check out the links above – and compare the code in the link below the the code in the previous post (it’s the same code – just updated for the WPF, and I removed a couple of annoyances I discussed last time).
Side Note - Kedra also wanted to thank me for doing my samples in VB.NET since its apparently hard to find code samples in VB.NET. I agree with her for some reason there’s a hundred samples out there and the conversion I also agree is pretty simple… but the fact is there are still a lot of new programmers out there who need some time with the code before their confidence is strong enough to jump into those waters. (They will get there – we all do – they just need to see shore for a bit longer while they work on their swimming.) I will continue to do as many samples as i can in VB, but will also try to put them out in C# as well. |
1 comment:
cXML Ariba Protocol - Vurbis Interactive used cxml ariba protocol created by ariba in 1999. It is based on XML and provides formal XML schemas for business transactions.
cXML Ariba
Post a Comment