到目前为止,我在我的应用程序开始时启动了一个 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() ..
我从你那里拿了代码,原样 - 但似乎有些东西不存在?