1

到目前为止,我在我的应用程序开始时启动了一个 LoginScreen(在启动之后):

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

    }

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

好吧,现在我需要改变它。在此LoginView上,我正在从 Web 服务填充数据。这意味着,我已经需要设置 Webservice Url(在我的情况下是 PreferenceActivity)。

所以我想要那个 PreferenceScreen,作为StartView/Activity(不再是 Login One)。

偏好活动:

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

我不知道如何完成这项任务,因为 PreferenceScreen 没有 ViewModel,所以如何调用活动StartApplicationObject.cs,还是我需要 WorkAround?也许我还应该补充一点,我需要能够稍后从 PreferenceActivity 导航到 LoginView(Model).. 在这种情况下也是.. 怎么做?

任何帮助,将不胜感激!

编辑:

谢谢斯图尔特的答案!,我尝试了你的第二种方法 - 创建我自己的 MvxPreferenceActivity。它看起来像这样:

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
    }
}

但我不断收到以下错误:

'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?)   

这个错误也重复出现:this.OnViewCreate(); , this.OnViewNewIntent(); , this.OnViewNewIntent(); , this.OnViewResume(); , this.OnViewStart(); , this.OnViewRestart(); 和 this.OnViewStop();

最后,还有其他 3 个错误:

No overload for method 'OnViewCreate' takes 0 arguments 

对于 OnViewCreate() 和 OnViewNewIntent() ..

我从你那里拿了代码,原样 - 但似乎有些东西不存在?

4

1 回答 1

1

如果您愿意,您可以在 MvvmCross 之外执行此操作。

看看提供一个特殊的 SplashScreen 来替换 SplashScreen - https://github.com/slodge/MvvmCross/blob/vnext/Cirrious/Cirrious.MvvmCross.Droid/Views/MvxBaseSplashScreenActivity.cs与您自己的功能。

然后在您的 SettingsShowActivity 中 - 当您完成特殊设置后 - 您可以触发 MvvmCross IMvxStartNavigation 操作。


不过……话虽如此……

我可能会通过为 SettingsShowActivity 创建一个 ViewModel 并将其集成到您的正常 MvvmCross 应用程序流中来实现这一点。

如果问题是您需要 PreferenceActivity 的 Mvx 版本,请考虑创建 MvxPreferenceActivity - 请参阅在MvvmCross monodroid Activity 中插入 Monogame 视图中的答案以提供帮助。

于 2012-12-04T16:59:14.213 回答