UWP - 需要示例如何使用OData服务

时间:2018-02-26 13:06:55

标签: uwp odata

有人可以解释如何在UWP应用程序中使用OData ConnectedService从表和相关表中获取数据吗? 我在ViewModel中有这段代码:

public async Task<bool> LoadData()
{
    bool returnValue = false;

    this.OrderTypeCollection = new ObservableCollection<OrderType>();
    this.OrderSelectedItem = new Order();

    try
    {
        // Use same data context
        OrderApplicationEntities context = new OrderApplicationEntities(AppStorage.Instance.OrderApplicationDataServiceUri);

        // Get Orders with expanded OrderTypes
        var queryOrder = from v in context.Orders.Expand("OrderType") select v;
        DataServiceQuery<Order> dataServiceQueryOrder = (DataServiceQuery<Order>)queryOrder;
        TaskFactory<IEnumerable<Order>> taskFactoryOrder = new TaskFactory<IEnumerable<Order>>();
        IEnumerable<Order> resultOrder = await taskFactoryOrder.FromAsync(dataServiceQueryOrder.BeginExecute(null, null), iar => dataServiceQueryOrder.EndExecute(iar));

        // Set selected Order
        this.OrderSelectedItem = resultOrder.ElementAt(0);

        // Get OrderTypes
        var queryOrderType = from v in context.OrderTypes select v;
        DataServiceQuery<OrderType> dataServiceQueryOrderType = (DataServiceQuery<OrderType>)queryOrderType;
        TaskFactory<IEnumerable<OrderType>> taskFactoryOrderType = new TaskFactory<IEnumerable<OrderType>>();
        IEnumerable<OrderType> resultOrderType = await taskFactoryOrderType.FromAsync(dataServiceQueryOrderType.BeginExecute(null, null), iar => dataServiceQueryOrderType.EndExecute(iar));
        ObservableCollection<OrderType> OrderTypeCollection = new ObservableCollection<OrderType>();

        // Set collection of available OrderTypes
        foreach (var v in resultOrderType)
        {
            OrderTypeCollection.Add(v);
        }

        this.OrderTypeCollection = OrderTypeCollection;

        returnValue = true;
    }
    catch(Exception)
    {
    }

    return returnValue;
}

它使用扩展的OrderTypes实体工作并加载订单,但首先 - 从我记得的Silverlight中 - 它看起来......超过了阻碍。 其次,当我在XAML组合框中使用它时:

<ComboBox Grid.Row="1" Grid.Column="1"
          HorizontalAlignment="Stretch"
          Margin="{StaticResource MediumLeftTopRightBottomMargin}"
          ItemsSource="{x:Bind ViewModel.OrderTypeCollection, Mode=TwoWay}"
          SelectedItem="{x:Bind ViewModel.OrderSelectedItem.OrderType, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
    <ComboBox.ItemTemplate>
        <DataTemplate x:DataType="ComboBoxItem">
            <TextBlock Text="{Binding Path=Name}"/>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

然后它不会显示当前的OrderType。下拉列表正确显示可用的OrderTypes,但是当我选择一些OrderType时 - 我得到了堆栈溢出&#39;例外。我该如何正确使用它?

0 个答案:

没有答案