gpt4 book ai didi

c# - 调用 Device.RuntimePlatform 导致运行时异常

转载 作者:太空狗 更新时间:2023-10-29 18:30:50 27 4
gpt4 key购买 nike

我在所有项目(平台+共享)中将我的 Xamarin.Forms 包更新到最新版本(2.3.4.224),现在看来我不应该再使用 Device.OSTargetPlatform 枚举,因为它们已被弃用。

编译器因为这些行而报错:

if (Device.OS == TargetPlatform.iOS) 
_API_BASE_URI = "http://XXX.XXX.XXX.XXX";
else
_API_BASE_URI = "http://YYY.YYY.YYY.YYY";

上面写着:

"Device.OS is obsolete. Use RuntimePlatform instead"

到目前为止一切顺利,现在我想解决这个问题,我一直在尝试使用:

Debug.WriteLine(Device.RuntimePlatform);

但它会抛出一个运行时异常。这是堆栈跟踪

04-08 14:57:34.812 I/MonoDroid( 3782): UNHANDLED EXCEPTION: 04-08 14:57:34.824 I/MonoDroid( 3782): System.TypeInitializationException: The type initializer for 'Mob.ApiCommunication' threw an exception. ---> System.MissingMethodException: Method 'Xamarin.Forms.Device.get_RuntimePlatform' not found. 04-08 14:57:34.824 I/MonoDroid( 3782): --- End of inner exception stack trace --- 04-08 14:57:34.824 I/MonoDroid( 3782): at (wrapper managed-to-native) System.Object:__icall_wrapper_mono_generic_class_init (intptr) 04-08 14:57:34.824 I/MonoDroid( 3782): at Mob.Views.Public.LoginViewModel.RestoreState (System.Collections.Generic.IDictionary`2[TKey,TValue] dictionary) [0x00001] in C:\Users...\Source...\LoginViewModel.cs:52 04-08 14:57:34.824 I/MonoDroid( 3782): at Mob.App.OnStart () [0x00001] in C:\Users...\App.xaml.cs:39 04-08 14:57:34.824 I/MonoDroid( 3782): at Xamarin.Forms.Application.SendStart () [0x00000] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Core\Application.cs:228 04-08 14:57:34.824 I/MonoDroid( 3782): at Xamarin.Forms.Platform.Android.FormsAppCompatActivity+d__43.MoveNext () [0x0003b] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\AppCompat\FormsAppCompatActivity.cs:426 04-08 14:57:34.824 I/MonoDroid( 3782): --- End of stack trace from previous location where exception was thrown --- 04-08 14:57:34.824 I/MonoDroid( 3782): at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Users/builder/data/lanes/4468/f913a78a/source/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143 04-08 14:57:34.824 I/MonoDroid( 3782): at System.Runtime.CompilerServices.AsyncMethodBuilderCore.m__0 (System.Object state) [0x00000] in /Users/builder/data/lanes/4468/f913a78a/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1018 04-08 14:57:34.824 I/MonoDroid( 3782): at Android.App.SyncContext+c__AnonStorey0.<>m__0 () [0x00000] in /Users/builder/data/lanes/4468/b16fb820/source/xamarin-android/src/Mono.Android/Android.App/SyncContext.cs:35 04-08 14:57:34.824 I/MonoDroid( 3782): at Java.Lang.Thread+RunnableImplementor.Run () [0x0000b] in /Users/builder/data/lanes/4468/b16fb820/source/xamarin-android/src/Mono.Android/Java.Lang/Thread.cs:36 04-08 14:57:34.824 I/MonoDroid( 3782): at Java.Lang.IRunnableInvoker.n_Run (System.IntPtr jnienv, System.IntPtr native__this) [0x00009] in /Users/builder/data/lanes/4468/b16fb820/source/monodroid/src/Mono.Android/platforms/android-25/src/generated/Java.Lang.IRunnable.cs:81 04-08 14:57:34.824 I/MonoDroid( 3782): at (wrapper dynamic-method) System.Object:88db5e57-5ac7-4ba4-a574-4ec5eaf704fd (intptr,intptr)

我在使用 RuntimePlatform 时是否遗漏了什么?我环顾四周,但目前所有关于 Device 类的文档/示例都在使用已弃用的成员。

最佳答案

我想出了一个解决方案。奇怪的是,在 SO 上发帖之前,我已经为我的 Xamarin 项目做了这件事,但没有任何效果。但是这一次,在阅读 Xamarin 论坛上的这个帖子后:https://forums.xamarin.com/discussion/92455/xamarin-forms-2-3-4-224这就是我所做的:

I've closed VisualStudio, cleaned all "bin" & "obj" folders from all projects in my solution, then restarted VS and then cleaned & rebuilt solution.

现在 Debug.WriteLine(Device.RuntimePlatform); 按预期返回“Android”字符串!

关于c# - 调用 Device.RuntimePlatform 导致运行时异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43294671/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com