問題
若想要在頁面畫面中,能夠做出一個定時更新內容的效果,通常我們會使用 Timer 這個物件來處理;可以,您會發現到在 Xamarin 的核心 PCL 專案內,找不到這個物件,那麼,要如何做到這樣的效果呢?
解答
這個問題有很多解法,在這裡,我們使用 Xamarin.Forms 提供的核心功能方法
            <Label
                VerticalOptions="End" VerticalTextAlignment="End"
                HorizontalOptions="End"
                Margin="0,0,14,10"
                FontSize="14"
                Text="{Binding 使用者時間}"
                TextColor="White"
                />
        protected override void OnAppearing()
        {
            base.OnAppearing();
            fooHomePageViewModel.繼續執行定時器 = true;
            Device.StartTimer(TimeSpan.FromMilliseconds(500), () =>
            {
                    fooHomePageViewModel.使用者時間 = DateTime.Now.ToString("yyyy/MM/dd HH:mm");
                return fooHomePageViewModel.繼續執行定時器;
            });
        }
        protected override void OnDisappearing()
        {
            base.OnDisappearing();
            fooHomePageViewModel.繼續執行定時器 = false;
        }

 
 
