Unity: Do DependencyProperties / Control.SetBinding work?
Posted: 22 Feb 2015, 04:27
Can I make dependency properties for my UserControls and use them like I would in WPF? I'm attempting this but I get the error:
Exception: Unknown Unity extended type
Noesis.Error.Check () (at Assets/Plugins/NoesisGUI/Scripts/Core/NoesisError.cs:21)
Noesis.DependencyProperty.Noesis_RegisterDependencyProperty_String_ (IntPtr classType, IntPtr propertyName, IntPtr propertyMetadata) (at Assets/Plugins/NoesisGUI/Scripts/Proxies/DependencyPropertyExtend.cs:262)
Noesis.DependencyProperty.Register (System.String propertyName, System.Type propertyType, System.Type ownerType, Noesis.PropertyMetadata propertyMetadata) (at Assets/Plugins/NoesisGUI/Scripts/Proxies/DependencyPropertyExtend.cs:155)
Controls.GringosIcon..cctor ()
Rethrow as TypeInitializationException: An exception was thrown by the type initializer for Controls.GringosIcon
Here are the relevant bits of my code
GringosIcon.xaml
GringosIcon.xaml
And lastly GringosIconController (which is a MonoBehavior)
I suspect that the problem is that I have not given one of these classes the [Noesis.Extend] attribute. But that attribute has been removed. I thought I could use INotifyPropertyChanged instead? Can you explain why this isn't working?
Exception: Unknown Unity extended type
Noesis.Error.Check () (at Assets/Plugins/NoesisGUI/Scripts/Core/NoesisError.cs:21)
Noesis.DependencyProperty.Noesis_RegisterDependencyProperty_String_ (IntPtr classType, IntPtr propertyName, IntPtr propertyMetadata) (at Assets/Plugins/NoesisGUI/Scripts/Proxies/DependencyPropertyExtend.cs:262)
Noesis.DependencyProperty.Register (System.String propertyName, System.Type propertyType, System.Type ownerType, Noesis.PropertyMetadata propertyMetadata) (at Assets/Plugins/NoesisGUI/Scripts/Proxies/DependencyPropertyExtend.cs:155)
Controls.GringosIcon..cctor ()
Rethrow as TypeInitializationException: An exception was thrown by the type initializer for Controls.GringosIcon
Here are the relevant bits of my code
GringosIcon.xaml
Code: Select all
<UserControl x:Class="Controls.GringosIcon"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
UseLayoutRounding="True">
<Button
x:Name="Button">
<Border
BorderThickness="4"
BorderBrush="{Binding Path=FrameBrush, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}">
<!-- Button Content -->
</Border>
</Button>
</UserControl>
Code: Select all
public enum IconState
{
Targetable,
Active,
NotTargetable,
Inactive,
Selected,
}
[Noesis.UserControlSource(@"Assets/Gringos/xamls/Controls/GringosIcon.xaml")]
public class GringosIcon : Noesis.UserControl, INotifyPropertyChanged
{
public static readonly DependencyProperty PropertyTypeProperty = DependencyProperty.Register(
"DpIconState", typeof (IconState), typeof (GringosIcon), new PropertyMetadata(default(IconState)));
public IconState DpIconState
{
get { return (IconState) GetValue(PropertyTypeProperty); }
set
{
SetValue(PropertyTypeProperty, value);
OnPropertyChanged("FrameBrush");
}
}
public Brush FrameBrush
{
get
{
switch (DpIconState)
{
// bla bla bla
}
}
}
Code: Select all
public class GringosIconController : EventBehaviour, INotifyPropertyChanged
{
public GringosIcon controlledIcon;
public IconState DpIconState
{
get
{
if (TargetMode)
{
if (gameObject.Equals(CurrentTargeter))
{
return IconState.Selected;
}
return CanBeTargeted ? IconState.Targetable : IconState.NotTargetable;
}
return IconActive ? IconState.Active : IconState.Inactive;
}
}
// other stuff
void Start()
{
controlledIcon.DataContext = this;
controlledIcon.SetBinding(GringosIcon.PropertyTypeProperty, "DpIconState");
// other stuff
}
// other stuff
}