Skip to main content

Microsoft Silverlight

XamlReader.Load with x:Name attributeRSS Feed

(0)

andrewz
andrewz

Member

Member

0 points

2 Posts

XamlReader.Load with x:Name attribute

I'm loading xaml via XamlReader.Load the class has a property named "Name", when loading the xaml it seems to ignore my Name attribute (I also tried x:Name).  The Title property loads fine, but the Name property is always null.

public class Entity
{
   public string Name { get; set; }
   public string Title { get; set; }
}

<Model:Entity
  xmlns="http://schemas.microsoft.com/client/2007"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:Model="clr-namespace:SilverlightApplication1;assembly=SilverlightApplication1"
  x:Name="EntityNameX"
  Title="EntityTitleX">
</Model:Entity>


<Model:Entity
  xmlns="http://schemas.microsoft.com/client/2007"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:Model="clr-namespace:SilverlightApplication1;assembly=SilverlightApplication1"
  Name="EntityNameY"
  Title="EntityTitleY">
</Model:Entity>

I Load the xaml using XamlReader.Load

var x = XamlReader.Load(...)

Does silverlight ignore attributes named Name?

Tim Favour
Tim Favour

Member

Member

93 points

69 Posts

Re: XamlReader.Load with x:Name attribute

Hi,

This is because the XamlReader.Load changed from beta 1 to beta 2 and now always creates a separate namescope so that your loaded elements cannot be found globally with root.FindName. Microsoft suuggests a workaround in the end of this tread:

http://silverlight.net/forums/t/17572.aspx

Tim

andrewz
andrewz

Member

Member

0 points

2 Posts

Re: Re: XamlReader.Load with x:Name attribute

I probably should have mentioned that this xaml is not for any UI controls, it is strictly for serializing data.

Allen Chen – MSFT
Allen Ch...

Star

Star

13862 points

1,803 Posts

Re: XamlReader.Load with x:Name attribute

Hi:

  Please try this:

public class Entity : Control
    {
  
           public string Title { get; set; }
    }
    public partial class Page : UserControl
    {
        public Page()
        {
            InitializeComponent();
            string s = @"<Model:Entity
xmlns='http://schemas.microsoft.com/client/2007'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
xmlns:Model='clr-namespace:SilverlightApplication6;assembly=SilverlightApplication6'
  x:Name='EntityNameX'
  Title='EntityTitleX'>
</Model:Entity>";
            Control q = XamlReader.Load(s) as Control;
         
               string name= q.GetValue(NameProperty).ToString();
      

}

Regards

Sincerely,
Allen Chen
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities