2017-08-09 83 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分钟)。

相关问题