我正在尝试在 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# 中完成?有没有一种特殊的方法可以使用这种语法构造对象?
更重要的是,我怎样才能在代码中重新创建它?