How does XAML set readonly CLR properties

Posted by Igor Zevaka on Stack Overflow See other posts from Stack Overflow or by Igor Zevaka
Published on 2010-04-12T23:39:10Z Indexed on 2010/04/12 23:42 UTC
Read the original article Hit count: 245

Filed under:
|
|

I am trying to create an application bar in code for WinPhone7. The XAML that does it goes like this:

<PhoneApplicationPage.ApplicationBar>
    <shellns:ApplicationBar Visible="True" IsMenuEnabled="True">
        <shellns:ApplicationBar.Buttons>
            <shellns:ApplicationBarIconButton IconUri="/images/appbar.feature.search.rest.png" />
        </shellns:ApplicationBar.Buttons>
    </shellns:ApplicationBar>
</PhoneApplicationPage.ApplicationBar>

So I thought I'd just rewrite it in C#:

var appbar = new ApplicationBar();
var buttons = new List<ApplicationBarIconButton>();
buttons.Add(new ApplicationBarIconButton(new Uri("image.png", UrlKind.Relative));
appbar.Buttons = buttons; //error CS0200: Property or indexer 'Microsoft.Phone.Shell.ApplicationBar.Buttons' cannot be assigned to -- it is read only

The only problem is that Buttons property does not have a set accessor and is defined like so:

public sealed class ApplicationBar {
  //...Rest of the ApplicationBar class from metadata
  public IList Buttons { get; }
}

How come this can be done in XAML and not C#? Is there a special way that the objects are constructed using this syntax? More importantly, how can I recreate this in code?

© Stack Overflow or respective owner

Related posts about xaml

Related posts about wpf