首页 > 解决方案 > 如何在 ViewGroup 类中将 UrhoSharp Surface 附加到 LinearLayout

问题描述

我正在尝试将 UrohSharp Surface 添加到LinearLayout继承自的类中ViewGroup(放置在另一个视图的左下角)。

大多数显示在 Android 中实现 UrhoSharp 的示例都是直接在主要活动中完成的,如下所示:

protected override async void OnCreate(Bundle savedInstanceState)
{
            base.OnCreate(savedInstanceState);
            var mLayout = new AbsoluteLayout(this);
            surface = UrhoSurface.CreateSurface(this);// (this, , true);
            mLayout.AddView(surface);

            SetContentView(mLayout);

            dronModel = await surface.Show<DroneScene>(new ApplicationOptions("Data"));
}

或者使用布局文件中的元素:

    <Urho.Droid.UrhoSurfacePlaceholder
        android:id="@+id/DroidModelId"
        android:background="@color/colorPrimary"
        android:layout_height="300dp"
        android:layout_width="match_parent"/>

并像这样替换它:

public override async void OnViewCreated(Android.Views.View view, Bundle savedInstanceState)
{
            base.OnViewCreated(view, savedInstanceState);

            surface = UrhoSurface.CreateSurface(base.Activity);
            UrhoSurfacePlaceholder parent = view.FindViewById<UrhoSurfacePlaceholder>(Resource.Id.DroidModelId);
            parent.AddView(surface);
            droneModel = await surface.Show<DroneModel>(new ApplicationOptions("Data"));

但是,我需要做的是创建某种视图来封装 Urho 代码并以如下方式引用它:

    <My.App.Droid.Views.Drone.Model.DroneModelView
        android:id="@+id/DroneModel2"
        android:layout_marginTop="32dp"
        android:layout_marginLeft="32dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_width="55dp"
        android:layout_height="55dp"
        android:background="@null"/>

然后从主活动视图中抓取它:

droneModelView = FindViewById<DroneModelView>(Resource.Id.DroneModel2);

由于UrhoSurface.CreateSurface需要 Activity 对象,我不确定如何最好地解决这个问题 - 据我所知,没有“干净”的方法可以从类继承中访问活动ViewGroup?最终目标是在包含许多以类似方式引用的其他元素的视图中显示 3D 模型。

标签: androidxamarinxamarin.androidurhosharpurho3d

解决方案


这是一个简单的例子,它在 CustomView 中将 RatingBar 添加到 LinearLayout,您可以参考它,将 RatingBar 更改为您的 UrohSharp Surface

https://stackoverflow.com/a/57764198/10768653


推荐阅读