我正在开发 Xamarin.Forms 项目。在 Prism 6.3 之前我使用过 6.2Corcav.Behaviors
包裹。我不需要传递参数,所以效果很好。但是,在iOS项目中AppDelegate
我需要运行这一行:
Corcav.Behaviors.Infrastructure.Init();
我有一个评论://添加以防止 iOS 链接器从部署的包中剥离行为程序集。
Now EventToCommand
6.3版本添加了,所以我卸载了Corcav.Behaviors
封装并实现了简单的例子。在 Android 中一切都很好,但 iOS ..我有一个例外:
![enter image description here](https://i.stack.imgur.com/oroWg.png)
我认为这是因为现在我错过了这一行: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);
}
}
视图模型
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 上不行。
问题:
我怎样才能在Infrastruction.Init中Prism.Behaviors
?
EDIT:
我认为错误可能比我想象的要多......正如你在我的 ViewModel 中看到的,我正在使用ICommand
and Command
班级来自Xamarin.Forms
命名空间:
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)));
安卓系统则不太好用。然后我有一个运行时异常:
未处理的异常:System.Reflection.TargetInitationException:
调用目标已引发异常。
并且..该项目符合要求,但在 Start.xaml.cs 中我有一个错误:
InitializeComponent() 在当前上下文中不存在
附言。请帮助我,请不要告诉我清除解决方案/删除 bin obj 文件夹...它不起作用。