用于列表框加载数据的 Windows Phone 7 进度条
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6346203/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
windows phone 7 progress bar for a listbox loading data
提问by Jarrette
Is there an event I can listen for when a listbox has completed loading it's data? I have a textbox and a listbox, when the user hits enter, the listbox is populated with results from a web service. I'd like to run the progress bar while the listbox is loading and collapse it when it's finished....
当列表框完成加载其数据时,是否有我可以侦听的事件?我有一个文本框和一个列表框,当用户按 Enter 键时,列表框会填充来自 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>
Here is my helper converter class....
这是我的助手转换器类....
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> { }
In my App.xaml....
在我的 App.xaml....
xmlns:HelperClasses="clr-namespace:MyVirtualHealthCheck.HelperClasses"
...
<HelperClasses:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter" TrueValue="Visible" FalseValue="Collapsed" />
The 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;
}
采纳答案by Matt Lacey
There's no built in functionality for this. You'll have to update the progressbar when you've finished loading the data.
Alternatively update a boolean dependency property in your view model and bind the progress bar to that.
没有内置的功能。完成数据加载后,您必须更新进度条。
或者更新视图模型中的布尔依赖属性并将进度条绑定到该属性。
Update
Some rough, example code, based on comments. This is written here and not checked but you should get the idea:
更新
一些粗略的示例代码,基于注释。这是写在这里并没有检查,但你应该明白:
The 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:
XAML:
<toolkit:PerformanceProgressBar IsEnabled="{Binding IsLoading}"
IsIndeterminate="{Binding IsLoading}"/>
Set the DataContext of the page to an instance of the view model and then call SimulateLoading()
on the view model instance.
将页面的 DataContext 设置为SimulateLoading()
视图模型的实例,然后调用视图模型实例。
Update yet again:
My mistake IsIndeterminate
is a bool so a converter isn't required.
再次更新:
我的错误IsIndeterminate
是一个布尔值,因此不需要转换器。
回答by Wicked Coder
You can create a new form which will have a progress bar.
您可以创建一个带有进度条的新表单。
The Progress form will have a timer and progress bar.
进度表将有一个计时器和进度条。
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
To Call a progress form from the Main form use the following code
要从主窗体调用进度窗体,请使用以下代码
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
To close the progress form use this code
要关闭进度表,请使用此代码
Public Sub CloseProgress()
If Progress IsNot Nothing Then
Progress.Invoke(New EventHandler(AddressOf Progress.KillMe))
Progress.Dispose()
Progress = Nothing
End If
End Sub
Because Progress form runs on a different thread it won't freeze the UI.
因为 Progress 表单在不同的线程上运行,所以它不会冻结 UI。
Sorry the code is in VB.NET
抱歉,代码在 VB.NET 中