问题描述
我正在尝试从我的MvvmCross独角兽活动视图中的RelativeLayout内部构建一个Monogame视图.
I'm trying to build a Monogame view inside a RelativeLayout from my MvvmCross monodroid Activity view.
一个Android Activity继承自Microsoft.Xna.Framework.AndroidGameActivity,从而能够在RelativeLayout(运行)中运行Monogame.
An android Activity inherits from Microsoft.Xna.Framework.AndroidGameActivity to be able to run a Monogame inside a RelativeLayout (working).
我的MvvmCross活动从MvxBindingActivityView(工作)继承.
My MvvmCross Activity inherits from MvxBindingActivityView(working).
因此,我需要一种方法来运行游戏并在同一活动中绑定一些数据.
So, I need a way to run the game and bind some datas within the same activity.
预先感谢您的帮助.
推荐答案
松散地说,您可以通过继承一些接口,然后剪切并粘贴少量执行基本加载和分配的代码,将任何Activity转换为MvxActivity.的ViewModel.
Loosely speaking, you can translate any Activity to an MvxActivity by inheriting some interfaces and by then cutting and pasting a small amount of code which does the basic loading and assignation of the ViewModel.
例如请参阅添加的#Region
和IMvxAndroidView<TViewModel>
以使 MvxActivityView.cs 来自普通的Activity
.
e.g. see the #Region
and IMvxAndroidView<TViewModel>
added to make MvxActivityView.cs from a normal Activity
.
例如这是用于将专门的Activity(例如Google的MapActivity
)改编为 MvxMapActivityView.cs
e.g. it's the same region and interface used for adapting a specialised Activity like Google's MapActivity
into MvxMapActivityView.cs
在此级别上,活动/视图"具有ViewModel
,可以在C#代码中使用,但是没有巧妙的xml膨胀-它没有巧妙的Binding
支持.
At this level, the Activity/View has a ViewModel
which can be used in C# code, but has no clever xml inflation - it has no clever Binding
support.
可以在此级别编写代码-我交付的应用程序没有绑定-但许多用户也喜欢添加DataBinding ...
Code can be written at this level - I've shipped apps without binding - but many users prefer to add DataBinding too...
要添加此DataBinding支持,您需要添加更多代码来提供BindingInflate,绑定存储,绑定处理等.
To add this DataBinding support, you need to add a bit more code which provides BindingInflate, storage of bindings, disposal of bindings, etc.
例如使用IMvxBindingActivity
界面和#region扩展原始的MvxActivityView
: MvxBindingActivityView.cs
e.g. a raw MvxActivityView
is extended using the IMvxBindingActivity
interface and a #region like: MvxBindingActivityView.cs
例如MvxMapActivityView
使用相同的区域和界面扩展: MvxBindingMapActivityView.cs
e.g. MvxMapActivityView
is extended using the same region and interface: MvxBindingMapActivityView.cs
因此,您可以扩展自定义AndroidGameActivity
:
So to extend your the custom AndroidGameActivity
:
- 从
AndroidGameActivity
继承以获取ViewModelOwningGameActivity<T>
,并从MvxActivityView<T>
剪切并粘贴IMvxAndroidView<TViewModel>
接口和#region
以提供ViewModel方法,字段和属性.
- Inherit from
AndroidGameActivity
to getViewModelOwningGameActivity<T>
and cut and paste theIMvxAndroidView<TViewModel>
interface and#region
fromMvxActivityView<T>
to provide the ViewModel methods, fields and properties.
然后假设您要绑定:
- 从
ViewModelOwningGameActivity<T>
继承以获取BindingGameActivity<T>
,并从MvxBindingActivityView<T>
剪切并粘贴IMvxBindingActivity
和#region
以获得绑定方法
- Inherit from
ViewModelOwningGameActivity<T>
to getBindingGameActivity<T>
and cut and paste theIMvxBindingActivity
and#region
fromMvxBindingActivityView<T>
to get the binding methods
对于专业活动,您可能需要添加更多内容-例如您可以为MapActivity添加一些自定义帮助程序方法以绘制点和线,或者为GameActivity添加无论游戏做什么 ...,但这取决于各个实现.
For specialist Activities you may want to add more - e.g. you may could add some custom helper methods for the MapActivity to plot points and lines, or for GameActivity to do whatever games do... but this is up to individual implementations.
很抱歉,改编活动"时需要剪切和粘贴代码-我已尝试将其降至最低.但是,到目前为止,编写Mvx确实是我真正想要使用C#进行多重继承或Mixins的一遍.
Sorry about the cut and paste of code required in adapting Activities - I have tried to keep this to a minimum. However, writing Mvx is the one time so far that I've really wanted Multiple Inheritance or Mixins in C#
这篇关于在MvvmCross monodroid活动内插入Monogame视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!