Wp 8.1和触发器

时间:2014-07-23 08:31:53

标签: windows-runtime windows-phone-8.1 eventtrigger

我在带有caliburn的Windows Phone 8上开发了应用程序。在我的一个页面上,我在datatemplate中有一个按钮,我在鼠标点击时设置了触发器:

            <DataTemplate>
                <Button>
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="Click">
                            <micro1:ActionMessage MethodName="GoToPage">
                                <micro1:Parameter Value="{Binding Path=PageId}" />
                            </micro1:ActionMessage>
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                    <TextBlock Text="{Binding Path=PageDescription}" TextWrapping="Wrap"
                           HorizontalAlignment="Center" VerticalAlignment="Center" />
                </Button>
            </DataTemplate>

工作正常。 现在我已经在Windows Phone 8.1(winrt)上创建了新应用程序,但是使用相同的代码我在触发器上出错了。我怎么能在wp 8.1上做到这一点?我找到了winrttriggers.codeplex.com但它与wp 8.1平台不兼容。

非常感谢!

编辑1:

使用您的代码,我的视图不会触发命令。 现在我有:

                    <i:Interaction.Behaviors>
                        <core:EventTriggerBehavior EventName="Click">
                            <core:InvokeCommandAction Command="{Binding OpenPageCommand}" CommandParameter="{Binding Path=PageId}" />
                        </core:EventTriggerBehavior>
                    </i:Interaction.Behaviors>

我的命令是委托命令:

public class DelegateCommand<T> : ICommand
{
    private readonly Func<T, bool> _canExecuteMethod;
    private readonly Action<T> _executeMethod;

    #region Constructors

    public DelegateCommand(Action<T> executeMethod)
        : this(executeMethod, null)
    {
    }

    public DelegateCommand(Action<T> executeMethod, Func<T, bool> canExecuteMethod)
    {
        _executeMethod = executeMethod;
        _canExecuteMethod = canExecuteMethod;
    }

    #endregion Constructors

    #region ICommand Members

    public event EventHandler CanExecuteChanged;

    bool ICommand.CanExecute(object parameter)
    {
        try
        {
            return CanExecute((T)parameter);
        }
        catch { return false; }
    }

    void ICommand.Execute(object parameter)
    {
        Execute((T)parameter);
    }

    #endregion ICommand Members

    #region Public Methods

    public bool CanExecute(T parameter)
    {
        return ((_canExecuteMethod == null) || _canExecuteMethod(parameter));
    }

    public void Execute(T parameter)
    {
        if (_executeMethod != null)
        {
            _executeMethod(parameter);
        }
    }

    public void RaiseCanExecuteChanged()
    {
        OnCanExecuteChanged(EventArgs.Empty);
    }

    #endregion Public Methods

    #region Protected Methods

    protected virtual void OnCanExecuteChanged(EventArgs e)
    {
        var handler = CanExecuteChanged;
        if (handler != null)
        {
            handler(this, e);
        }
    }

    #endregion Protected Methods
}

在我的视图模型中,我有:

  public ICommand OpenPageCommand { get; set; }

在构造函数上:

  OpenPageCommand = new DelegateCommand<Guid>(GoToPage);

我的方法是:

  public void GoToPage(Guid pageId)
    { .....

但它不起作用。有什么想法吗?

非常感谢!

1 个答案:

答案 0 :(得分:5)

您应该可以使用Behaviors SDK执行此操作。包括此命名空间:

xmlns:i="using:Microsoft.Xaml.Interactivity"
xmlns:core="using:Microsoft.Xaml.Interactions.Core"

而且你应该能够触发这样的命令:

<i:Interaction.Behaviors>
    <core:EventTriggerBehavior EventName="Click">
        <core:InvokeCommandAction Command="{Binding GoToPage}" CommandParameter="{Binding Path=PageId}" />
    </core:EventTriggerBehavior>
</i:Interaction.Behaviors>