[英]MonoDroid & MVVMCross - StartApplicationObject, Start a PreferenceActivity?
Until now I started a LoginScreen (after the splash) at the beginning of my app: 到目前为止,我在应用程序的开头启动了LoginScreen(启动后):
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.
在此LoginView上,我正在从Web服务填充数据。 That means, that I already need to set the Webservice Url (in my case in a PreferenceActivity).
这意味着,我已经需要设置Webservice Url (在我的情况下为PreferenceActivity)。
So I want that PreferenceScreen, as the StartView/Activity (not the Login One Anymore). 因此,我希望使用该PreferenceScreen作为StartView / Activity (不再是Login One)。
The PreferenceActivity: 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? 我不知道如何完成此任务,因为PreferenceScreen没有ViewModel,那么如何在
StartApplicationObject.cs
调用活动,还是我需要一个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? 也许我还应该补充一点,我需要能够稍后从PreferenceActivity导航到LoginView(Model)..在这种情况下也是如此..该怎么做?
Any help would be appreciated! 任何帮助,将不胜感激!
EDIT: 编辑:
Thx Stuart for the answer!, I tried your second approach - creating my own MvxPreferenceActivity. Thx Stuart寻求答案!,我尝试了第二种方法-创建自己的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.OnViewCreate(); , this.OnViewNewIntent();
,this.OnViewNewIntent(); , this.OnViewNewIntent();
,this.OnViewNewIntent(); , this.OnViewResume();
,this.OnViewResume(); , this.OnViewStart();
,this.OnViewStart(); , this.OnViewRestart();
,this.OnViewRestart(); and this.OnViewStop();
和this.OnViewStop();
And at the end, there are 3 other errors: 最后,还有其他3个错误:
No overload for method 'OnViewCreate' takes 0 arguments
For OnViewCreate() and OnViewNewIntent() .. 对于OnViewCreate()和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. 如果需要,可以在MvvmCross之外执行此操作。
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. 看一下提供一个特殊的SplashScreen,它用您自己的功能替换了SplashScreen- https://github.com/slodge/MvvmCross/blob/vnext/Cirrious/Cirrious.MvvmCross.Droid/Views/MvxBaseSplashScreenActivity.cs 。
Then in your SettingsShowActivity - when you've finished with your special setup - then you can trigger the MvvmCross IMvxStartNavigation operation. 然后,在SettingsShowActivity中-完成特殊设置后-然后可以触发MvvmCross IMvxStartNavigation操作。
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. 我可能会通过为SettingsShowActivity创建一个ViewModel并将其集成到常规MvvmCross应用程序流中来实现此目的。
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. 如果问题是您需要Mvx版本的PreferenceActivity,则考虑创建MvxPreferenceActivity-请参阅在MvvmCross monodroid活动内插入Monogame视图中的答案以提供帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.