有一个事件,我可以听一个列表框已经完成加载它的数据? 我有一个文本框和一个列表框,当用户点击input时,列表框会填充来自Web服务的结果。 我想在列表框加载时运行进度条,并在完成时将其折叠。
UPDATE
<controls:PivotItem Header="food" Padding="0 110 0 0"> <Grid x:Name="ContentFood" Grid.Row="2" > <StackPanel> ... ... <toolkit:PerformanceProgressBar Name="ppbFoods" HorizontalAlignment="Left" VerticalAlignment="Center" Width="466" IsIndeterminate="{Binding IsDataLoading}" Visibility="{Binding IsDataLoading, Converter={StaticResource BoolToVisibilityConverter}}" /> <!--Food Results--> <ListBox x:Name="lbFoods" ItemsSource="{Binding Foods}" Padding="5" SelectionChanged="lbFoods_SelectionChanged" Height="480" > .... </ListBox> </StackPanel> </Grid> </controls:PivotItem>
这是我的帮手转换器类….
public class BoolToValueConverter<T> : IValueConverter { public T FalseValue { get; set; } public T TrueValue { get; set; } public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (value == null) return FalseValue; else return (bool)value ? TrueValue : FalseValue; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { return value != null ? value.Equals(TrueValue) : false; } } public class BoolToStringConverter : BoolToValueConverter<String> { } public class BoolToBrushConverter : BoolToValueConverter<Brush> { } public class BoolToVisibilityConverter : BoolToValueConverter<Visibility> { } public class BoolToObjectConverter : BoolToValueConverter<Object> { }
在我的App.xaml ….
xmlns:HelperClasses="clr-namespace:MyVirtualHealthCheck.HelperClasses" ... <HelperClasses:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter" TrueValue="Visible" FalseValue="Collapsed" />
viewModel ….
... public bool IsDataLoading { get; set; } ... public void GetFoods(string strSearch) { IsDataLoading = true; WCFService.dcFoodInfoCollection localFoods = IsolatedStorageCacheManager<WCFService.dcFoodInfoCollection>.Retrieve("CurrentFoods"); if (localFoods != null) { Foods = localFoods; } else { GetFoodsFromWCF(strSearch); } } public void GetFoodsFromWCF(string strSearch) { IsDataLoading = true; wcfProxy.GetFoodInfosAsync(strSearch); wcfProxy.GetFoodInfosCompleted += new EventHandler<WCFService.GetFoodInfosCompletedEventArgs>(wcfProxy_GetFoodInfosCompleted); } void wcfProxy_GetFoodInfosCompleted(object sender, WCFService.GetFoodInfosCompletedEventArgs e) { WCFService.dcFoodInfoCollection foods = e.Result; if (foods != null) { //set current foods to the results from the web service this.Foods = foods; this.IsDataLoaded = true; //save foods to phone so we can use cached results instead of round tripping to the web service again SaveFoods(foods); } else { Debug.WriteLine("Web service says: " + e.Result); } IsDataLoading = false; }
这没有内置的功能。 加载完数据后,您必须更新进度条。
或者更新视图模型中的布尔依赖项属性,并将进度条绑定到该属性。
更新
一些粗糙的示例代码,基于评论。 这是写在这里,而不是检查,但你应该得到的想法:
VM:
public class MyViewModel : INotifyPropertyChanged { private bool isLoading; public bool IsLoading { get { return isLoading; } set { isLoading = value; NotifyPropertyChanged("IsLoading"); } } public void SimulateLoading() { var bw = new BackgroundWorker(); bw.RunWorkerCompleted += (s, e) => Deployment.Current.Dispatcher.BeginInvoke( () => { IsLoading = false; }); bw.DoWork += (s, e) => { Deployment.Current.Dispatcher.BeginInvoke(() => { IsLoading = true; }); Thread.Sleep(5000); }; bw.RunWorkerAsync(); } public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged(String propertyName) { PropertyChangedEventHandler handler = PropertyChanged; if (null != handler) { handler(this, new PropertyChangedEventArgs(propertyName)); } } }
XAML:
<toolkit:PerformanceProgressBar IsEnabled="{Binding IsLoading}" IsIndeterminate="{Binding IsLoading}"/>
将页面的DataContext设置为视图模型的实例,然后在视图模型实例上调用SimulateLoading()
。
再次更新 :
我的错误IsIndeterminate
是一个布尔,所以不需要转换器。
你可以创建一个新的表单,它将有一个进度条。
进度表格将有一个计时器和进度条。
Private Sub tProgress_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tProgress.Tick Count = (Count + 1) Mod ProgressBar1.Maximum ProgressBar1.Value = Count End Sub Public Sub KillMe(ByVal o As Object, ByVal e As EventArgs) Me.Close() End Sub
从主窗体调用进度窗体使用下面的代码
Dim ProgressThread As New Threading.Thread(New Threading.ThreadStart(AddressOf StartProgress)) ProgressThread.Start() Public Sub ProgressSplash() 'Show please wait splash Progress = New frmProgress Application.Run(Progress) End Sub
要关闭进度表,请使用此代码
Public Sub CloseProgress() If Progress IsNot Nothing Then Progress.Invoke(New EventHandler(AddressOf Progress.KillMe)) Progress.Dispose() Progress = Nothing End If End Sub
由于Progress表单在不同的线程上运行,因此不会冻结UI。
对不起,代码是在VB.NET中
你只要按照下面的链接我已经发布了一篇文章,以使用Windows Phone 7应用程序中的自定义进度栏如何在Windows Phone 7应用程序中创建自定义进度栏
在上面的例子中,我只是在画布内部使用画布控制和图像,并以360度角旋转画布。
当你想显示你的进度条并将canvas的可见性设置为可见时,以及当你的listBox
数据完全加载的时候,你listBox
调用storyboard.Stop();
将canvas的可见性设置为Collapsed并且调用storyboard.Stop();