【发布时间】: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 命名空间的 ICommand 和 Command 类:
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