如何创建一个按钮控制,这是灰色/禁用了一定的时间间隔

本文关键字:时间 灰色 创建 何创建 一个 按钮 控制 | 更新日期: 2023-09-27 18:15:07

我想创建一个画布,显示广告像10秒和一个按钮控件,它是灰色的10秒后,使用户能够关闭广告画布。

我不知道如何创建按钮控件。谁来帮帮我。

如何创建一个按钮控制,这是灰色/禁用了一定的时间间隔

如果你不想沿着视图模型路径走下去,你可以使用Storyboard。

我拼凑了这个MainPage:

<phone:PhoneApplicationPage
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:System="clr-namespace:System;assembly=mscorlib" 
    x:Class="PhoneApp1.MainPage" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" d:DesignHeight="800" d:DesignWidth="480">
    <phone:PhoneApplicationPage.Resources>
        <Storyboard x:Name="DelayEnableButton">
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Control.IsEnabled)" Storyboard.TargetName="button">
                <DiscreteObjectKeyFrame KeyTime="0:0:10">
                    <DiscreteObjectKeyFrame.Value>
                        <System:Boolean>True</System:Boolean>
                    </DiscreteObjectKeyFrame.Value>
                </DiscreteObjectKeyFrame>
            </ObjectAnimationUsingKeyFrames>
        </Storyboard>
    </phone:PhoneApplicationPage.Resources>
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Button x:Name="button" Content="Button" Height="100" Margin="50" VerticalAlignment="Top" IsEnabled="False"/>
    </Grid>
</phone:PhoneApplicationPage>

重要的部分是按钮有一个名称,这个名称在故事板定义中指定。故事板设置为在10秒后将IsEnabled更改为True。然后在mainpage . example .cs中,我在Loaded事件中触发Storyboard:

public partial class MainPage : PhoneApplicationPage
{
    public MainPage()
    {
        InitializeComponent();
        this.Loaded += new RoutedEventHandler(MainPage_Loaded);
    }
    void MainPage_Loaded(object sender, RoutedEventArgs e)
    {
        DelayEnableButton.Begin();
    }
}

将按钮的IsEnabled绑定到视图模型中的一个属性,比如isaddisplays。然后在应用程序启动时将此属性设置为true,并使用计时器,在10秒后将其设置为false。