GalaSoft.MvvmLight的AppCompatActivityBase的棱镜版本是什么?

时间:2019-09-09 23:34:57

标签: android xamarin xamarin.forms prism mvvm-light

我正在将Xamarin.Android中支持GalaSoft.MVVMLight的应用重写为Prism。

此Android项目具有从AppCompatActivityBase继承的特定类GalaSoft.MVVMLight AppCompatActivity

public class AppCompatActivityBase : AppCompatActivity
    {
        public AppCompatActivityBase()
        {
        }

        internal string ActivityKey
        {
            get;
            private set;
        }

        /// <summary>
        /// The activity that is currently in the foreground.
        /// </summary>
        public static AppCompatActivityBase CurrentActivity
        {
            get;
            private set;
        }

        internal static string NextPageKey
        {
            get;
            set;
        }

        /// <summary>
        /// If possible, discards the current page and displays the previous page
        /// on the navigation stack.
        /// </summary>
        public static void GoBack()
        {
            if (CurrentActivity != null)
            {
                CurrentActivity.OnBackPressed();
            }
        }

        /// <summary>
        /// Overrides <see cref="M:Android.App.Activity.OnResume" />. If you override
        /// this method in your own Activities, make sure to call
        /// base.OnResume to allow the <see cref="T:GalaSoft.MvvmLight.Views.NavigationService" />
        /// to work properly.
        /// </summary>
        protected override void OnResume()
        {
            CurrentActivity = this;
            if (string.IsNullOrEmpty(ActivityKey))
            {
                ActivityKey = NextPageKey;
                NextPageKey = null;
            }
            base.OnResume();
        }

        /// <summary>
        /// Overrides <see cref="M:Android.App.Activity.OnCreate" />. If you override
        /// this method in your own Activities, make sure to call
        /// base.OnCreate to allow the <see cref="T:GalaSoft.MvvmLight.Views.NavigationService" />
        /// to work properly.
        /// </summary>
        protected override void OnCreate(Bundle savedInstanceState)
        {
            // Set CurrentActivity to the first activity that is created
            if (CurrentActivity == null)
            {
                CurrentActivity = this;
                if (string.IsNullOrEmpty(ActivityKey))
                {
                    ActivityKey = NextPageKey;
                    NextPageKey = null;
                }
            }
            base.OnCreate(savedInstanceState);
        }
    }

然后在用户首选项类中引用此AppCompatActivityBase,如下所示。

   class UserPreferences : IUserPreferences
    {
        const int MaximumHistoryEntries = 5;
        readonly ISharedPreferences preferences;
        readonly ISharedPreferencesEditor editor;

        public UserPreferences()
        {
            preferences = PreferenceManager.GetDefaultSharedPreferences(AppCompatActivityBase.CurrentActivity.ApplicationContext);
            editor = preferences.Edit();
        }

        public event EventHandler Saved;

        public string AssetsExtractedVersion
        {
            get
            {
                return preferences.GetString(AppCompatActivityBase.CurrentActivity.GetString(Resource.String.PrefKeyAssetsExtractedVersion), null);
            }
            set
            {
                editor.PutString(AppCompatActivityBase.CurrentActivity.GetString(Resource.String.PrefKeyAssetsExtractedVersion), value);
            }
        }

        public void Save()
        {
            editor.Commit();
            Saved?.Invoke(this, EventArgs.Empty);
        }

    }

因为我的项目现在支持Prism而不是GalaSoft.MVVMLight。如何在Prism中替换AppCompatActivityBase?我的背景是iOS,所以我很难理解Xamarin / Prism的Android版本。

1 个答案:

答案 0 :(得分:1)

没有一个。虽然Prism确实具有特定于Android的二进制文件,以使用Xamarin.Forms Dependency Resolver来支持依赖注入,其中可能包含Android.Content.Context,但Prism本身完全依赖Xamarin.Forms,并且在所有平台上都可以完全相同地运行。因此,不需要进行棱镜专用的基本活动