开发者

When would you use XamlServices.Transform()?

开发者 https://www.devze.com 2023-01-29 23:55 出处:网络
I\'m looking over th开发者_如何学运维e MSDN documentation for the System.Xaml namespace and in particular the XamlServices class. I\'m wondering what a use case for the XamlServices.Transform method w

I'm looking over th开发者_如何学运维e MSDN documentation for the System.Xaml namespace and in particular the XamlServices class. I'm wondering what a use case for the XamlServices.Transform method would be?

I've got nothing against it, I just don't see a case where this would be useful and I'm wondering what I'm missing.


The Transform takes a XamlReader and a XamlWriter.

There are two out-of-the-box writers and there are five out-of-the-box readers. Of course you can create your own.

So the Transform method allows you to stitch one of the readers and one of the writers together to transfer/translate/transform the xaml from one place to another.

There are a couple of scenarios when loose Xaml is useful but to me the most obvious two are:

  1. Workflow definitions and
  2. Design-time/run-time generators of UI

EDIT

This is the code from the Transform method:

public static void Transform(XamlReader xamlReader, XamlWriter xamlWriter, bool closeWriter)
{
    if (xamlReader == null)
        throw new ArgumentNullException("xamlReader");
    if (xamlWriter == null)
        throw new ArgumentNullException("xamlWriter");
    IXamlLineInfo xamlLineInfo = xamlReader as IXamlLineInfo;
    IXamlLineInfoConsumer lineInfoConsumer = xamlWriter as IXamlLineInfoConsumer;
    bool flag = false;
    if (xamlLineInfo != null && xamlLineInfo.HasLineInfo && (lineInfoConsumer != null && lineInfoConsumer.ShouldProvideLineInfo))
        flag = true;
    while (xamlReader.Read())
    {
        if (flag && xamlLineInfo.LineNumber != 0)
            lineInfoConsumer.SetLineInfo(xamlLineInfo.LineNumber,xamlLineInfo.LinePosition);
        xamlWriter.WriteNode(xamlReader);
    }

    if (!closeWriter)
        return;
    xamlWriter.Close();
}

Nothing fancy but just convenient so you do not have to write it yourself.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号