locked
Namespace and alias RRS feed

  • Question

  • I am trying to create a ResourceDictionary on the fly (don't worry this is not a WPF question). I would like to have output like:

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    . . . .
    
     <DataTemplate x:Key="TemplateClassA" DataType="{x:Type local:ClassA}">
      <TextBlock Text="{Binding Path=Property1}" />
      </DataTemplate>
    
    . . . .  
    </ResourceDictionary>
    

    I have code to do this that looks like:

          var messagesTemplates = new XDocument(
            new XElement(xmlns + "ResourceDictionary",
     . . . . .
                    select new XElement(xmlns + "DataTemplate", 
                         new XAttribute(x + "Key", c.Attribute("name").Value + "Template"),
                         new XAttribute("DataType", string.Format("{{x:Type local:{0}}}", c.Attribute("name").Value)),
    
    
    The problem I am having is two fold. 1) I cannot set the namespace alias for the DataTemplate attribute. So instead of seeing x:Key I see p2:Key 2) The DataType attribute *value* needs two namespace alias'. So I write something like "{x:Type local:....}" and since I cannot or do not know how to declare x namespace alias it doesn't mean much. So the question is basically how to I control the namespace alias's and declare more than one namespace alias declaration. Any ideas?
    Kevin Burton
    Sunday, March 13, 2011 5:16 AM

Answers

  • Is this enough?

    <Window x:Class="XAMLWriting.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
      <Grid>
        <TextBox x:Name="display"/>
      </Grid>
    </Window>
    
    using System.Windows;
    using System.Xml;
    using System.IO;
    
    namespace XAMLWriting
    {
      public partial class MainWindow : Window
      {
        private const string xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation";
        private const string xmlnsX = "http://schemas.microsoft.com/winfx/2006/xaml";
        private const string x = "x";
        private const string xmlnsLocal = "localNamespace";
        private const string local = "local";
    
        public MainWindow()
        {
          InitializeComponent();
          var ms = new MemoryStream();
          var writer = XmlWriter.Create(ms, new XmlWriterSettings { Indent = true, OmitXmlDeclaration = true });
          
          writer.WriteStartElement("ResourceDictionary", xmlns);
          writer.WriteAttributeString("xmlns", x, null, xmlnsX);
          writer.WriteAttributeString("xmlns", local, null, xmlnsLocal);
    
          writer.WriteStartElement("DataTemplate");
          writer.WriteAttributeString(x, "Key", null, "TemplateClassA");
          writer.WriteAttributeString("DataType", string.Format("{{{0} {1}}}", 
            ComposedName(x, "Type"), ComposedName(local, "ClassA")));
    
          writer.WriteStartElement("TextBlock");
          writer.WriteAttributeString("Text", "{Binding Path=Property1}");
          writer.WriteEndElement(); // TextBlock
    
          writer.WriteEndElement(); // DataTemplate
    
          writer.WriteEndElement(); // ResourceDictionary
          writer.Flush();
          ms.Position = 0;
          display.Text = new StreamReader(ms).ReadToEnd();
        }
    
        private static string ComposedName(string ns, string name)
        { return ns + ":" + name; }
      }
    }
    
    


    Bigsby, Lisboa, Portugal
    O que for, quando for, é que será o que é...
    Wenn ist das Nunstruck git und Slotermeyer? Ja! ... Beiherhund das Oder die Flipperwaldt gersput!
    http://bigsby.eu
    • Marked as answer by KevinBurton Monday, March 14, 2011 1:07 PM
    Sunday, March 13, 2011 11:26 PM

All replies

  • Is this enough?

    <Window x:Class="XAMLWriting.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
      <Grid>
        <TextBox x:Name="display"/>
      </Grid>
    </Window>
    
    using System.Windows;
    using System.Xml;
    using System.IO;
    
    namespace XAMLWriting
    {
      public partial class MainWindow : Window
      {
        private const string xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation";
        private const string xmlnsX = "http://schemas.microsoft.com/winfx/2006/xaml";
        private const string x = "x";
        private const string xmlnsLocal = "localNamespace";
        private const string local = "local";
    
        public MainWindow()
        {
          InitializeComponent();
          var ms = new MemoryStream();
          var writer = XmlWriter.Create(ms, new XmlWriterSettings { Indent = true, OmitXmlDeclaration = true });
          
          writer.WriteStartElement("ResourceDictionary", xmlns);
          writer.WriteAttributeString("xmlns", x, null, xmlnsX);
          writer.WriteAttributeString("xmlns", local, null, xmlnsLocal);
    
          writer.WriteStartElement("DataTemplate");
          writer.WriteAttributeString(x, "Key", null, "TemplateClassA");
          writer.WriteAttributeString("DataType", string.Format("{{{0} {1}}}", 
            ComposedName(x, "Type"), ComposedName(local, "ClassA")));
    
          writer.WriteStartElement("TextBlock");
          writer.WriteAttributeString("Text", "{Binding Path=Property1}");
          writer.WriteEndElement(); // TextBlock
    
          writer.WriteEndElement(); // DataTemplate
    
          writer.WriteEndElement(); // ResourceDictionary
          writer.Flush();
          ms.Position = 0;
          display.Text = new StreamReader(ms).ReadToEnd();
        }
    
        private static string ComposedName(string ns, string name)
        { return ns + ":" + name; }
      }
    }
    
    


    Bigsby, Lisboa, Portugal
    O que for, quando for, é que será o que é...
    Wenn ist das Nunstruck git und Slotermeyer? Ja! ... Beiherhund das Oder die Flipperwaldt gersput!
    http://bigsby.eu
    • Marked as answer by KevinBurton Monday, March 14, 2011 1:07 PM
    Sunday, March 13, 2011 11:26 PM
  • Thank you.

    I guess there isn't a solution just using the LINQ XML classes such as XDocument and XElement? This is better than just writting the file out as text which I was leaning towards.

     


    Kevin Burton
    Monday, March 14, 2011 1:09 PM