silverlight ProgressBar使用问题!
以下是我使用ProgressBar的代码;
.xaml:
<Grid x:Name="myGrid" Background="White">
<StackPanel VerticalAlignment="Center">
<TextBlock x:Name="lblPercent" TextAlignment="Center" ></TextBlock>
<ProgressBar x:Name="progressBar" Width="200" Height="20" Minimum="0" Maximum="100">
</ProgressBar>
</StackPanel>
</Grid>
.cs:
public partial class Page : UserControl
{
Storyboard _loop = new Storyboard();
int _count = 0;
public Page()
{
InitializeComponent();
ProgressBarDemo();
}
void ProgressBarDemo()
{
_loop.Duration = TimeSpan.FromMilliseconds(50d);
_loop.Completed += new EventHandler(_loop_Completed);
_loop.Begin();
}
void _loop_Completed(object sender, EventArgs e)
{
progressBar.Value = _count;
if (_count <100)
{
_count++;
lblPercent.Text = _count.ToString() + "%";
}
else
{
lblPercent.Text = "登陆成功";
progressBar.Visibility = Visibility.Collapsed;
}
_loop.Begin();
}
}
我做了一个页面要异步加载数据,在数据还未出现的时候我想用进度条来告诉用户数据加载中,上面的代码是静态自己设置的,怎么样才能应用到实际开发中!然后进度条结束后数据加载成功?各位高手帮忙下,提供一些例子也可以!
[解决办法]
比如你用WebClient
WebClient client = new WebClient();
client.OpenReadCompleted += new OpenReadCompletedEventHandler(OnLoadSampleCompleted);
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(OnLoadSampleDownloadProgressChanged);
client.OpenReadAsync(uri);
你可以在OnLoadSampleDownloadProgressChanged里更新ProgressBar(事件参数里有BytesReceived和TotalBytesToReceive属性),在OnLoadSampleCompleted里显示数据