Windows phone 8 如何为caliburn.micro添加自定义约定?
在我的项目中,我需要将ui元素的可见性绑定到bool属性,正如您所知,caliburn.micro具有约定“CanName”,所以我想添加我自己的自定义约定。 然后我发现这个[Visibility Autobinding with naming convention]我在我的项目中添加了这个代码,但它不起作用,而且约定“CanName”也不起作用Windows phone 8 如何为caliburn.micro添加自定义约定?,windows-phone-8,naming-conventions,caliburn.micro,Windows Phone 8,Naming Conventions,Caliburn.micro,在我的项目中,我需要将ui元素的可见性绑定到bool属性,正如您所知,caliburn.micro具有约定“CanName”,所以我想添加我自己的自定义约定。 然后我发现这个[Visibility Autobinding with naming convention]我在我的项目中添加了这个代码,但它不起作用,而且约定“CanName”也不起作用 ConventionManager.AddElementConvention<FrameworkElement>(Control.Visi
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));
}
}
}
ConventionManager.AddElementConvention(Control.VisibilityProperty,“可见性”、“IsVisible”);
var baseBindProperties=ViewModelBinder.BindProperties;
ViewModelBinder.BindProperties=(框架元素,viewModel)=>
{
BindVisiblityProperty(框架元素、视图模型);
返回baseBindProperties(frameWorkElements、viewModel);
};
静态void bindVisiblityProperty(IEnumerable项,类型viewModel)
{
foreach(项目中的框架元素)
{
字符串PropertyName=element.Name+“IsVisible”;
var property=viewModel.getPropertyCaseSensitive(PropertyName);
if(属性!=null)
{
var convention=ConventionManager.GetElementConvention(typeof(FrameworkElement));
ConventionManager.SetBindingWithoutBindingOverwrite(viewModel、PropertyName、property、,
元素,约定,约定.GetBindableProperty(元素));
}
}
}
有人知道这段代码有什么问题吗?我在我的项目中使用了这个约定,没有任何问题。下面是一个演练: 在
AppBootstrapper.cs
中,在其他给定约定中包括元素约定
private static void AddCustomConventions()
{
ConventionManager.AddElementConvention(Control.VisibilityProperty,“可见性”、“IsVisible”);
var baseBindProperties=ViewModelBinder.BindProperties;
ViewModelBinder.BindProperties=(框架元素,viewModel)=>
{
BindVisiblityProperty(框架元素、视图模型);
返回baseBindProperties(frameWorkElements、viewModel);
};
}
和您的助手方法:
private static void bindVisiblityProperty(IEnumerable items,类型viewModel)
{
foreach(项目中的框架元素)
{
字符串PropertyName=element.Name+“IsVisible”;
var property=viewModel.getPropertyCaseSensitive(PropertyName);
if(属性!=null)
{
var convention=ConventionManager.GetElementConvention(typeof(FrameworkElement));
ConventionManager.SetBindingWithoutBindingOverwrite(
viewModel,PropertyName,property,element,convention,convention.GetBindableProperty(element));
}
}
}
在PageView.xaml中放置控件并提供x:Name
开始
停止
福
酒吧
在PageViewModel.cs
中放置一个public属性
,类型为bool
,后缀为IsVisible
public bool FooIsVisible{get{return true;}}
公共bool BarIsVisible{get{return false;}
你的使用环境如何?wp8?我不理解你的后续问题?你能把它放在给定问题/答案的上下文中吗?这似乎不适用于当前版本。如果我在此方法中设置断点并打开页面,则frameWorkElements
仅包含顶级控件,如网格
或堆栈面板
。是否需要递归循环?