带有arcore的Xamarin android自定义视图

问题描述

我看了这个视频,想用 Android 版的 Arcore 制作一个简单的 AR:

Augmented Reality with Arcore

一切顺利,但现在我想在自定义视图中以 xamarin 形式查看此内容。 但我不能:(

我最后试过的: 在表单中:

   <?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:controls="clr-namespace:FormsAR.Controls"
         x:Class="FormsAR.MainPage">

    <StackLayout BackgroundColor="Red"  HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
        <Label BackgroundColor="Blue" TextColor="White" Text="JUST TEST"/>
        <controls:ARView HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" BackgroundColor="Yellow"/>
    </StackLayout>

 </ContentPage>

还有:

using System;
using Xamarin.Forms;

namespace FormsAR.Controls
{
    public class ARView : View
    {
    }
}

和安卓:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
    android:id="@+id/fragmentAR"
    android:name="com.google.ar.SceneForm.ux.ArFragment"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/>
</LinearLayout>

最后:

[assembly: ExportRenderer(typeof(ARView),typeof(ARVieWrenderer))]
namespace FormsAR.Droid.Controls
{
public class ARVieWrenderer : VieWrenderer<ARView,Android.Views.View>
{      
    private ArFragment arFragment;
    private Material readyColor;
    Android.Views.View _view;

    public ARVieWrenderer(Context context) : base(context){}

    protected override void OnElementChanged(ElementChangedEventArgs<ARView> e)
    {
        base.OnElementChanged(e);
        _view = LayoutInflater.From(this.Context).Inflate(Resource.Layout.ARLayout,null,false);
        AddView(_view);
        if (Control == null)
        {
            Initialize();    
        } 
    }

    private void Initialize()
    {
        var activity = this.Context as AppCompatActivity;
        arFragment = (ArFragment)activity.SupportFragmentManager.FindFragmentById(Resource.Id.fragmentAR);
        setupPlane();
        MaterialFactory.MakeOpaqueWithColor(Context,new Google.AR.Sceneform.rendering.Color(Android.Graphics.Color.Yellow)).GetAsync().ContinueWith(materialTask =>
        {
            readyColor = (Material)materialTask.Result;
        });
    }

    private void setupPlane()
    {
        arFragment.TapArPlane += (sender,args) => this.OnTapArPlaneListener(args.HitResult,args.Plane,args.MotionEvent);
    }

    private void OnTapArPlaneListener(HitResult hitResult,Plane plane,MotionEvent motionEvent)
    {
        Anchor anchor = hitResult.CreateAnchor();
        AnchorNode anchorNode = new AnchorNode(anchor);
        anchorNode.SetParent(arFragment.ArSceneView.Scene);
        CreateModel(anchorNode);
    }

    private void CreateModel(AnchorNode anchorNode)
    {
        TransformableNode node = new TransformableNode(arFragment.TransformationSystem);
        node.SetParent(anchorNode);
        var nodeRenderable = ShapeFactory.MakeSphere(0.08f,new Google.AR.SceneForm.Math.Vector3(0.0f,0.15f,0.0f),readyColor);
        node.Renderable = nodeRenderable;
        node.Select();
    }
}       

}

但我看不到: image result

Link for project

你能帮我找出错误在哪里吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)