Insert a Monogame view inside MvvmCross monodroid Activity

风流意气都作罢 提交于 2019-12-18 07:08:53

问题


I'm trying to build a Monogame view inside a RelativeLayout from my MvvmCross monodroid Activity view.

An android Activity inherits from Microsoft.Xna.Framework.AndroidGameActivity to be able to run a Monogame inside a RelativeLayout (working).

My MvvmCross Activity inherits from MvxBindingActivityView(working).

So, I need a way to run the game and bind some datas within the same activity.

Thanks in advance for your help.


回答1:


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.

e.g. see the #Region and IMvxAndroidView<TViewModel> added to make MvxActivityView.cs from a normal Activity.

e.g. it's the same region and interface used for adapting a specialised Activity like Google's MapActivity into MvxMapActivityView.cs

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.

Code can be written at this level - I've shipped apps without binding - but many users prefer to add DataBinding too...


To add this DataBinding support, you need to add a bit more code which provides BindingInflate, storage of bindings, disposal of bindings, etc.

e.g. a raw MvxActivityView is extended using the IMvxBindingActivity interface and a #region like: MvxBindingActivityView.cs

e.g. MvxMapActivityView is extended using the same region and interface: MvxBindingMapActivityView.cs


So to extend your the custom AndroidGameActivity:

  1. Inherit from AndroidGameActivity to get ViewModelOwningGameActivity<T> and cut and paste the IMvxAndroidView<TViewModel> interface and #region from MvxActivityView<T> to provide the ViewModel methods, fields and properties.

Then assuming you want binding:

  1. Inherit from ViewModelOwningGameActivity<T> to get BindingGameActivity<T> and cut and paste the IMvxBindingActivity and #region from MvxBindingActivityView<T> to get the binding methods

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.


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#



来源:https://stackoverflow.com/questions/13201174/insert-a-monogame-view-inside-mvvmcross-monodroid-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!