Windows Phone 7中针对ListBox加载数据的进度条

3

当一个列表框完成数据加载时,有没有我可以监听的事件?我有一个文本框和一个列表框,当用户按下回车键时,列表框会从 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, 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" />

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;
    }
2个回答

3

这方面没有内置功能。当数据加载完成后,您需要更新进度条。
或者,在您的视图模型中更新一个布尔依赖属性,并将进度条绑定到该属性。

更新
以下是一些基于评论的示例代码,这里编写并未经过检查,但您应该可以理解:

视图模型:

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是一个布尔值,所以不需要转换器。


我已经更新了我的答案,并附上了一些示例代码。希望这能指引你朝着正确的方向前进。 - Matt Lacey
干得好,马特。喜欢这个解决方案。我从没想到你可以像那样绑定进度指示器,太牛逼了。 - Jarrette
Matt,你能解释一下以下这行代码吗?Converter={StaticResource BoolToVisibilityConverter} - Jarrette
@Matt 由于某些未知原因,我无法让进度条显示出来。我通过在控件上将值硬编码为 true 进行了测试,它可以显示出来。但是绑定不起作用...我已经使用代码更新了我的初始帖子。 - Jarrette
@Jarrette 再次更新。我的错误,这不需要转换器。或者在 http://cid-cc22250598bf7f04.office.live.com/self.aspx/Public/so6346203.zip 上有一个可用的示例。 - Matt Lacey
显示剩余5条评论

2
你可以创建一个新表单,其中将包含进度条。
进度表单将具有计时器和进度条。
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

由于进度表单在不同的线程上运行,它不会冻结用户界面。

抱歉,代码是VB.NET。


谢谢,VB很好!但是我不知道的是,如何知道何时关闭进度条。似乎没有事件告诉我列表框何时完成数据绑定... - Jarrette
在将数据绑定到列表框之后,您可以调用CloseProgress()函数。因此,您的顺序应该是:1. 创建一个进度线程并启动它;2. 绑定列表框(BindListBox);3. 关闭进度条(CloseProgress)。这样,在一个新的线程上,它将显示带有进度条的表单,并且在主线程上仍然将数据绑定到列表框上。当主线程完成绑定后,通过在新线程上调用KillMe函数来关闭进度表单。 - Wicked Coder
但是,listbox绑定是在viewmodel内部使用异步调用Web服务完成的...因此,当我绑定listbox时,无法知道数据何时完全下载并绑定到listbox中。 listbox基本上是使用foursquare API列出本地场馆的列表。根据用户的互联网连接和GPS坐标以及API服务器的速度,完成操作所需的时间可能会大大变化。 - Jarrette
你最好将其设置为同步调用,因为这样可以显示进度条,直到完成。或者在主线程上使用thread.sleep()来处理最坏的情况。 - Wicked Coder

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接