本文介绍了在 xmlns clr-namespace:Prism.Behaviors 中找不到 EventToCommandBehavior的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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();

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

现在 EventToCommand 已添加到 6.3 版本,因此我卸载了 Corcav.Behaviors 包并实现了简单示例.在 Android 中一切正常,但 iOS .. 我有例外:

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:

我认为这是因为现在我错过了这一行:Corcav.Behaviors.Infrastructure.Init();

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

我的例子:

查看:

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

视图模型

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:

.. and my custom EventArgs:

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

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

我 100% 在 Android 上工作,在 iOS 上不工作.

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

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

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

我认为可能有比我想象的更多的错误......正如您在我的 ViewModel 中看到的那样,我正在使用 ICommandCommand 类来自 Xamarin.Forms 命名空间:

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

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

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

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

Android 也不行.然后我有一个运行时异常:

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

未处理的异常:System.Reflection.TargetInvocationException:调用的目标已抛出异常.

并且..项目符合要求,但在 Start.xaml.cs 中出现错误:

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

InitializeComponent() 在当前上下文中不存在

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

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

推荐答案

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

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.

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>

这篇关于在 xmlns clr-namespace:Prism.Behaviors 中找不到 EventToCommandBehavior的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-06 02:27