加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

wpf – XAML如何设置只读CLR属性?

发布时间:2020-12-14 05:52:18 所属栏目:Windows 来源:网络整理
导读:我正在尝试在WinPhone7的代码中创建一个应用程序栏.执行此操作的XAML如下所示: PhoneApplicationPage.ApplicationBar shellns:ApplicationBar Visible="True" IsMenuEnabled="True" shellns:ApplicationBar.Buttons shellns:ApplicationBarIconButton IconU
我正在尝试在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; }
}

为什么在XAML而不是C#中可以做到这一点?是否有使用此语法构造对象的特殊方法?

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

解决方法

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

直接添加到Buttons属性.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读