对于一个列表框数据加载Windows Phone 7的进度条 [英] windows phone 7 progress bar for a listbox loading data

查看:129
本文介绍了对于一个列表框数据加载Windows Phone 7的进度条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有我可以侦听当一个列表框加载完成它的数据的事件?我有一个文本框和一个列表框,当用户点击进入,列表框会填充从Web服务结果。我想运行,而列表框加载进度条和折叠它时,它的完成......



更新

 <控制:PivotItem标题=食物填充=0 110 0 0> 

<电网X:NAME =ContentFoodGrid.Row =2>

<&StackPanel的GT;
...


<工具箱:PerformanceProgressBar NAME =ppbFoods的Horizo​​ntalAlignment =左
VerticalAlignment =中心
WIDTH =466IsIndeterminate ={结合IsDataLoading}
能见度={结合IsDataLoading,转换器= {StaticResource的BoolToVisibilityConverter}}
/>


<! - 食品结果 - >
< ListBox的X:名称=lbFoods的ItemsSource ={结合食品}填充=5
的SelectionChanged =lbFoods_SelectionChangedHEIGHT =480>
....
< /列表框>

< / StackPanel的>
< /网格和GT;


< /控制:PivotItem>

下面是帮助我的转换器类....

 公共类BoolToValueConverter< T> :的IValueConverter 
{
公共牛逼FalseValue {搞定;组; }
公共牛逼TrueValue {搞定;组; }

公共对象转换(对象的值,类型TARGETTYPE,对象参数,System.Globalization.CultureInfo文化)
{
如果(价值== NULL)
返回FalseValue;
,否则
返回(布尔)值? TrueValue:FalseValue;
}

公共对象ConvertBack(对象的值,类型TARGETTYPE,对象参数,System.Globalization.CultureInfo文化)
{
返回值!= NULL? value.Equals(TrueValue):假的;
}
}

公共类BoolToStringConverter:BoolToValueConverter<串GT; {}
公共类BoolToBrushConverter:BoolToValueConverter<刷及GT; {}
公共类BoolToVisibilityConverter:BoolToValueConverter<能见度和GT; {}
公共类BoolToObjectConverter:BoolToValueConverter<对象> {}

在我的App.xaml ....

 的xmlns:HelperClasses =CLR的命名空间:MyVirtualHealthCheck.HelperClasses

< HelperClasses:BoolToVisibilityConverter X:键=BoolToVisibilityConverter TrueValue =可见FalseValue =折叠/>



视图模型......

  ... 
公共BOOL IsDataLoading
{
搞定;
组;
}
...
公共无效GetFoods(字符串strSearch)
{
IsDataLoading = TRUE;
WCFService.dcFoodInfoCollection localFoods = IsolatedStorageCacheManager< WCFService.dcFoodInfoCollection> .Retrieve(CurrentFoods);

如果(localFoods!= NULL)
{
=食品localFoods;
}
,否则
{
GetFoodsFromWCF(strSearch);
}
}


公共无效GetFoodsFromWCF(字符串strSearch)
{
IsDataLoading = TRUE;
wcfProxy.GetFoodInfosAsync(strSearch);
wcfProxy.GetFoodInfosCompleted + =新的EventHandler< WCFService.GetFoodInfosCompletedEventArgs>(wcfProxy_GetFoodInfosCompleted);
}

无效wcfProxy_GetFoodInfosCompleted(对象发件人,WCFService.GetFoodInfosCompletedEventArgs E)
{
WCFService.dcFoodInfoCollection食物= e.Result;
如果(食品!= NULL)
{
//设定电流食物从Web服务
this.Foods =食物的结果;
this.IsDataLoaded = TRUE;

//保存食物,以电话,以便我们可以使用缓存的结果,而不是轮跳闸到Web服务再次
SaveFoods(食品);
}
,否则
{
的Debug.WriteLine(Web服务云:+ e.Result);
}
IsDataLoading = FALSE;
}


解决方案

有在功能上没有专为这个。你必须在你完成加载数据来更新进度。结果
在你的视图模型或者更新的布尔依赖项属性并绑定进度条来表示。



更新结果
有些粗糙,示例代码的基础上,征求意见。这是在这里写的,而不是检查,但你应该得到的想法:



虚拟机:

 <为code>公共类MyViewModel:INotifyPropertyChanged的
{
私人布尔isLoading;
公共BOOL IsLoading
{
{返回isLoading; }


{
isLoading =价值;
NotifyPropertyChanged(IsLoading);
}
}

公共无效SimulateLoading()
{
变种体重=新的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();
}

公共事件PropertyChangedEventHandler的PropertyChanged;

私人无效NotifyPropertyChanged(字符串propertyName的)
{
PropertyChangedEventHandler处理器=的PropertyChanged;
如果(空=处理!)
{
处理器(这一点,新PropertyChangedEventArgs(propertyName的));
}
}
}



XAML:



 <工具箱:PerformanceProgressBar IsEnabled ={结合IsLoading}
IsIndeterminate ={结合IsLoading}/>



设置页面的DataContext到视图模型的实例,然后调用 SimulateLoading()的视图模型实例



更新再次:结果
我的错误 IsIndeterminate 是一个布尔所以不需要转换器。


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....

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....

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

解决方案

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:

<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.

Update yet again:
My mistake IsIndeterminate is a bool so a converter isn't required.

这篇关于对于一个列表框数据加载Windows Phone 7的进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆