Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows phone 7 WP7.1-如何使用oData刷新列表框_Windows Phone 7_Listbox_Wcf Data Services_Odata - Fatal编程技术网

Windows phone 7 WP7.1-如何使用oData刷新列表框

Windows phone 7 WP7.1-如何使用oData刷新列表框,windows-phone-7,listbox,wcf-data-services,odata,Windows Phone 7,Listbox,Wcf Data Services,Odata,在我的Windows Phone 7.1应用程序中。我在pivot控件中有一个列表框。我的列表框使用来自web服务的OData填充数据。我正在使用在上找到的服务进行测试。刷新列表框中的数据时遇到问题。例如,当应用程序加载时,应用程序将获取医嘱ID 10248和10249的数据。现在,如果用户按下应用程序栏中的按钮,我想获取OrderID10250和10251的记录。当我调用获取数据时,我不会从应用程序中得到任何错误,UI中的数据也不会刷新。我从阅读中了解到,DataServiceCollecti

在我的Windows Phone 7.1应用程序中。我在pivot控件中有一个列表框。我的列表框使用来自web服务的OData填充数据。我正在使用在上找到的服务进行测试。刷新列表框中的数据时遇到问题。例如,当应用程序加载时,应用程序将获取医嘱ID 10248和10249的数据。现在,如果用户按下应用程序栏中的按钮,我想获取OrderID10250和10251的记录。当我调用获取数据时,我不会从应用程序中得到任何错误,UI中的数据也不会刷新。我从阅读中了解到,DataServiceCollection实现了ObservableCollection,而ObservableCollection本身实现了INotifyPropertChanged,因此我的UI应该在集合更改时刷新。但事实并非如此

我已经在WPF应用程序中使用GridView对此进行了测试,UI使用新数据进行刷新。不过我知道WPF中的调用不是异步的。感谢您的帮助

下面是我在ViewModel中用来获取数据的代码

    private NorthwindEntities context;
    private const string svcUri = "http://services.odata.org/Northwind/Northwind.svc/";

    public MainViewModel()
    {
        List<string> nums = new List<string>() { "10248", "10249" };
        GetDataFromService(nums);
    }

    public void GetDataFromService(List<string> zNumbers)
    {
        try
        {
            string partQuery = "Orders()?$filter =";

            if (zNumbers.Count > 0)
            {
                foreach (var item in zNumbers)
                {
                    partQuery += "(OrderID eq " + item + ") or ";
                }

                partQuery = partQuery.Substring(0, partQuery.Length - 3).Trim();
            }

            // Initialize the context for the data service.
            context = new NorthwindEntities(new Uri(svcUri));

            Uri queryUri = new Uri(partQuery, UriKind.Relative);
            trackedCustomers = new DataServiceCollection<Order>(context);
            trackedCustomers.LoadAsync(queryUri);
        }
        catch (DataServiceQueryException ex)
        {
            MessageBox.Show("The query could not be completed:\n" + ex.ToString());
        }
        catch (InvalidOperationException ex)
        {
            MessageBox.Show("The following error occurred:\n" + ex.ToString());
        }
    }

    private DataServiceCollection<Order> trackedCustomers;

    public DataServiceCollection<Order> TrackedCustomers
    {
        get { return trackedCustomers; }
        set
        {
            if (value != trackedCustomers)
            {
                trackedCustomers = value;
                NotifyPropertyChanged("TrackedCustomers");
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(String propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (null != handler)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
私有Northwind实体上下文;
私有常量字符串svcUri=”http://services.odata.org/Northwind/Northwind.svc/";
公共主视图模型()
{
List nums=new List(){“10248”、“10249”};
GetDataFromService(nums);
}
public void GetDataFromService(列表编号)
{
尝试
{
string partQuery=“Orders()?$filter=”;
如果(zNumbers.Count>0)
{
foreach(变量项,以zNumbers为单位)
{
partQuery+=“(OrderID eq”+项+”)或“;
}
partQuery=partQuery.Substring(0,partQuery.Length-3.Trim();
}
//初始化数据服务的上下文。
上下文=新的NorthwindEntities(新Uri(svcUri));
Uri queryUri=新Uri(partQuery,UriKind.Relative);
trackedCustomers=新的DataServiceCollection(上下文);
LoadAsync(queryUri);
}
捕获(DataServiceQueryException ex)
{
Show(“查询无法完成:\n”+ex.ToString());
}
捕获(无效操作异常ex)
{
Show(“发生以下错误:\n”+ex.ToString());
}
}
私人数据服务收集跟踪客户;
公共数据服务收集跟踪的客户
{
获取{return trackedCustomers;}
设置
{
如果(值!=跟踪客户)
{
跟踪客户=价值;
NotifyPropertyChanged(“跟踪客户”);
}
}
}
公共事件属性更改事件处理程序属性更改;
私有void NotifyPropertyChanged(字符串propertyName)
{
PropertyChangedEventHandler处理程序=PropertyChanged;
if(null!=处理程序)
{
处理程序(这是新的PropertyChangedEventArgs(propertyName));
}
}
这是我主页上的XAML

    <Grid x:Name="LayoutRoot" Background="Transparent">
    <!--Pivot Control-->
    <controls:Pivot Title="MY APPLICATION">
        <!--Pivot item one-->
        <controls:PivotItem Header="first">
            <!--Double line list with text wrapping-->
            <ListBox x:Name="FirstListBox" Margin="0,0,-12,0" ItemsSource="{Binding             TrackedCustomers, Mode=OneWay}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                      <StackPanel Margin="0,0,0,17" Width="432" Height="78">
                            <TextBlock Text="{Binding OrderID}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
                            <TextBlock Text="{Binding Freight}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
                      </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </controls:PivotItem>          
    </controls:Pivot>
</Grid>

是否存在在LoadAsync完成后调用的处理程序?如果是这样,则此时您需要向公共财产TrackedCustomers分配任务

或者,可能更改这些行以使用公共属性:

        trackedCustomers = new DataServiceCollection<Order>(context);
        trackedCustomers.LoadAsync(queryUri);
trackedCustomers=newdataservicecollection(上下文);
LoadAsync(queryUri);

问题在于,您正在将后端集合
trackedCustomers
更改为一个新对象,而不告诉UI它正在更改。UI绑定到该对象的第一个实例,您将丢弃该对象。你需要做两件事中的一件

清除支持集合:

trackedCustomers.Clear();
trackedCustomers.LoadAsync(queryUri);
或者使用暴露的财产

TrackedCustomers= new DataServiceCollection<Order>(context);
TrackedCustomers.LoadAsync(queryUri);
TrackedCustomers=newdataservicecollection(上下文);
LoadAsync(queryUri);

非常感谢。使用公共财产解决了这个问题。我没想到解决办法会这么简单。再次感谢。