This is what’s known as a “on the train” project 🙂
I wanted to get a version of Kaxaml that understood WPF 4.0 Beta 2 and so I spotted that the source was on CodePlex and thought I’d set about downloading it and seeing if I could get something going.
I should probably try and contribute to the project rather than just writing this blog-post but as far as I can tell the source on CodePlex isn’t the most recent version of Kaxaml as it doesn’t seem to have the Silverlight 2 support that the most recent version from the website looks to have.
So, rather than contribute to the wrong code-base I took some simple steps that you might be able to replicate to get a Kaxaml for WPF 4 up and running. Took me about 10 mins on the train.
- Download and unzip the Kaxaml source using the link above.
- Download and unzip the source for SharpDevelop from this site.
- In the SharpDevelop code, navigate to the src\Libraries\ICSharpCode.TextEditor folder and open up the solution with VS 2010.
- Use the properties page on the ICSharpCode.TextEditor project to switch the runtime version to .NET 4.0 ( or .NET 4.0 Client Profile if you prefer ). Note – I took the tests project out of the solution, it’s up to you whether you want to run the tests.
- Build the release version of the assembly.
- Open up the Kaxaml.sln file in VS2010.
- Visit all the projects except the setup project and change their .NET Framework version to 4.0.
- Visit the Kaxaml project itself and update its reference to ICSharpCode.TextEditor to point to the version of the assembly that you built back in step 5. That will be in the top level SharpDeveloper folder called bin as the output settings of the project put it there.
- Build the release version of the project.
- You’ll hit a number of build problems which I fixed as below;
- KaxamlTextEditor.xaml, line 26 – I removed the UseAntiAliasFont on the TextEditorControl.
- KaxamlTextEditor.xaml.cs line 155 – not entirely sure that I got this one correct as I couldn’t execute the functionality yet but I went with;
private static void LinePositionChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args) { if (obj is KaxamlTextEditor) { KaxamlTextEditor owner = (KaxamlTextEditor)obj; owner.TextEditor.ActiveTextAreaControl.Caret.Position = new TextLocation((int)args.NewValue, owner.TextEditor.ActiveTextAreaControl.Caret.Position.Y); } }
- KaxamlTextEditor.xaml.cs line 691 – I simply removed the return value here as it seems that the call to FormatLine used to return an integer but seems like it doesn’t any more but the integer wasn’t being used anywhere. Code now reads;
TextEditor.Document.FormattingStrategy.FormatLine( TextEditor.ActiveTextAreaControl.TextArea, currentLineNr, TextEditor.Document.PositionToOffset( TextEditor.ActiveTextAreaControl.TextArea.Caret.Position), ch);
- KaxamlTextEditor.xaml.cs line 1164 – this is the function SetSelection and its only problem is that its working in terms of Point whereas the control now seems to work in terms of TextLocation so it seemed like a quick and easy fix to change it to;
public void SetSelection(int fromOffset, int toOffset, bool suppressSelectionChangedEvent) { try { TextLocation from = TextEditor.Document.OffsetToPosition(fromOffset); TextLocation to = TextEditor.Document.OffsetToPosition(toOffset); if (suppressSelectionChangedEvent) { TextEditor.ActiveTextAreaControl.TextArea.SelectionManager.SelectionChanged -= new EventHandler(SelectionManager_SelectionChanged); TextEditor.ActiveTextAreaControl.SelectionManager.SetSelection(from, to); TextEditor.ActiveTextAreaControl.TextArea.SelectionManager.SelectionChanged += new EventHandler(SelectionManager_SelectionChanged); } else { TextEditor.ActiveTextAreaControl.SelectionManager.SetSelection(from, to); } } catch { } }
- KaxamlTextEditor.xaml.cs line 1190 – this is the function SelectLine which I changed to;
public void SelectLine(int lineNumber) { try { TextLocation startOfLine = new TextLocation(0, lineNumber); TextEditor.ActiveTextAreaControl.SelectionManager.SetSelection( startOfLine, new TextLocation(0, lineNumber + 1)); TextEditor.ActiveTextAreaControl.Caret.Position = startOfLine; } catch { } }
Note – this just comes from 5 mins of hacking around.I’ve never used that SharpDevelop control before but these felt like semi-sensible changes to make based on the code that Kaxaml seemed to have previously.
With that in place, the code compiles and runs and I can type a bit of WPF 4.0 Beta 2 XAML into it;
but – a couple of other things I’d want to do here. One is a pet peeve in that I’d always like to be able to collapse that area on the left hand side so I thought I’d drop in a quick Expander. So, I opened up MainWindow.xaml. In there is a Grid which contains;
- Grid
- MenuView
- PluginView
- StatusView
- DocumentsView
and I just wrapped a Grid within an Expander around the first 4 of those controls ( the DocumentsView is in the other grid column on the right ). I set the Expander so that it had an ExpandDirection of “Right” and set IsExpanded to be “True” so now I have that change;
But there’s still ( at least ) a couple of problems with IntelliSense. One is that I kept getting an unhandled exception around line 1067 of KaxamlTextEditor.xaml.cs where it sorts an ArrayList. The exception was around the item type not implementing IComparable. The item type is a type called XmlCompletionData and it already has a CompareTo implementation so I simply went to that type and added the IComparable interface to its declaration. Seemed easy.
The other aspect is that I think the IntelliSense that’s being displayed will be out of date with respect to WPF 4.0 Beta 2. For instance, trying to use the new UseLayoutRounding attribute shows that it’s not in the IntelliSense which looks to be driven by a file called XamlPresentation2006.xsd in the Schemas folder.
Fortunately, that’s when the train pulled into the station – I’ve not yet figured out whether there’s a replacement XSD that I can drop into Kaxaml to tell it about XAML in WPF 4.0 because I’ve a sneaky suspicion that the VS editor moved away from being XSD driven and so maybe that XSD doesn’t exist any more. I’ll do a bit of research and update the post if I find out what the right thing to do is but, for the meantime, I’ve got a “working” Kaxaml version on WPF 4.0 for demos and so on.