1
votes

I'm working on Xamarin.Forms project. Before Prism 6.3 I used 6.2 with Corcav.Behaviors package. I didn't need to pass parameters, so it worked good. But, in iOS project in AppDelegate I needed to run this line:

Corcav.Behaviors.Infrastructure.Init();

I had a comment: //Added to prevent iOS linker to strip behaviors assembly out of deployed package.

Now EventToCommand was added to 6.3 version, so I uninstalled Corcav.Behaviors package and implemented simple example. In Android everything works great, but the iOS.. I have exception:

enter image description here

I think this is because now I'm missing this line: Corcav.Behaviors.Infrastructure.Init();

My example:

VIEW:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:v="clr-namespace:TestApp.Mobile.Views"
             xmlns:behavior="clr-namespace:Prism.Behaviors;assembly=Prism.Forms" 
             x:Class="TestApp.Mobile.Views.StartPage">
  <v:TestGrid x:Name="MainGrid">
    <v:TestGrid.Behaviors>
      <behavior:EventToCommandBehavior EventName="OnTestTapped" 
                                        Command="{Binding OnTestTappedCommand}" 
                                        EventArgsParameterPath="Foo"/>
    </v:TestGrid.Behaviors>
  </v:TestGrid>
</ContentPage>

my custom gird:

public class TestGrid : Grid
{
    public event EventHandler<OnTouchedEventArgs> OnTestTapped;

    public TestGrid()
    {
        var tgr = new TapGestureRecognizer { NumberOfTapsRequired = 1 };
        tgr.Tapped += Tgr_Tapped;
        this.GestureRecognizers.Add(tgr);
    }

    private void Tgr_Tapped(object sender, EventArgs e)
    {
        OnTouchedEventArgs args = new OnTouchedEventArgs(6);
        OnTestTapped?.Invoke(sender, args);
    }
}

VIEWMODEL

public class StartPageViewModel : BindableBase
{
    private bool _canExecute;
    private ICommand onTestTappedCommand;

    public StartPageViewModel()
    {
        _canExecute = true;
    }

    public ICommand OnTestTappedCommand
    {
        get
        {
            return onTestTappedCommand ?? (onTestTappedCommand = 
                    new Command<int>((foo) => HandleEvent(foo), 
                                     (foo) => CanExecute(foo)));
        }
    }

    public async void HandleEvent(int a)
    {
        _canExecute = false;
        Status = $"Working with parameter={a}...";
        Debug.WriteLine("Test with param=" + a);
        await Task.Delay(5000);
        Status = "Done";
        _canExecute = true;
    }

    public bool CanExecute(int a)
    {
        return _canExecute;
    }
}    

.. and my custom EventArgs:

public class OnTouchedEventArgs : EventArgs
{
    public int Foo { get; set; }

    public OnTouchedEventArgs(int foo)
    {
        Foo = foo;
    }
}   

I works 100% on Android, doesn't work on iOS.

QUESTION: How can I Infrastructure.Init in Prism.Behaviors?

EDIT:

I think there might be more errors than I thought... as you can see in my ViewModel, I'm using ICommand and Command class from Xamarin.Forms namespace:

    private ICommand onTestTappedCommand;        
    public ICommand OnTestTappedCommand
    {
        get
        {
            return onTestTappedCommand ?? (onTestTappedCommand = 
                    new Command<int>((foo) => HandleEvent(foo), 
                                     (foo) => CanExecute(foo)));
        }
    }

It works on Android, BUT.. when I change to DelegateCommand:

        return onTestTappedCommand ?? (onTestTappedCommand = 
                new DelegateCommand<int>((foo) => HandleEvent(foo), 
                                 (foo) => CanExecute(foo)));

Android doesn't work as well. Then I have a runtime exception:

Unhandled Exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.

and.. the project complies, but in Start.xaml.cs I have an error:

InitializeComponent() does not exist in the current context

PS. Please help me, and please don't tell me to clear the solution/ remove bin obj folders... it doesn't work.

1

1 Answers

1
votes

There is no init needed. From the exception you are showing my suggestion would be to do a complete clean and rebuild. Make sure that all of the files in your obj and bin folders are deleted.

ViewModel

public DelegateCommand<string> PersonSelectedCommand => new DelegateCommand<string>(OnPersonSelectedCommandExecuted);

public ObservableRangeCollection<string> People { get; set; }

void OnPersonSelectedCommandExecuted(string name)
{
    _pageDialogService.DisplayAlertAsync("Person Selected", name, "Ok" );
}

View

<ListView ItemsSource="{Binding People}">
    <ListView.Behaviors>
        <behaviors:EventToCommandBehavior Command="{Binding PersonSelectedCommand}" 
                                          EventName="ItemTapped"
                                          EventArgsParameterPath="Item" />
    </ListView.Behaviors>
</ListView>