到目前为止,我在应用程序开头启动了一个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服务中填充数据。 这意味着,我已经需要设置Web服务 Url (在我的情况下是在PreferenceActivity中)。
所以我想要PreferenceScreen,作为 StartView / Activity (不再是Login One)。
PreferenceActivity:
[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(模型)..在这种情况下也好..怎么做?
任何帮助将不胜感激!
修改
Thx Stuart的答案!我尝试了你的第二种方法 - 创建我自己的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()..
我从你那里拿走了你的代码 - 但似乎有些东西不存在了吗?
答案 0 :(得分:1)
如果您愿意,可以在MvvmCross之外执行此操作。
看看提供一个特殊的SplashScreen,用您自己的功能取代SplashScreen - https://github.com/slodge/MvvmCross/blob/vnext/Cirrious/Cirrious.MvvmCross.Droid/Views/MvxBaseSplashScreenActivity.cs。
然后在您的SettingsShowActivity中 - 当您完成特殊设置 - 然后您可以触发MvvmCross IMvxStartNavigation操作。
然而......说过......
我可能通过为SettingsShowActivity创建一个ViewModel并将其集成到您的普通MvvmCross应用程序流中来实现这一点。
如果问题是您需要Mvx版本的PreferenceActivity,请考虑创建MvxPreferenceActivity - 请参阅Insert a Monogame view inside MvvmCross monodroid Activity中的答案以提供帮助。