简体   繁体   中英

MonoDroid & MVVMCross - StartApplicationObject, Start a PreferenceActivity?

Until now I started a LoginScreen (after the splash) at the beginning of my app:

public class StartApplicationObject
        : MvxApplicationObject
        , IMvxStartNavigation
{
    public void Start()
    {
        //this.RequestNavigate<AddressSearchViewModel>();
        //this.RequestNavigate<MainScreenViewModel>();
        this.RequestNavigate<LoginViewModel>();

    }

    public bool ApplicationCanOpenBookmarks
    {
        get { return true; }
    }
}

Well, now I need to change that. On this LoginView I'm filling data from a Webservice. That means, that I already need to set the Webservice Url (in my case in a PreferenceActivity).

So I want that PreferenceScreen, as the StartView/Activity (not the Login One Anymore).

The PreferenceActivity:

[Activity]
public class SettingsShowActivity : PreferenceActivity
{
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        AddPreferencesFromResource(Resource.Xml.PreferenceScreen);
    }
}

I have no Idea how to accomplish this task, as the PreferenceScreen doesn't have a ViewModel, so how to call the activity in StartApplicationObject.cs , or do I need a WorkAround? Maybe I should also add, that I need to be able, to navigate later from the PreferenceActivity to the LoginView(Model).. well in this case also.. how to do that?

Any help would be appreciated!

EDIT:

Thx Stuart for the answer!, I tried your second approach - creating my own MvxPreferenceActivity. It looks like that:

using System;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Preferences;
using Cirrious.MvvmCross.Android.Interfaces;
using Cirrious.MvvmCross.ExtensionMethods;
using Cirrious.MvvmCross.Interfaces.ServiceProvider;
using Cirrious.MvvmCross.Interfaces.ViewModels;
using Cirrious.MvvmCross.Platform.Diagnostics;

namespace INMobileCRM4Android
{
    public abstract class MvxPreferenceActivity<TViewModel>
    : PreferenceActivity
    , IMvxAndroidView<TViewModel>
    , IMvxServiceConsumer<IMvxIntentResultSink>
    where TViewModel : class, IMvxViewModel
    {
        protected MvxPreferenceActivity()
        {
            IsVisible = true;
        }

        #region Common code across all android views - one case for multiple inheritance?

        private TViewModel _viewModel;

        public Type ViewModelType
        {
            get { return typeof(TViewModel); }
        }

        public bool IsVisible { get; private set; }

        public TViewModel ViewModel
        {
            get { return _viewModel; }
            set
            {
                _viewModel = value;
                OnViewModelSet();
            }
        }

        public void MvxInternalStartActivityForResult(Intent intent, int requestCode)
        {
            base.StartActivityForResult(intent, requestCode);
        }

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            this.OnViewCreate();
        }

        protected override void OnDestroy()
        {
            this.OnViewDestroy();
            base.OnDestroy();
        }

        protected override void OnNewIntent(Intent intent)
        {
            base.OnNewIntent(intent);
            this.OnViewNewIntent();
        }

        protected abstract void OnViewModelSet();

        protected override void OnResume()
        {
            base.OnResume();
            IsVisible = true;
            this.OnViewResume();
        }

        protected override void OnPause()
        {
            this.OnViewPause();
            IsVisible = false;
            base.OnPause();
        }

        protected override void OnStart()
        {
            base.OnStart();
            this.OnViewStart();
        }

        protected override void OnRestart()
        {
            base.OnRestart();
            this.OnViewRestart();
        }

        protected override void OnStop()
        {
            this.OnViewStop();
            base.OnStop();
        }

        public override void StartActivityForResult(Intent intent, int requestCode)
        {
            switch (requestCode)
            {
                case (int)MvxIntentRequestCode.PickFromFile:
                    MvxTrace.Trace("Warning - activity request code may clash with Mvx code for {0}", (MvxIntentRequestCode)requestCode);
                    break;
                default:
                    // ok...
                    break;
            }
            base.StartActivityForResult(intent, requestCode);
        }

        protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
        {
            this.GetService<IMvxIntentResultSink>().OnResult(new MvxIntentResultEventArgs(requestCode, resultCode, data));
            base.OnActivityResult(requestCode, resultCode, data);
        }

        #endregion
    }
}

But I keep getting the following errors:

'INMobileCRM4Android.MvxPreferenceActivity<TViewModel>' does not contain a definition for 'OnViewPause' and no extension method 'OnViewPause' accepting a first argument of type 'INMobileCRM4Android.MvxPreferenceActivity<TViewModel>' could be found (are you missing a using directive or an assembly reference?)   

And this error repeats also for: this.OnViewCreate(); , this.OnViewNewIntent(); , this.OnViewNewIntent(); , this.OnViewResume(); , this.OnViewStart(); , this.OnViewRestart(); and this.OnViewStop();

And at the end, there are 3 other errors:

No overload for method 'OnViewCreate' takes 0 arguments 

For OnViewCreate() and OnViewNewIntent() ..

I took the code from you, as it was - but seems some things are absent?

You can do this outside of MvvmCross if you want to.

Look at providing a special SplashScreen which replaces the SplashScreen - https://github.com/slodge/MvvmCross/blob/vnext/Cirrious/Cirrious.MvvmCross.Droid/Views/MvxBaseSplashScreenActivity.cs with your own functionality.

Then in your SettingsShowActivity - when you've finished with your special setup - then you can trigger the MvvmCross IMvxStartNavigation operation.


However... having said that...

I'd probably implement this by making a ViewModel for the SettingsShowActivity and integrating this into your normal MvvmCross application flow.

If the problem is that you need an Mvx version of PreferenceActivity then consider creating an MvxPreferenceActivity - see the answer in Insert a Monogame view inside MvvmCross monodroid Activity to help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM