Timer 在UWP应用程序中仅在页面中显示按钮15分钟

Timer 在UWP应用程序中仅在页面中显示按钮15分钟,timer,uwp,windows-store-apps,win-universal-app,Timer,Uwp,Windows Store Apps,Win Universal App,无论应用程序是否关闭,我只想在页面中显示该按钮15分钟。我已尝试使用dispatcher计时器,但每次启动应用程序时都会重置该时间。有没有办法做到这一点?您可以使用LocalSettings: public sealed partial class MainPage : Page { private const string _timestampKey = "timestamp"; public MainPage() { this.Initialize

无论应用程序是否关闭,我只想在页面中显示该按钮15分钟。我已尝试使用dispatcher计时器,但每次启动应用程序时都会重置该时间。有没有办法做到这一点?

您可以使用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。

@user37779有一个很好的方法。我想我可以用故事板在15分钟内隐藏按钮

我应该写一个xaml

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

当发生
挂起
事件时,将运行时间保存到
Windows.Storage.ApplicationData.Current.LocalSettings
。当应用程序正在恢复或启动时->从设置中读取上一次并从中启动


例如:用户花费7分钟关闭应用程序->将此值保存到设置中。当用户启动应用程序->您读取上一个值(7分钟)并启动计时器(等待8分钟)。

您可以存储初始启动时间,然后比较计时器中的偏移量,因此如果存储的时间超过15分钟,则不显示按钮,然后外部检查该时间,因此也不要启动计时器,类似的东西可能有用吗?@JustinXL如果关闭将关闭按钮的应用程序,@JustinXL尝试使用user3777939和我的方式。
  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();
    }