我正在尝试创建一个 WPF 自定义控件,其中包含对已在 BootStrapper 中创建的 Unity 容器的引用 - 例如,
原因是我希望自定义控件能够解析 Unity 容器以便能够使用一些已注册到容器的服务。例如,用户偏好服务/权利服务。
到目前为止,我已经创建了自定义控件,并在文件后面的代码中包含了以下依赖属性
public static readonly DependencyProperty ContainerProperty = DependencyProperty.Register("Container", typeof(UnityContainer), typeof(SomeCustomWPFControl), new PropertyMetadata("DefaultTestValue"));
public UnityContainer Container
{
get { return (UnityContainer)GetValue(ContainerProperty); }
set { SetValue(ContainerProperty, value); }
}
在我尝试包含自定义控件的 WPF 表单中,我已将以下行添加到资源区域:
<Unity:UnityContainer x:Key="unitContainer"></Unity:UnityContainer>
在表单中我自己尝试创建自定义控件:
<Globe:SomeCustomWPFControl Container="{DynamicResource unitContainer}" DockPanel.Dock="Right" x:Name="JimEditor1" Grid.Column="0" Grid.Row="3"></Globe:SomeCustomWPFControl>
我在运行时收到的唯一错误信息是
标记文件“[程序集名称];[路径/文件].xaml中的对象“System.Windows.Controls.Grid”错误' 第 135 行位置 22。
对于我哪里出错了有什么建议吗?谢谢。
完整的Xaml:
<UserControl x:Class="DB.GPF.Globe.Views.JimTestPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Globe="clr-namespace:DB.GPF.Globe"
xmlns:Unity="clr-namespace:Microsoft.Practices.Unity;assembly=Microsoft.Practices.Unity">
<UserControl.Resources>
<Unity:UnityContainer x:Key="unitContainer"></Unity:UnityContainer>
</UserControl.Resources>
<Grid>
<Globe:SomeCustomWPFControl Container="{DynamicResource unitContainer}" x:Name="JImTestControl1"></Globe:SomeCustomWPFControl>
</Grid>
更新:问题似乎是具有 UnityContainer 或 IUnityContainer 类型的 DependencyProperty,如果将类型更改为内置 .Net 类型(例如字符串),则它可以正常运行。为什么我们不能拥有 UnityContainer 或 IUnityContainer 类型的 DependencyProperty 有什么想法吗?
I'm attempting to create a WPF custom control that includes a reference to the unity container that has been created in the BootStrapper - e.g.
The reason being that I would like the custom control to be able to resolve the unity container to be able to use some of the services that have been registered to the container. e.g. a user preference service / an entitlements service.
So far I have created my custom control and have included in the code behind file the following dependency property
public static readonly DependencyProperty ContainerProperty = DependencyProperty.Register("Container", typeof(UnityContainer), typeof(SomeCustomWPFControl), new PropertyMetadata("DefaultTestValue"));
public UnityContainer Container
{
get { return (UnityContainer)GetValue(ContainerProperty); }
set { SetValue(ContainerProperty, value); }
}
In the WPF form where I'm attempting to include my custom control, I've added the following line to the Resources area:
<Unity:UnityContainer x:Key="unitContainer"></Unity:UnityContainer>
In the form it's self I've attempted to create the custom control:
<Globe:SomeCustomWPFControl Container="{DynamicResource unitContainer}" DockPanel.Dock="Right" x:Name="JimEditor1" Grid.Column="0" Grid.Row="3"></Globe:SomeCustomWPFControl>
The only error information I receive at run time is
Error at object 'System.Windows.Controls.Grid' in markup file '[assembly name];[path/file].xaml' Line 135 Position 22.
Any suggestions as to where I'm going wrong? Thx.
Full Xaml:
<UserControl x:Class="DB.GPF.Globe.Views.JimTestPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Globe="clr-namespace:DB.GPF.Globe"
xmlns:Unity="clr-namespace:Microsoft.Practices.Unity;assembly=Microsoft.Practices.Unity">
<UserControl.Resources>
<Unity:UnityContainer x:Key="unitContainer"></Unity:UnityContainer>
</UserControl.Resources>
<Grid>
<Globe:SomeCustomWPFControl Container="{DynamicResource unitContainer}" x:Name="JImTestControl1"></Globe:SomeCustomWPFControl>
</Grid>
Update: The problem appears to be having a DependencyProperty of type UnityContainer or IUnityContainer, if the type is changed to an inbuilt .Net type, e.g. string then it runs fine. Any ideas why we can't have a DependencyProperty of type UnityContainer or IUnityContainer?
发布评论
评论(2)
我认为,在自定义控件中嵌入Unity并不是一个好主意。
如果您需要更改自定义控件的某些属性,则需要公开这些属性并更改它们
I think , embedding Unity in Custom Control is not a good idea.
If you need to change some properties of your custome control than expose those properties and change them
编辑:你的获取值也是错误的。你有 GetValue(GetValue(ContextMenuProperty)) 并且它应该是......
There is a problem in you xaml right after
<Globe:SomeCustomWPFControlContainer
. You're missing a property to bind to.EDIT: your get value is also wrong. You have GetValue(GetValue(ContextMenuProperty)) and it should be ...