2017-08-09 81 views
-1

我想在頁面中僅顯示按鈕十五分鐘,而不管應用程序是關閉還是不關閉。我嘗試了調度程序計時器,但時間已經重置每次應用程序啓動時。有什麼辦法可以做到這一點?在UWP應用程序中僅顯示頁面中的按鈕15分鐘

+0

您可以存儲初始開始時間,然後比較定時器中的偏移量,所以如果存儲時間超過15分鐘,那麼不要顯示按鈕,然後在該時間外部檢查,因此不要啓動定時器,這樣的東西可能會工作? – RoguePlanetoid

回答

2

您可以使用LocalSettings:

public sealed partial class MainPage : Page 
{ 

    private const string _timestampKey = "timestamp"; 

    public MainPage() 
    { 
     this.InitializeComponent(); 
    } 

    protected override void OnNavigatedTo(NavigationEventArgs e) 
    { 
     base.OnNavigatedTo(e); 
     Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings; 

     DateTime started; 

     if (localSettings.Values.ContainsKey(_timestampKey)) 
     { 
      started = DateTime.ParseExact(localSettings.Values[_timestampKey].ToString(), "O", CultureInfo.InvariantCulture); 
     } 
     else 
     { 
      started = DateTime.Now; 
      localSettings.Values[_timestampKey] = started.ToString("O"); 
     } 

     System.Diagnostics.Debug.WriteLine("First launch: " + started.ToString("O")); 
    } 
} 

,然後只用DispathcerTimer像之前。

0

@ user37779有一個很好的方法來做到這一點。我想我可以使用Storyboard在15分鐘內隱藏按鈕。

我應該寫一個xaml。

<Button Name="Button" Margin="10,10,10,10" Content="123"></Button> 

我會在代碼中寫一個故事板。

public MainPage() 
    { 
     InitializeComponent(); 

     var storyboard = new Storyboard(); 
     var animation = new ObjectAnimationUsingKeyFrames(); 
     animation.KeyFrames.Add(new DiscreteObjectKeyFrame() 
     { 
      KeyTime = new KeyTime(), 
      Value = Visibility.Collapsed 
     }); 
     Storyboard.SetTarget(animation, Button); 
     Storyboard.SetTargetProperty(animation, "Visibility"); 
     animation.EnableDependentAnimation = true; 
     storyboard.BeginTime=TimeSpan.FromMinutes(15); 
     storyboard.Children.Add(animation); 
     storyboard.Completed += Storyboard_Completed; 
     storyboard.Begin(); 
    } 
+2

如果關閉應用程序,這將不起作用。 –

+0

@JustinXL如果關閉將關閉按鈕的應用程序。 – lindexi

+0

@JustinXL嘗試使用user3777939和我的方式。 – lindexi

0

保存經過時間Windows.Storage.ApplicationData.Current.LocalSettingsSuspended事件發生。當應用程序正在恢復或啓動時 - >從設置中讀取前一時間並從中啓動。

例如:用戶花費7分鐘並關閉應用程序 - >將此值保存到設置。當用戶啓動應用程序 - >您讀取以前的值(7分鐘)並啓動計時器(等待8分鐘)。

相關問題