2015-11-18 111 views
1

我已经写了一个简单的Web服务,允许我插入到我的SQL DB(工作正常),并从我的SQL DB(目前不工作)检索信息。我试图将信息放入列表中,然后将该信息显示在通用Windows应用程序的列表框中。此登录我:从WebService LINQ查询填充列表框

WEB SERVICE

[OperationContract] 
    List<TBL_My_Info> FindInfo(string uid); 

    public List<TBL_My_Info> FindInfo(string uid) 
    { 
     DataClasses1DataContext context = new DataClasses1DataContext(); 
     var res = from r in context.TBL_My_Info where r.User_Name == uid select r; 
     return res.ToList(); 
    } 

通用web应用

 private void btnView_Click(object sender, RoutedEventArgs e) 
    { 
     string s = txtNameFind.Text; 
     this.Content = new Page1(s); 
    }  

    public Page1(string s) 
    { 
     this.InitializeComponent(); 
     LoadData(s);   
    } 

    private async void LoadData(string s) 
    { 
     var client = new ServiceReference1.Service1Client(); 
     var res = await client.FindMyInfoAsync(s); 
     articleMyInfoListBox.ItemsSource = res; 
    } 

XAML

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> 
    <ListBox x:Name="articleMyInfoListBox"></ListBox> 
</Grid> 

我知道我需要像myListBox.ItemSource = res;这样的东西,但res在ItemSource之后没有显示为选项。我使用using myapp.ServiceReference1;引用了我的Web服务。

因此,在短期,我所希望做的是填充列表框的信息返回的列表,了解这些信息是从Web服务来....

如果我这样做的错误的方式,并有更好的方式,请让我知道。我对LINQ没有任何经验,但对SQL有一定的经验。所以,当然,SQL将是可取的..

编辑

我目前得到这回在我的列表框

using InsertAppTest.ServiceReference1.TBL_My_Info; 

回答

0

为服务客户端生成的方法是异步的,并返回的情况下, Task,所以你需要await它们才能真正获得“res”返回值。尝试是这样的:

private void btnView_Click(object sender, RoutedEventArgs e) 
{ 
    string s = txtNameFind.Text; 
    this.Content = new Page1(s); 
} 

public Page1(string s) 
{ 
    this.InitializeComponent(); 
    LoadData(s); 
} 

private async void LoadData(string s) 
{ 
    var client = new ServiceReference1.Service1Client(); 
    var res = await client.FindInfoAsync(s); 
    listBox.ItemsSource = res; 
} 

这应该工作,但也有可能是一个更好的方式来组织你的异步调用 - 我只是不知道有足够的了解你要完成的任务。

编辑

你的XAML是要要有的DisplayMemberPath属性集,以便它知道要显示在TBL_My_Info什么财产。现在它可能只是在每个实例上做一个ToString()

如果TBL_My_Info看起来是这样的:

public class TBL_My_Info 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
} 

那么你的XAML应该是这样的:

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> 
    <ListBox x:Name="articleMyInfoListBox" DisplayMemberPath="Name"></ListBox> 
</Grid> 
+0

好了,我调试的代码将您的建议在后,以及在将数据通过“res”变量,但不显示它在列表框中。相反,在列表框中,它显示我在页面顶部使用的语句“using myapp.ServiceReference1”,并将该表添加到using语句的末尾......因此,在整个列表框中都返回这个“使用myapp.ServiceReference1.TBL_My_Info”...我将在上面发布更新后的代码,以及列表框的xaml。有什么建议? – Code

+0

我只是将编辑添加到我的问题 – Code

+0

当它呈现时,您的列表框的内容实际上是字符串“using myapp.ServiceReference1.TBL_My_Info”?这没有意义。您是否将列表框的DisplayMemberPath属性设置为TBL_My_Info类中属性的名称? – alexchardin