0

Silverlight 新手在这里,尝试做一个简单的数据绑定:

我试图获取测试数据库中所有用户的列表,但我不能;我使用 Openaccess 并遵循所有关于如何创建域模型和域服务的教程;我在执行以下代码时遇到问题,即使我在 db 上有 200 行,它也总是返回 0:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ServiceModel.DomainServices.Client;
using SLTest.Web;

namespace SLTest {
    public partial class MainPage : UserControl  {

        private TestDomainContext link = new TestDomainContext();



        public MainPage() {
            InitializeComponent();
            Loaded += new RoutedEventHandler(MainPage_Loaded);
        }

        private void MainPage_Loaded(object sender, RoutedEventArgs e) {

            int count = 0;
            LoadOperation<DomainUser> loadOperation = link.Load(link.GetDomainUsersQuery());
            loadOperation.Completed += (s, a) =>{
                theList.ItemsSource = link.DomainUsers;
                count = link.DomainUsers.Count();
            };

        }
    }
}

另外,为什么那只是在做

int count = link.DomainUsers.Count();

不像在 ASP.NET 中那样工作?

这是 XAML:

<UserControl x:Class="SLTest.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
        mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">

    <Grid x:Name="theGrid">
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="200"></ColumnDefinition>
            <ColumnDefinition Width="*"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <ListBox Grid.Row="0" Grid.Column="0" x:Name="theList">
            <DataTemplate>
                <TextBlock Text="{Binding FullName}"></TextBlock>
            </DataTemplate>
        </ListBox>
    </Grid>
</UserControl>
4

1 回答 1

1

我猜链接对象是您的实际域服务,因此您不应该直接在回调方法中处理它,因为您的服务中的所有查询操作都是异步的,并且不会产生任何以这种方式处理的结果。相反,您可以使用异步回调方法参数来获取您请求的信息,如下所示:

  loadOperation.Completed += (s, a) =>
        {
            LoadOperation<DomainUser> loadedObjects = (LoadOperation<DomainUser>)s;
            theList = loadedObjects.Entities;
            count = loadedObjects.Entities.Count();
        };

那应该行得通。

于 2012-12-03T09:56:16.103 回答