How does XAML set read-only CLR properties?

I am trying to create an application panel in code for WinPhone7. The XAML that does this is as follows:

<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 just rewrote 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 the property Buttonsdoes not have an accessory set and is defined as follows:

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

How can this be done in XAML and not in C #? Is there a special way to create objects using this syntax?

More importantly, how can I recreate this in code?

+3
source share
3 answers

appbar.Buttons.Add(new ApplicationBarIconButton(new Uri("image.png", UrlKind.Relative));

Add directly to the property Buttons.

+4
source

, Buttons.Add Buttons.

+2

The ApplicationBar.Buttons element has an Add function (see this )

var appBarButton = 
           new ApplicationBarIconButton(new Uri("image.png", UrlKind.Relative)

appBar.Buttons.Add(appBarButton);
+1
source

Source: https://habr.com/ru/post/1740845/


All Articles