首页 > 解决方案 > Xamarin.Forms OpenGLView 在 XAML 页面中使用时不起作用

问题描述

我尝试使用此处提供的简单示例: https : //docs.microsoft.com/en-us/dotnet/api/xamarin.forms.openglview?view=xamarin-forms 尝试使用 OpenGLView 类创建 XAML 视图. 但我遇到了问题。这是 Xamarin.Forms(l,在 iOS 模拟器上运行并使用 Prism 框架、macOS Mojave 和 Visual Studio for Mac 2019 Pro。

这是我的 XAML 代码:

<?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:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
   xmlns:otk="clr-namespace:OpenTK.Graphics.ES3"    
   x:Class="MyApp.Views.OpenGlPage">

   <StackLayout HorizontalOptions="CenterAndExpand" 
       VerticalOptions="CenterAndExpand">
       <OpenGLView x:Name="openGLView" HasRenderLoop="true"/>
       <Button Command="{Binding Display}" Text="Display"/>
   </StackLayout>
</ContentPage>

和背后的代码

using Xamarin.Forms;
using OpenTK.Graphics.ES30;

namespace MyApp.Views
{
    public partial class OpenGlPage : ContentPage
    {
        float red, green, blue;

        public OpenGlPage()
        {
            InitializeComponent();
            Title = "OpenGL";


            openGLView.HeightRequest = 300;
            openGLView.WidthRequest = 300;

            openGLView.OnDisplay = r => {

               GL.ClearColor(red, green, blue, 1.0f);
               GL.Clear((ClearBufferMask.ColorBufferBit | 
ClearBufferMask.DepthBufferBit));

               red += 0.01f;
               if (red >= 1.0f)
                  red -= 1.0f;
               green += 0.02f;
               if (green >= 1.0f)
                  green -= 1.0f;
               blue += 0.03f;
               if (blue >= 1.0f)
                  blue -= 1.0f;
           };
       }
   }
}

ViewModel 没有太多,并且 View 已在 Prism 中正确注册,因此导航到该视图工作正常。

但是,当像这样在 XAML 中放置 OpenGL 时,使用或初始化 OpenGL 的方式似乎存在一些问题。当执行遇到任何 GL.* 调用应用程序崩溃时。这是错误:

=================================================================
    Native Crash Reporting
=================================================================
    Got a SIGSEGV while executing native code. This usually indicates
    a fatal error in the mono runtime or one of the native libraries 
    used by your application.
=================================================================

=================================================================
    Basic Fault Adddress Reporting
=================================================================
    instruction pointer is NULL, skip dumping
=================================================================
    Native stacktrace:
=================================================================
    0x10184704d - /Users/user/Library/Developer/CoreSimulator/Devices/602074EE-E100-4319-B9DE-D9460B6264EB/data/Containers/Bundle/Application/7937567A-2C4D-43A6-8A81-B27BFEF73EF6/Prism1.iOS.app/Prism1.iOS : mono_dump_native_crash_info
    0x10183b7a5 - /Users/user/Library/Developer/CoreSimulator/Devices/602074EE-E100-4319-B9DE-D9460B6264EB/data/Containers/Bundle/Application/7937567A-2C4D-43A6-8A81-B27BFEF73EF6/Prism1.iOS.app/Prism1.iOS : mono_handle_native_crash
    0x10184db43 - /Users/user/Library/Developer/CoreSimulator/Devices/602074EE-E100-4319-B9DE-D9460B6264EB/data/Containers/Bundle/Application/7937567A-2C4D-43A6-8A81-B27BFEF73EF6/Prism1.iOS.app/Prism1.iOS : mono_sigsegv_signal_handler
    0x10f329b5d - /usr/lib/system/libsystem_platform.dylib : _sigtramp
    0x18 - Unknown
    0x135d884bf - Unknown
    0x135d88927 - Unknown
    0x101850fa1 - /Users/user/Library/Developer/CoreSimulator/Devices/602074EE-E100-4319-B9DE-D9460B6264EB/data/Containers/Bundle/Application/7937567A-2C4D-43A6-8A81-B27BFEF73EF6/Prism1.iOS.app/Prism1.iOS : mono_jit_runtime_invoke
    0x10196ab78 - /Users/user/Library/Developer/CoreSimulator/Devices/602074EE-E100-4319-B9DE-D9460B6264EB/data/Containers/Bundle/Application/7937567A-2C4D-43A6-8A81-B27BFEF73EF6/Prism1.iOS.app/Prism1.iOS : mono_runtime_invoke_checked
    0x10196e4ce - /Users/user/Library/Developer/CoreSimulator/Devices/602074EE-E100-4319-B9DE-D9460B6264EB/data/Containers/Bundle/Application/7937567A-2C4D-43A6-8A81-B27BFEF73EF6/Prism1.iOS.app/Prism1.iOS : mono_runtime_invoke
    0x101a8142a - /Users/user/Library/Developer/CoreSimulator/Devices/602074EE-E100-4319-B9DE-D9460B6264EB/data/Containers/Bundle/Application/7937567A-2C4D-43A6-8A81-B27BFEF73EF6/Prism1.iOS.app/Prism1.iOS : xamarin_invoke_trampoline
    0x101a89365 - /Users/user/Library/Developer/CoreSimulator/Devices/602074EE-E100-4319-B9DE-D9460B6264EB/data/Containers/Bundle/Application/7937567A-2C4D-43A6-8A81-B27BFEF73EF6/Prism1.iOS.app/Prism1.iOS : xamarin_arch_trampoline
    0x101a8a761 - /Users/user/Library/Developer/CoreSimulator/Devices/602074EE-E100-4319-B9DE-D9460B6264EB/data/Containers/Bundle/Application/7937567A-2C4D-43A6-8A81-B27BFEF73EF6/Prism1.iOS.app/Prism1.iOS : xamarin_x86_64_common_trampoline
    0x1077a8ffd - /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/Frameworks/GLKit.framework/GLKit : -[GLKView _display:]
    0x112231c21 - /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/Frameworks/QuartzCore.framework/QuartzCore : -[CALayer display]
    0x112244003 - /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/Frameworks/QuartzCore.framework/QuartzCore : _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE
    0x1121b33aa - /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/Frameworks/QuartzCore.framework/QuartzCore : _ZN2CA7Context18commit_transactionEPNS_11TransactionE
    0x1121ea584 - /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/Frameworks/QuartzCore.framework/QuartzCore : _ZN2CA11Transaction6commitEv
    0x117924d8a - /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/PrivateFrameworks/UIKitCore.framework/UIKitCore : _UIApplicationFlushRunLoopCATransactionIfTooLate
    0x117a17f5f - /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/PrivateFrameworks/UIKitCore.framework/UIKitCore : __handleEventQueueInternal
    0x10242ebe1 - /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation : __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__
    0x10242e463 - /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation : __CFRunLoopDoSources0
    0x102428b1f - /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation : __CFRunLoopRun
    0x102428302 - /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation : CFRunLoopRunSpecific
    0x11249f2fe - /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/PrivateFrameworks/GraphicsServices.framework/GraphicsServices : GSEventRunModal
    0x11792aba2 - /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/PrivateFrameworks/UIKitCore.framework/UIKitCore : UIApplicationMain
    0x134910932 - Unknown
    0x1349106c3 - Unknown

=================================================================
    Managed Stacktrace:
=================================================================
  at <unknown> <0xffffffff>
  at UIKit.UIApplication:UIApplicationMain <0x00211>
  at UIKit.UIApplication:Main <0x000b2>
  at UIKit.UIApplication:Main <0x00222>
  at Prism1.iOS.Application:Main <0x00092>
  at <Module>:runtime_invoke_void_object <0x00168>
=================================================================

无论我在 OnDisplay 处理程序中有什么 GL 调用,都会发生这种情况。如果我删除 GL 调用当然一切正常,减去 OpenGL 内容:)

编译警告: 编译警告

标签: openglxamarin.formsxamarin.ios

解决方案


推荐阅读