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>
答案 0 :(得分:1)
我猜测链接对象是您的实际域服务,因此您不应该直接在回调方法中解决它,因为您的服务中的所有查询操作都是异步的,并且不会产生任何以这种方式解决的结果。 相反,您可以使用异步回调方法参数来获取您要求的信息:
loadOperation.Completed += (s, a) =>
{
LoadOperation<DomainUser> loadedObjects = (LoadOperation<DomainUser>)s;
theList = loadedObjects.Entities;
count = loadedObjects.Entities.Count();
};
这应该有效。