2010-04-12 98 views
3

我想在WinPhone7的代码中创建一个应用程序栏。 ,做它的XAML是这样的:XAML如何设置只读CLR属性?

<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> 

所以我想我只是把它改写在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 

唯一的问题是,Buttons属性没有set访问并定义如下:

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

这是怎么来的,可以在XAML而不是C#?有没有一种特殊的方式来使用这种语法构造对象?

更重要的是,我该如何在代码中重新创建它?

回答

4

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

直接添加到Buttons财产。

+0

卫生署!感谢那。 – 2010-04-12 23:58:42

2

它可能使用Buttons.Add而不是分配给Buttons属性。

+0

是的。列表是可变的,所以您不必重新分配引用,因此它们使其只读以避免令人讨厌的副作用 – Earlz 2010-04-12 23:50:40

1

ApplicationBar.Buttons部件具有添加功能(参见this

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

appBar.Buttons.Add(appBarButton);