如何在 ViewGroup 类中将 UrhoSharp Surface 附加到 LinearLayout



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

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

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 模型。

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

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

最新更新