windows phone 7进度条,用于列表框加载数据

时间:2011-06-14 15:38:38

标签: c# windows windows-phone-7

当列表框完成加载数据时,是否有可以侦听的事件?我有一个文本框和一个列表框,当用户点击进入时,列表框中填充了来自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" />

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;
    }

3 个答案:

答案 0 :(得分:3)

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

<强>更新
一些粗略的示例代码,基于注释。这是写在这里而不是检查,但你应该明白:

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是一个bool,因此不需要转换器。

答案 1 :(得分: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

因为Progress表单在不同的线程上运行,所以它不会冻结UI。

很抱歉代码在VB.NET中

答案 2 :(得分:1)

您只需按照以下链接发布一篇文章,在Windows Phone 7应用程序中使用自定义进度条How to create custom progress bar in windows phone 7 application

在上面的示例中,我只使用画布控件和画布内的图像,只需旋转360度角的画布。

如果要显示进度条并将画布可见性设置为可见,则只需调用storyboard.Begin()即可  当您的listBox数据完全加载时,将画布可见性设置为折叠并调用storyboard.Stop();