我正在尝试为WinPhone7创建一个应用程序栏。实现它的XAML代码如下:
<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
属性没有设置访问器,并且定义如下:public sealed class ApplicationBar {
//...Rest of the ApplicationBar class from metadata
public IList Buttons { get; }
}
为什么可以在XAML中实现这个功能,而在C#中却不能?使用这种语法构建对象的方式有什么特殊之处吗?
更重要的是,我该如何在代码中重新创建它?