wpf – XAML如何设置只读CLR属性?
发布时间:2021-02-20 17:48:46 所属栏目:系统 来源:网络整理
导读:我正在尝试在WinPhone7的代码中创建一个应用程序栏.执行此操作的XAML如下所示: PhoneApplicationPage.ApplicationBar shellns:ApplicationBar Visible="True" IsMenuEnabled="True" shellns:ApplicationBar.Buttons shellns:ApplicationBarIconButton Ico
我正在尝试在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属性. (编辑:天津站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |