如何在XAML中专门为Windows Phone 8.1实现自定义行为?
我正在使用VS2013和Blend 2013。
代码:
public class SelectionBehavior : IBehavior
{
public DependencyObject AssociatedObject
{
get { throw new NotImplementedException(); }
}
public void Attach(DependencyObject associatedObject)
{
throw new NotImplementedException();
}
public void Detach()
{
throw new NotImplementedException();
}
}
XAML:
xmlns:Interactivity="using:Microsoft.Xaml.Interactivity" xmlns:Core="using:Microsoft.Xaml.Interactions.Core" xmlns:Media="using:Microsoft.Xaml.Interactions.Media" x:Name="page"
<Interactivity:Interaction.Behaviors>
<behaviors:SelectionBehavior />
</Interactivity:Interaction.Behaviors>
当我尝试构建解决方案时,收到以下错误:
无法将指定的值分配给集合。该 期望以下类型:“DependencyObject”。
无法将“SelectionBehavior”添加到集合属性中 'Behaviors',类型必须是'DependencyObject'
“SelectionBehavior”类型的值无法添加到集合或 'BehaviorCollection'类型的字典。
答案 0 :(得分:3)
您的行为应该继承自DependencyObject
,请尝试此操作 -
public class SelectionBehavior : DependencyObject, IBehavior