开发者

App.Config or XAML

I am currently evaluating my options for a rewrite of the projects I'm working on and I am a bit miffed by the stringly-typed nature of our app.config files.

I'd like to move to a more structured approach, so I have two options:

  • Use custom SectionHandlers in the app.config
  • Scrap app.config and use XAML instead.

I'd like to get your opinions and horror stories on this, what are the pros and 开发者_运维知识库cons of using XAML for this?

Cheers, Florian


If we are talking about config files I would use the app.config? Why? That's what it's meant for. If we are talking about resources (pictures, messages) I would put them in a XAML resource directory.

There is a guideline available on the web on where to put what, but was still in draft last time I checked and doesn't mention app.config afaik.

But do what works best for you :)


In terms of development effort is 6 of one half a dozen of the other.

If you use Xaml you will need to create a set of classes instances of which the Xaml will create.

If you using Custom SectionHandlers you will still need to create classes instances of which these sections will represent. You also need to create the SectionHandlers as well.

1 - 0 to Xaml.

In the case of Xaml though you will need to provide your own infrastucture to load the xaml at startup and access the configuration throughout your app.

On the other hand using Section Handlers the existing .NET ConfigurationManager provides the infrastrucure to access these.

1 all


If you're working with .NET 4, why not combine your two options and put XAML inside App.config?

using System.Configuration;
using System.Xaml;
using System.Xml;

public class XamlConfigurationSection : IConfigurationSectionHandler
{
    public object Create(object parent, object configContext, XmlNode section)
    {
        return XamlServices.Parse(section.OuterXml);
    }
}

This custom configuration section allows you to include any object described as XAML in App.config:

<configSections>
  <section name="SomeType" type="XamlConfigurationSection, …" />
</configSections>

<SomeType xmlns="clr-namespace:SomeNamespace;assembly=…"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  …
</SomeType>

provided that you have a type:

namespace SomeNamespace
{
    public class SomeType
    {
        public SomeType() { … }  // XAML requires a parameterless constructor
        …
    }
}

and finally retrieve an instance of this type from App.config with:

var objectOfSomeType = ConfigurationManager.GetSection("SomeType") as SomeType;
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜