|
|
|
| Saturday, October 24, 2009 |
XAML again?
Posted: 4:46:00 PM
|
Back in January of last year, I started work on the Six Minutes To Release website using Silverlight. It... didn't go so well. People couldn't use it on Macs, and there were other fugly things about it I didn't like. I ended up abandoning the project and coding it in straight ASP.Net.
Turns out that the XAML experience I got from working on it the first time through the SMTR website came in handy. The Rawr project is using Silverlight for its next major release, Rawr 3. Having taken on the protection paladin module, I was able to write a needed piece for that module to work in Rawr 3 in about 3 days on and off. Of course, the stumbling of not having touched it in nearly 2 years took me some time to get past, but once I did it was smooth sailing.
There's a few things I don't like about it, though. Rawr is going to release a Silverlight web release and a WPF application release. The problem with the two releases is that the XAML used in each is not necessarily compatible with each other in all cases. This results in being unable to use certain features, such as StringFormatting. We also have to use a cheap preprocessing approach to be able to compile the same XAML file in both places.
Is it me, or could Microsoft have done a better job of streamlining the two versions? Why even have two versions of XAML? It seems like a lot of extra work for similar functionality that could be done all in one place.
In any case, I've put a lot on my plate, and I've devoted this weekend to getting it out. Up next, SMTR's new raid performance reporting, which is almost done. Should be fun.Labels: Coding, Gaming, Rawr, Silverlight, Six Minutes To Release, World of Warcraft, WPF, XAML
|
| Monday, December 10, 2007 |
Databinding to a XAML FlowDocument
Posted: 11:25:00 PM
|
I couldn't find a full example of this, I had to put bits and pieces together before I got this working.
Let's say you have a datasource, and in that datasource, one of the datamembers is a string that contains some FlowDocument XAML. For example:
<Section xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<Paragraph>This is a test.</Paragraph>
</Section>
Unfortunately, there's no way to directly bind a string to a FlowDocument object. However, XAML databinding allows the use of custom converters that allows you to take that string and convert it to whatever you like. To do this, create a class:
Namespace Converters
<ValueConversion(GetType(Object), GetType(FlowDocument))> Public Class FlowDocumentConverter
Implements IValueConverter
Public Function Convert(ByVal objValue As Object, ByVal tTarget As Type, ByVal objParam As Object, _
ByVal ciCulture As CultureInfo) As Object Implements IValueConverter.Convert
Using msDocument As New MemoryStream((New ASCIIEncoding).GetBytes(CStr(objValue)))
Dim fdDocument As New FlowDocument()
Dim trDocument As New TextRange(fdDocument.ContentStart, fdDocument.ContentEnd)
trDocument.Load(msDocument, DataFormats.Xaml)
trDocument = Nothing
Return fdDocument
End Using
End Function
Public Function ConvertBack(ByVal objValue As Object, ByVal tTarget As Type, ByVal objParam As Object, _
ByVal ciCulture As CultureInfo) As Object Implements IValueConverter.ConvertBack
Return Nothing 'Not interested in converting back from a FlowDocument to a String
End Function
End Class
End Namespace
Now add your xmlns to the page/usercontrol/window/whatever...
<Page ... xmlns:converters="clr-namespace:Converters" ... >
Create yourself some resources...
<Page.Resources>
<converter:FlowDocumentConverter x:Key="FlowDocumentConverter" />
</Page.Resources>
And bind like so...
<FlowDocumentScrollViewer Document="{Binding PropertyName, Converter={StaticResource FlowDocumentConverter}}" />
Just replace "PropertyName" with the name of the property or column that has the XAML FlowDocument string you wish to use.
It's a little counter-intuitive, it would be nice if you could just Document="{Binding PropertyName}" and be done with it, converting it to a FlowDocument on the fly... but the solution here is easy enough.Labels: Coding, Databinding, FlowDocument, VB.NET, XAML
|
|