windows-phone-8 – 如何为caliburn.micro添加自定义约定?
发布时间:2020-12-14 03:54:16 所属栏目:Windows 来源:网络整理
导读:在我的项目中,我需要将ui元素的可见性绑定到bool属性,因为您知道caliburn.micro具有约定“CanName”,所以我想添加我自己的自定义约定. 然后我发现[可见性自动绑定与命名约定我在我的项目中添加此代码,但它不起作用和约定“CanName”也不起作用. ConventionMa
在我的项目中,我需要将ui元素的可见性绑定到bool属性,因为您知道caliburn.micro具有约定“CanName”,所以我想添加我自己的自定义约定.
然后我发现[可见性自动绑定与命名约定我在我的项目中添加此代码,但它不起作用和约定“CanName”也不起作用. ConventionManager.AddElementConvention<FrameworkElement>(Control.VisibilityProperty,"Visibility","IsVisible"); var baseBindProperties = ViewModelBinder.BindProperties; ViewModelBinder.BindProperties = (frameWorkElements,viewModel) => { BindVisiblityProperties(frameWorkElements,viewModel); return baseBindProperties(frameWorkElements,viewModel); }; static void BindVisiblityProperties(IEnumerable<FrameworkElement> items,Type viewModel) { foreach (FrameworkElement element in items) { string PropertyName = element.Name + "IsVisible"; var property = viewModel.GetPropertyCaseInsensitive(PropertyName); if (property != null) { var convention = ConventionManager.GetElementConvention(typeof(FrameworkElement)); ConventionManager.SetBindingWithoutBindingOverwrite(viewModel,PropertyName,property,element,convention,convention.GetBindableProperty(element)); } } } 有谁知道这段代码有什么问题? 解决方法
我在我的项目中使用本公约没有任何问题.这是一个演练:
在AppBootstrapper.cs中,您可以在其他给定约定中包含元素约定 private static void AddCustomConventions() { ConventionManager.AddElementConvention<FrameworkElement>(Control.VisibilityProperty,viewModel) => { BindVisiblityProperties(frameWorkElements,viewModel); return baseBindProperties(frameWorkElements,viewModel); }; } 和你的助手方法: private static void BindVisiblityProperties(IEnumerable<FrameworkElement> items,Type viewModel) { foreach (FrameworkElement element in items) { string PropertyName = element.Name + "IsVisible"; var property = viewModel.GetPropertyCaseInsensitive(PropertyName); if (property != null) { var convention = ConventionManager.GetElementConvention(typeof(FrameworkElement)); ConventionManager.SetBindingWithoutBindingOverwrite( viewModel,convention.GetBindableProperty(element)); } } } 在PageView.xaml中,放置控件并提供x:Name <StackPanel Orientation="Horizontal" HorizontalAlignment="Center"> <Button x:Name="StartRecord">start</Button> <Button x:Name="StopRecord">stop</Button> <Button x:Name="Foo">foo</Button> <Button x:Name="Bar">bar</Button> </StackPanel> 在PageViewModel.cs中,放置一个bool类型的公共属性,后缀为IsVisible public bool FooIsVisible { get { return true; } } public bool BarIsVisible { get { return false; } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 使用PowerShell管理Windows防火墙是否容易甚至可能?
- windows – 最快的大型目录/文件夹删除方法
- windows – SVN修订图
- Windows – XP,Vista,Win7之间的IE8行为差异?
- windows – 想要抑制文件未找到输出
- windows – 如何安排任务来调用URL
- windows-server-2008 – 列出Win Server 2008上的所有共享文
- 用于.NET 4安装的Windows-7 reboot上的nsis不会继续安装
- winapi – Windows 7 Ribbon UI Win32 API代码
- 事件 – 如何向$(window).load()添加函数?
推荐文章
站长推荐
- Windows Server 2016-Active Directory域服务概述
- 指定Windows cmd中接口的路由
- windows-server-2008-r2 – Windows Server 2008
- 使用Kubernetes部署应用程序中的Windows SMB共享
- 有关使用Microsoft OLE DB Provider for Jet 4.0
- windows – 记录学校的电脑和网络(自愿)
- WPF MahApps.Metro – 在哪里放置ResourceDictio
- windows-phone-7 – Windows Phone 7模拟器:我可
- dll连锁不一致
- windows-phone-8 – Windows Phone 8页面生命周期
热点阅读