gpt4 book ai didi

windows-phone-8 - 如何为 caliburn.micro 添加自定义约定?

转载 作者:行者123 更新时间:2023-12-04 23:51:04 25 4
gpt4 key购买 nike

在我的项目中,我需要将 ui 元素的可见性绑定(bind)到 bool 属性,因为你知道 caliburn.micro 有约定“CanName”,所以我想添加我自己的自定义约定。
然后我发现这个[使用命名约定的可见性自动绑定(bind)我在我的项目中添加了这个代码,但它不起作用并且约定“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你放置一个 public property类型 bool带有后缀 IsVisible

public bool FooIsVisible { get { return true; } }

public bool BarIsVisible { get { return false; } }

关于windows-phone-8 - 如何为 caliburn.micro 添加自定义约定?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21842860/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com