2011-11-19 25 views

回答

3

如果你不想沿着viewmodel路線走,你可以使用一個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.xaml.cs中我踢了故事板的Loaded事件:

public partial class MainPage : PhoneApplicationPage 
{ 
    public MainPage() 
    { 
     InitializeComponent(); 
     this.Loaded += new RoutedEventHandler(MainPage_Loaded); 
    } 

    void MainPage_Loaded(object sender, RoutedEventArgs e) 
    { 
     DelayEnableButton.Begin(); 
    } 
} 
+0

我得到錯誤在 InitializeComponent(); and DelayEnableButton.Begin(); 這是說,這不存在於當前的情況下 我已經複製你的代碼在我的xaml和cs文件。 也許一些「使用」語句從後面的代碼丟失.. ?? –

+0

您需要修復x:Class的定義以匹配MainPage.xaml中的命名空間 –

3

將按鈕的IsEnabled綁定到ViewModel中的屬性,如IsAdDisplayed。然後在應用程序啓動時將此屬性設置爲true,並使用計時器在10秒後將其設置爲false。

+0

我真正的問題是讓這個計時器.. !!! 我不能讓那個計時器..! –

相關問題