3

我正在尝试在 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# 中完成?有没有一种特殊的方法可以使用这种语法构造对象?

更重要的是,我怎样才能在代码中重新创建它?

4

3 回答 3

4

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

直接添加到Buttons属性。

于 2010-04-12T23:47:14.850 回答
2

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

于 2010-04-12T23:47:30.720 回答
1

ApplicationBar.Buttons成员有一个Add 函数(见这个)

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

appBar.Buttons.Add(appBarButton);
于 2010-04-12T23:48:30.547 回答