undefined

ViewModel

    public class MainViewModel : BaseViewModel
    {
        private IDictionary _settings = Application.Current.Properties;

        public override void OnAppearing()
        {
            base.OnAppearing();

            RunReadFlow();
        }

        public override Task OnDisappearing()
        {
            RunSaveFlow();

            return base.OnDisappearing();
        }

        private void RunReadFlow()
        {
            string jsonString = string.Empty;
            string key = nameof(Person);

            if (_settings.ContainsKey(key))
            {
                jsonString = _settings[key] as string;
            }

            Person = JsonConvert.DeserializeObject(jsonString) ?? defaultPerson;
        }

        private void RunSaveFlow()
        {
            string key = nameof(Person);
            _settings[key] = JsonConvert.SerializeObject(Person);
        }
    }

(使用Application.Current.Properties來儲存或讀取設定值,在讀取前如果沒有key會Crash,需先用ContainsKey檢查)


App.xaml.cs

	public partial class App : Application
	{
		public App ()
		{
			InitializeComponent();

			MainPage = new MilitaryCountdown.MainPage();
		}

		protected override void OnStart ()
		{

		}

		protected override void OnSleep ()
		{
            var page = Application.Current.MainPage as BasePage;
            page.OnSleep();
		}

		protected override void OnResume ()
		{
            var page = Application.Current.MainPage as BasePage;
            page.OnResume();
        }
    }

(將目前頁面轉型成BasePage(需先繼承BasePage),然後再Call自定的Method)


BasePage

    public partial class BasePage : ContentPage
    {
        private BaseViewModel _viewModel => BindingContext as BaseViewModel;

        public BasePage()
        {

        }

        public void OnResume()
        {
            OnAppearing();
        }

        public  void OnSleep()
        {
            OnDisappearing();
        }

        protected override void OnAppearing()
        {
            base.OnAppearing();

            _viewModel?.OnAppearing();
        }

        protected override async void OnDisappearing()
        {
            await _viewModel?.OnDisappearing();

            base.OnDisappearing();
        }
    }

(定義OnResume, OnSleep Method,並在裡面Call 該頁面的OnAppearing 與 OnDisappearing,然後裡面會再Call BaseViewModel的Virtual Method)


MainPage

	public partial class MainPage : BasePage
    {
        public MainPage()
        {
            InitializeComponent();
        }
    }

(繼承 BasePage)


BaseViewModel

    public class BaseViewModel : BaseBindable
    {
        public virtual void OnAppearing()
        {

        }

        public virtual Task OnDisappearing()
        {
            return Task.FromResult(false);
        }
    }

(定義OnAppearing 與 OnDisappearing Virtual Method,可讓頁面的ViewModel override)

 

結論:

  這樣就能在App進入背景或前景時,呼叫頁面對應的方法。

 

參考資料:

http://stackoverflow.com/questions/37266797/how-to-store-app-settings-in-xamarin-forms

https://forums.xamarin.com/discussion/comment/94259/#Comment_94259

 

全站熱搜

小賢 發表在 痞客邦 留言(0) 人氣()