哪位大侠能提供一个完整的ProgressBar例子,页面加载时显示加载了百分之几,加载成功多少条,失败多少条?
按如下步骤即可实现:
一、在承载SilverlightWeb网站目录下新建一个xaml页。命名为:SplashScreen.xaml。右击此属性,确保“生成操作”为“内容”。此页代码如下:
<Grid
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Name="parentCanvas"
>
<Grid x:Name="LayoutRoot">
<StackPanel Width="360" Grid.Row="1" Grid.Column="1" Height="236" VerticalAlignment="Top" Margin="0,150,0,0">
<Border BorderBrush="#FF5E849A" BorderThickness="1" Height="12" HorizontalAlignment="Center" VerticalAlignment="Center" Width="360" CornerRadius="5" Background="#FF0D283F" Grid.Row="3">
<!--显示进度条 SplanshScreen.js用到-->
<Grid x:Name="animaGrid" Margin="2" HorizontalAlignment="Left">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="5"/>
<ColumnDefinition/>
<ColumnDefinition Width="5"/>
</Grid.ColumnDefinitions>
<Border BorderThickness="0" CornerRadius="3,0,0,3">
<Border.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="White" Offset="0"/>
<GradientStop Color="#FF70C7E8" Offset="1"/>
</LinearGradientBrush>
</Border.Background>
</Border>
<Border BorderThickness="0" Grid.Column="1">
<Border.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="White" Offset="0"/>
<GradientStop Color="#FF70C7E8" Offset="1"/>
</LinearGradientBrush>
</Border.Background>
</Border>
<Border BorderThickness="0" CornerRadius="0,3,3,0" Grid.Column="2">
<Border.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="White" Offset="0"/>
<GradientStop Color="#FF70C7E8" Offset="1"/>
</LinearGradientBrush>
</Border.Background>
</Border>
</Grid>
</Border>
<!--显示百分比进度 SplanshScreen.js用到-->
<TextBlock x:Name="uxStatus"></TextBlock>
</StackPanel>
</Grid>
</Grid>
二、同样在此Web网站目录新建一个js文件,命名:SplanshScreen.js。js内容如下:
function onSourceDownloadProgressChanged(sender, eventArgs)
{
sender.findName("uxStatus").Text = "Loading: " + Math.round((eventArgs.progress * 1000)) / 10 + "%";
sender.findName("animaGrid").Width = eventArgs.progress * 354;
}
三、经过以上两个步骤后,最后只需在起始页中添加部分设置。如Login.aspx中。
1.在<head>...</head>中添加js文件引用:<script type="text/javascript" src="SplashScreen.js"></script>
2.在<object>....</object>中添加:
<param name="SplashScreenSource" value="SplashScreen.xaml"/>
<param name="OnSourceDownloadProgressChanged" value="onSourceDownloadProgressChanged" />
伪代码:
var i = 0; foreach(var file in files) { readfile(file); i++; progressBar1.value = progressBar1.max * (i / files.count); }
代码的意思也就是你在适当的地方设置一下progressbar的进度值就可以了。
能否给个实例看下额?
同用沧海一杰的例子,readfile应该是一个长时间操作,请将循环放到另外一个线程异步执行,然后
用Dispatcher.BeginInvoke设置value,否则界面会卡住,然后看到progress直接跳到100