【问题标题】:EventToCommandBehavior not found in xmlns clr-namespace:Prism.Behaviors在 xmlns clr-命名空间中找不到 EventToCommandBehavior:Prism.Behaviors
【发布时间】:2017-08-19 10:47:09
【问题描述】:

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

Corcav.Behaviors.Infrastructure.Init();

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

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

我认为这是因为现在我错过了这一行: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:

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

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

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

问题: 我如何才能在Prism.Behaviors 中进行 Infrastructure.Init?

编辑:

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

    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 也不能正常工作。然后我有一个运行时异常:

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

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

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

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

【问题讨论】:

    标签: c# xamarin xamarin.ios xamarin.forms prism


    【解决方案1】:

    不需要初始化。从您显示的例外情况来看,我的建议是进行彻底的清理和重建。确保您的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>
    

    【讨论】:

    • 不幸的是,它不起作用。我删除了所有 bin、obj 文件夹。甚至包裹。重新启动 Visual Studio。以管理员身份打开,恢复包...不起作用。显然我不想向现有控件添加行为。我有一个继承自Grid 的控件。某处有工作样本吗?我认为这可能是一个错误
    • 从您的模拟器中卸载应用程序。我使用 EventToCommandBehavior 没有任何问题。
    • 另外,请确保您已升级所有项目以使用正确的 NuGet 包。
    • @BrianLagunas 使用 Xamarin.Forms.Command 和 Prism 的 DelegateCommand 有区别吗?你用的是哪一个?
    • @LouisaBickley 将 [assembly: XamlCompilation( XamlCompilationOptions.Compile )] 添加到您的 AssemblyInfo.cs 中,效果很好
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-20
    • 1970-01-01
    • 2022-11-23
    • 2011-08-08
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多