2017-08-09 7 views
-1

アプリケーションが閉じているかどうかにかかわらず、15分だけボタンを表示したいディスパッチャのタイマーで試してみましたが、リセットされていますアプリケーションが起動されるたびにこれを達成する方法はありますか?ページ内のボタンを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それを行うには良い方法があります。ストーリーボードを使って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

Suspendedイベントが発生しているとき、保存Windows.Storage.ApplicationData.Current.LocalSettingsに経過時間。アプリケーションが再開しているとき、または起動しているとき - >前回の設定から読み込んで開始します。

例:ユーザーが7分を費やしてアプリケーションを閉じる - >この値を設定に保存します。ユーザーがapp - >を起動すると、以前の値(7分)を読み、タイマーを開始します(8分待ち)。

関連する問題