问题
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
:
- 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.
Then assuming you want binding:
- Inherit from
ViewModelOwningGameActivity<T>
to getBindingGameActivity<T>
and cut and paste theIMvxBindingActivity
and#region
fromMvxBindingActivityView<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