加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

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; } }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读