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プロパティに set アクセサーがなく、次のように定義されていることです。

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

C# ではなく XAML でこれを実行できるのはなぜですか? この構文を使用してオブジェクトを構築する特別な方法はありますか?

さらに重要なことに、コードでこれをどのように再現できますか?

4

3 に答える 3

4

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

プロパティに直接追加しButtonsます。

于 2010-04-12T23:47:14.850 に答える
2

おそらく、Buttons プロパティに割り当てる代わりに、Buttons.Add を使用します。

于 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 に答える