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

问题描述

我正在研究Xamarin.Forms项目.在Prism 6.3之前,我使用6.2和Corcav.Behaviors包.我不需要传递参数,因此效果很好.但是,在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);
    }
}

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:

.. and my custom EventArgs:

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

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

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

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

问题:如何在Prism.Behaviors中进行基础设施初始化?

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

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

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

当我更改为DelegateCommand时,它可以在Android上运行,但....

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:

和..项目符合,但是在Start.xaml.cs中出现错误:

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

PS.请帮助我,请不要告诉我清除解决方案/删除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