Windows 8.1:弹出窗口上的行为不起作用

时间:2013-12-16 14:15:41

标签: xaml mvvm behavior windows-8.1 flyout

我正在使用VS 2013和MVVM Light开发Windows 8.1应用程序。

以下代码显示了appbar中弹出按钮的行为:

<AppBarButton.Flyout>
    <Flyout x:Name="FlyoutCalculator"
            Placement="Top"
            FlyoutPresenterStyle="{StaticResource FlyoutPresenterBaseStyle}">

        <uc:Calculator ApplyCommand="{Binding CancelCommand}"
                       CancelCommand="{Binding CancelCommand}"                                           
                       Available="{Binding AvailableCounter, Mode=OneWay}"
                       SelectedItem="{Binding SelectedItem, Mode=TwoWay}"/>
        <interactivity:Interaction.Behaviors>
            <core:EventTriggerBehavior EventName="Opening">
                <core:InvokeCommandAction Command="{Binding ShowCurrentCostsCommand}" />
            </core:EventTriggerBehavior>
        </interactivity:Interaction.Behaviors>
    </Flyout>
</AppBarButton.Flyout>

不幸的是,我在编译应用程序时遇到异常:

  

WinRT-Informationen:无法将类型Microsoft.Xaml.Interactions.Core.EventTriggerBehavior的实例添加到Microsoft.Xaml.Interactivity.BehaviorCollection类型的集合

视图中的其他行为确实有效,有人知道解决方案吗?

2 个答案:

答案 0 :(得分:1)

我没有解决方案但是: 我没有在我的Windows 8.1应用程序中使用Flyout,我正在使用UserControl,我已经添加了EventTriggerBehavior。我在运行时从VisualStudio获得完全相同的Errormessage。 当我使用RoutedEventHandler时,这可能会导致问题,因为您使用

EventHandler<object> Opening

作为行为的触发器。但这只是对问题的看法。

对我来说,我找到了答案:

我已将RoutedEventHandler的类型更改为普通EventHandler。并且仅使用发件人调用触发RoutedEventHandler的CodeBehind内部的方法,因为我不知道如何将RoutedEventArgs转换为EventArgs,但只要我不需要EventArgs它就是不是问题。

您还可以通过创建带有弹出控件的UserControl来制作解决方法,并将Opening事件公开到您使用它的页面。然后,您可以将EventTriggerBehavior添加到UserControl并将其连接到您的自定义Opening事件,您应该获得预期的行为。

答案 1 :(得分:1)

这里的答案非常晚,但我遇到了同样的问题,并在找到这篇文章后想出了一个解决方案。

我刚刚创建了一个专门针对弹出窗口的自定义行为,就像这样使用。弹出弹出窗口时将执行OpenActions,弹出窗口关闭时将执行CloseActions。在这种情况下,我希望当弹出窗口打开时,底部应用栏不可见。

<Flyout Placement="Full">
  <i:Interaction.Behaviors>
    <behaviors:FlyoutBehavior>
      <behaviors:FlyoutBehavior.OpenActions>
        <core:ChangePropertyAction PropertyName="Visibility" Value="Collapsed" TargetObject="{Binding ElementName=CommandBar}" />
      </behaviors:FlyoutBehavior.OpenActions>
      <behaviors:FlyoutBehavior.CloseActions>
        <core:ChangePropertyAction PropertyName="Visibility" Value="Visible" TargetObject="{Binding ElementName=CommandBar}" />
      </behaviors:FlyoutBehavior.CloseActions>
    </behaviors:FlyoutBehavior>
  </i:Interaction.Behaviors>
  <Grid>
    ...
  </Grid>
</Flyout>

代码在这里:

class FlyoutBehavior : DependencyObject, IBehavior
{
    public DependencyObject AssociatedObject { get; private set; }

    public void Attach(Windows.UI.Xaml.DependencyObject associatedObject)
    {
        var flyout = associatedObject as FlyoutBase;

        if (flyout == null)
            throw new ArgumentException("FlyoutBehavior can be attached only to FlyoutBase");

        AssociatedObject = associatedObject;

        flyout.Opened += FlyoutOpened;
        flyout.Closed += FlyoutClosed;
    }

    public void Detach()
    {
        var flyout = AssociatedObject as FlyoutBase;

        if (flyout != null)
        {
            flyout.Opened -= FlyoutOpened;
            flyout.Closed -= FlyoutClosed;
        }
    }

    public static readonly DependencyProperty OpenActionsProperty =
        DependencyProperty.Register("OpenActions", typeof(ActionCollection), typeof(FlyoutBehavior), new PropertyMetadata(null));

    public ActionCollection OpenActions
    {
        get { return GetValue(OpenActionsProperty) as ActionCollection; }
        set { SetValue(OpenActionsProperty, value); }
    }

    public static readonly DependencyProperty CloseActionsProperty =
        DependencyProperty.Register("CloseActions", typeof(ActionCollection), typeof(FlyoutBehavior), new PropertyMetadata(null));

    public ActionCollection CloseActions
    {
        get { return GetValue(CloseActionsProperty) as ActionCollection; }
        set { SetValue(CloseActionsProperty, value); }
    }

    private void FlyoutOpened(object sender, object e)
    {
        foreach (IAction action in OpenActions)
        {
            action.Execute(AssociatedObject, null);
        }
    }

    private void FlyoutClosed(object sender, object e)
    {
        foreach (IAction action in CloseActions)
        {
            action.Execute(AssociatedObject, null);
        }
    }

    public FlyoutBehavior()
    {
        OpenActions = new ActionCollection();
        CloseActions = new ActionCollection();
    }
}