2012-04-05 59 views
0

我是编程windows phone 7的新手,但真的很累因为我花了3天的时间解决了一个问题。我搜索所有的互联网,并得到一些很好的解释,但没有运气 - 它不适用于我的程序。使用WCF将SQL Azure连接到Windows Phone 7

我在SQL Azure中一个表中我把它叫做dbo.Messenger与结构创造:

  • ID(PK,不为空)
  • 类(为nvarchar(30),NULL)
  • 消息(为nvarchar(最大),NULL)
  • 描述(nvarchar的(200),NULL)

然后我使它WC ˚Fwchich应该给我带来的是一个清单:

 [OperationContract] 
     List<NoteDto> GetNotes(); 
public List<NoteDto> GetNotes() 
    { 
     using (var context = new WP7mgrEntities()) 
     { 
      var notes = (from eachNote in context.Messenger 
         orderby eachNote.id ascending 
         select new NoteDto 
      { 
       id = eachNote.id, 
       category= eachNote.category, 
       description= eachNote.description, 
       message= eachNote.message, 
      } 
       ).ToList(); 
      return notes; 
     } 
    } 
五言的

得到了每个数据成员像这样的额外的类NoteDto:

[DataMember] 
    public int id {get; set; } 

所以在此之后我让WP7应用程序获得列表框,这也应该填写afert我点击按钮2

 <ListBox Height="431" HorizontalAlignment="Left" Margin="12,199,0,0" Name="listBox1" VerticalAlignment="Top" Width="438" 
       ItemsSource="{Binding Notes}"> 
     <ListBox.ItemTemplate> 
       <DataTemplate> 
        <StackPanel> 
         <TextBlock Text="{Binding category}"/> 
        </StackPanel> 
       </DataTemplate> 
      </ListBox.ItemTemplate>   
     </ListBox> 

并在此背后代码:

private void button2_Click(object sender, RoutedEventArgs e) 
    { 
     Service1Client client = new Service1Client(); 
     client.GetNotesCompleted += new EventHandler<GetNotesCompletedEventArgs>(client_GetNotesCompleted); 
     this.Notes = new ObservableCollection<NoteDto>(); 

    } 
    private ObservableCollection<NoteDto> _notes; 
    public ObservableCollection<NoteDto> Notes 
    { 
     get { return _notes; } 
     set { _notes = value; 
     this.RaisePropertyChanged("Notes"); 
     } 
    } 

公共事件PropertyChangedEventHandler的PropertyChanged; private void RaisePropertyChanged(string propertyName) { PropertyChangedEventHandler propertyChanged = this.PropertyChanged;如果((propertyChanged!= null)) propertyChanged(this,new PropertyChangedEventArgs(propertyName)); }}

void client_GetNotesCompleted(object sender, GetNotesCompletedEventArgs e) 
    {this.Notes = e.Result; } 

当我点击按钮2我的列表框没有被从数据库记录填写。
任何想法? Plz的帮助?

+0

你真的不知道你的代码是未能获得票据或屏幕是否未能对变化做出反应。我会改变button2_Click来设置这个。注意到一个静态值。 – Rich 2012-04-05 20:00:23

+0

如何设置? – HelpMyProgram 2012-04-06 15:22:56

+0

我只是说,在button2_Click内:this.Notes = new List {new NoteDto {id = 1,category =“Foo”}};调试101.确定哪个部分失败。然后找出原因。 – Rich 2012-04-06 16:07:46

回答

0

你使用什么样的绑定?回想一下,只有wshttpbinding不适用于WP7。另一方面,你为什么不把这样的数据库与WCF数据服务公开为OData

Check it out.

希望它有用,

+0

我使用默认绑定,自从创建项目(使用asp.net 4.0)以来没有在web.config中进行任何更改。WCF工作propety cuz我尝试使用它来获取一个记录来填充文本框,它是好的,只有列表只有这个问题。我可以尝试oData,但因为我看到我需要visual studio 2010,而不是在表达式上工作,我preffer :) – HelpMyProgram 2012-04-06 15:21:42

+0

我认为默认绑定是不支持的MEX。尽量只通过添加来添加使用WCF WCF编辑器或basicHttpBinding的: <服务behaviorConfiguration = “ServiceBehavior” 名称= “服务”> <端点地址= “” 结合= “basicHttpBinding的” 名称= “BasicHttpEndpoint” bindingConfiguration = “” contract =“IService”> 2012-04-10 07:46:26