wpf – XAML如何设置只读CLR属性?
发布时间:2021-02-20 02:03 所属栏目:52 来源:网络整理
导读:我正在尝试在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属性. (编辑:ASP站长网) |
相关内容
网友评论
推荐文章
热点阅读