在Windows Phone 7開發人員我在不使用Progressbar的前提下又想告訴使用者程式正在運行,顯示一個進度對話方塊,這時我們就可以用Popup。
前台
<Popup x:Name="ProgressPopup" Width="300" IsOpen="False" HorizontalAlignment="Center"
VerticalAlignment="Top" d:LayoutOverrides="Width, HorizontalMargin" Margin="89,203,91,0">
<Border BorderThickness="10" BorderBrush="Black" Background="DarkGray" Padding="30,30">
<StackPanel>
<TextBlock MaxHeight="100" Foreground="White" FontWeight="Bold" FontSize="36" x:Name="txt" Text="1">
<TextBlock.Triggers>
<EventTrigger RoutedEvent="TextBlock.Loaded">
<BeginStoryboard> <Storyboard>
<DoubleAnimation AutoReverse="True" Duration="0:0:1"
From="1.0" RepeatBehavior="Forever" Storyboard.TargetName="txt" Storyboard.TargetProperty="Opacity" To="0.0"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</TextBlock.Triggers>
</TextBlock>
</StackPanel>
</Border>
</Popup>
後台
public partial class MainPage : PhoneApplicationPage
{
Storyboard _timer = new Storyboard();
int i=0;
public MainPage()
{
InitializeComponent();
_timer.Duration = TimeSpan.FromMilliseconds(10);
_timer.Completed += new EventHandler(_timer_Completed);
_timer.Begin();
i = Convert.ToInt32(txt.Text);
}
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
this.Dispatcher.BeginInvoke(new Action(() => ProgressPopup.IsOpen = true));
}
void _timer_Completed(object sender, EventArgs e)
{
if (i <= txt.MaxHeight)
{
i++;
this.txt.Text = i.ToString();
_timer.Begin();
return;
}
this.Dispatcher.BeginInvoke(new Action(() => { ProgressPopup.IsOpen = false; }));
}
}