如何解决Windows Phone 7进度条,用于列表框加载数据
| 列表框完成数据加载后,我可以监听事件吗?我有一个文本框和一个列表框,当用户单击Enter时,该列表框将填充Web服务的结果。我想在列表框加载时运行进度栏,并在完成时折叠它。 更新 <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,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;
}
解决方法
没有内置功能。完成加载数据后,您将必须更新进度条。
或者,在视图模型中更新布尔依赖项属性,然后将进度条绑定到该属性。
更新资料
一些基于注释的粗糙示例代码。这是在此处编写的,未经检查,但您应该了解一下:
虚拟机:
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设置为视图模型的实例,然后在视图模型实例上调用ѭ6。
再次更新:
我的错误“ 7”是个布尔值,因此不需要转换器。
, 您可以创建一个新的窗体,该窗体将带有进度条。
进度表单将具有计时器和进度栏。
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中
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。