如何为caliburn.micro添加自定义约定?

问题描述 投票:4回答:1

在我的项目中,我需要将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));
                }
            }
        }

任何人都知道这段代码有什么问题吗?

windows-phone-8 naming-conventions caliburn.micro
1个回答
7
投票

我正在我的项目中使用此公约,没有任何问题。这是一个演练:

AppBootstrapper.cs中,您将元素约定包括在其他给定约定中

private static void AddCustomConventions()
{
    ConventionManager.AddElementConvention<FrameworkElement>(Control.VisibilityProperty, "Visibility", "IsVisible");
    var baseBindProperties = ViewModelBinder.BindProperties;
    ViewModelBinder.BindProperties = (frameWorkElements, 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, PropertyName, property, element, convention, 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内,放置后缀为IsVisible的public property类型的bool

public bool FooIsVisible { get { return true; } }

public bool BarIsVisible { get { return false; } }
热门问题
推荐问题
最新问题