对于我使用Xamarin.Forms编写的应用程序,我使用Result对象,因此在查询服务器端代码时可以处理正面和负面结果:
namespace GenerationLondon
{
public abstract class Result<E, S>
{
private Result() {}
public abstract R fold<R>(Func<E, R> left, Func<S, R> right);
public abstract void consume (Action<E> left, Action<S> right);
public static Result<E, S> success(S value)
{
return new Success (value);
}
public static Result<E, S> error(E value)
{
return new Error (value);
}
public class Success : Result<E, S>
{
private S success;
public Success(S success)
{
this.success = success;
}
public override R fold<R> (Func<E, R> left, Func<S, R> right)
{
return right.Invoke(success);
}
public override void consume (Action<E> left, Action<S> right)
{
right.Invoke (success);
}
}
public class Error : Result<E, S>
{
private E error;
public Error(E error)
{
this.error = error;
}
public override R fold<R> (Func<E, R> left, Func<S, R> right)
{
return left.Invoke (error);
}
public override void consume (Action<E> left, Action<S> right)
{
left.Invoke (error);
}
}
}
}
用户成功登录后,我向服务器发出2个请求以收集一些信息,但我一直收到以下异常:
System.InvalidProgramException: Invalid IL code in GenerationLondon.LoginPage/<OnLogin>c__asyncD/<OnLogin>c__AnonStorey14:<>m__1 (System.Collections.Generic.List`1<GenerationLondon.UserEvent>): IL_0008: stfld 0x040000ef
at GenerationLondon.Result`2+Success[System.String,System.Collections.Generic.List`1[GenerationLondon.UserEvent]].consume (System.Action`1 left, System.Action`1 right) [0x00008] in /Users/tom/Documents/workspace/GenerationLondon/GenerationLondon/Result.cs:39
at GenerationLondon.LoginPage+<OnLogin>c__asyncD.MoveNext () [0x001df] in /Users/tom/Documents/workspace/GenerationLondon/GenerationLondon/Views/LoginPage.xaml.cs:30
at --- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000b] in /Users/builder/data/lanes/2320/1f068b49/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/corlib/System.Runtime.ExceptionServices/ExceptionDispatchInfo.cs:61
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<ThrowAsync>m__0 (System.Object state) [0x00000] in /Users/builder/data/lanes/2320/1f068b49/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/external/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1006
at UIKit.UIKitSynchronizationContext+<Post>c__AnonStorey0.<>m__0 () [0x00000] in /Users/builder/data/lanes/2320/1f068b49/source/maccore/src/UIKit/UIKitSynchronizationContext.cs:24
at Foundation.NSAsyncActionDispatcher.Apply () [0x00000] in /Users/builder/data/lanes/2320/1f068b49/source/maccore/src/Foundation/NSAction.cs:163
at at (wrapper managed-to-native) UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
at UIKit.UIApplication.Main (System.String[] args, IntPtr principal, IntPtr delegate) [0x00005] in /Users/builder/data/lanes/2320/1f068b49/source/maccore/src/UIKit/UIApplication.cs:77
at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00038] in /Users/builder/data/lanes/2320/1f068b49/source/maccore/src/UIKit/UIApplication.cs:61
at GenerationLondon.iOS.Application.Main (System.String[] args) [0x00008] in enter code here/Users/tom/Documents/workspace/GenerationLondon/iOS/Main.cs:17
enter code here
有问题的代码如下:
result.consume(async () => {
Result<string, Details> detailsResult = await restService.getDetails ();
Result<string, List<Event>> eventsResult = await restService.getEvents();
eventsResult.consume(async error => await DisplayAlert("Problem communicating with the server", error, "Ok"), events => {
detailsResult.consume(async error => await DisplayAlert("Problem communicating with the server", error, "Ok"), details => {
loginManager.ShowMainPage (details, events);
});
});
}, async error => await DisplayAlert("Login Issue", error, "Ok"));
单独运行时代码可以正常运行,但在iOS模拟器上引入detailsResult后尝试处理eventsResult时会出现此异常。两个调用都成功返回,异常本身来自return right.Invoke(success);
我唯一能想到的是Mono遇到了类型问题。
有什么想法吗?