在 xmlns clr 命名空间中找不到 EventToCommandBehavior:Prism.Behaviors

EventToCommandBehavior not found in xmlns clr-namespace:Prism.Behaviors

我正在从事 Xamarin.Forms 项目。在 Prism 6.3 之前,我使用 6.2 和 Corcav.Behaviors 包。我不需要传递参数,所以效果很好。但是,在 AppDelegate 的 iOS 项目中,我需要 运行 这一行:

Corcav.Behaviors.Infrastructure.Init();

我有评论://添加以防止 iOS 链接器将行为程序集从已部署的包中剥离。

现在6.3版本增加了EventToCommand,所以我卸载了Corcav.Behaviors包,实现了简单的例子。在 Android 中一切正常,但是 iOS.. 我有例外:

我认为这是因为现在我缺少这一行:Corcav.Behaviors.Infrastructure.Init();

我的例子:

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>

我的定制腰带:

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

..和我的自定义 EventArgs:

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

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

我在 Android 上 100% 地工作,在 iOS 上不工作。

问题: 如何在 Prism.Behaviors 中 Infrastructure.Init?

编辑:

我认为错误可能比我想象的要多...正如您在我的 ViewModel 中看到的那样,我使用的是 ICommandCommand class 来自 [=26] =] 命名空间:

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

它适用于 Android,但是..当我更改为 DelegateCommand:

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

Android 也不起作用。然后我有一个运行时间异常:

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

and.. 项目符合要求,但在 Start.xaml.cs 我有一个错误:

InitializeComponent() does not exist in the current context

PS。请帮助我,请不要告诉我清除解决方案/删除 bin obj 文件夹...它不起作用。

不需要初始化。从您展示的例外情况来看,我的建议是进行彻底的清理和重建。确保删除 objbin 文件夹中的所有文件。

视图模型

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

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

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

查看

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