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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何为caliburn.micro添加自定义约定?相关的知识,希望对你有一定的参考价值。

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

以上是关于如何为caliburn.micro添加自定义约定?的主要内容,如果未能解决你的问题,请参考以下文章

C# WPF MVVM开发框架Caliburn.Micro自定义引导程序④

如何将 ValueConverter 应用于基于约定的 Caliburn.Micro 绑定示例?

C# WPF MVVM开发框架Caliburn.Micro入门介绍①

Caliburn.Micro-如何从继承的ViewModel在WPF视图中显示多个项目:Conductor 。Collection.AllActive

C# WPF MVVM开发框架Caliburn.Micro 关于Conventions⑧

C# WPF MVVM开发框架Caliburn.Micro View / View Model 命名⑨