Max
Max

Reputation: 246

Wp 8.1 and trigger

I've developed app on windows phone 8 with caliburn. On one of my pages, I have a button inside of a datatemplate and I have set trigger on mouse click:

            <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>

It works fine. Now I've created new app on windows phone 8.1 (winrt), but with same code I've an error on trigger. How i can do it on wp 8.1 ? I have found winrttriggers.codeplex.com but it isn't compatible with wp 8.1 platform.

Thanks a lot!

EDIT 1:

With your code my view doesn't fire command. Now I have:

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

My command is delegate command:

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
}

On my view model I have:

  public ICommand OpenPageCommand { get; set; }

on constructor:

  OpenPageCommand = new DelegateCommand<Guid>(GoToPage);

and my methods is:

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

but it does not work. Any idea?

Thanks so much!

Upvotes: 2

Views: 1380

Answers (1)

Kajzer
Kajzer

Reputation: 2385

You should be able to do this with the Behaviors SDK. Include this namespaces:

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

And than you should be able to trigger a command like this:

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

Upvotes: 5

Related Questions