在xmlns clr-namespace中找不到EventToCommandBehavior:Prism.Behaviors
我正在研究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();
我的例子:
查看 :
我的自定义网格:
public class TestGrid : Grid { public event EventHandler 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((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中看到的那样,我正在使用来自Xamarin.Forms
命名空间的ICommand
和Command
类:
private ICommand onTestTappedCommand; public ICommand OnTestTappedCommand { get { return onTestTappedCommand ?? (onTestTappedCommand = new Command((foo) => HandleEvent(foo), (foo) => CanExecute(foo))); } }
它适用于Android,但是当我改为DelegateCommand
:
return onTestTappedCommand ?? (onTestTappedCommand = new DelegateCommand((foo) => HandleEvent(foo), (foo) => CanExecute(foo)));
Android不能正常工作。 然后我有一个运行时exception:
未处理的exception:System.Reflection.TargetInvocationException:调用的目标抛出了exception。
和..项目符合,但在Start.xaml.cs我有一个错误:
InitializeComponent()在当前上下文中不存在
PS。 请帮帮我,请不要告诉我清除解决方案/删除bin obj文件夹…它不起作用。
没有init需要。 从您显示的例外情况来看,我的建议是完成清理和重建。 确保删除obj
和bin
文件夹中的所有文件。
视图模型
public DelegateCommand PersonSelectedCommand => new DelegateCommand (OnPersonSelectedCommandExecuted); public ObservableRangeCollection People { get; set; } void OnPersonSelectedCommandExecuted(string name) { _pageDialogService.DisplayAlertAsync("Person Selected", name, "Ok" ); }
视图