我是编程Windows Phone 7的新手,但是真的很累,因为我在一个问题上花了3天时间 . 我搜索所有互联网并得到一些很好的解释,但没有运气 - 它不适用于我的程序 .
我在SQL Azure中创建了一个表,我用结构调用了 dbo.Messenger :
-
id(PK,非null)
-
category(nvarchar(30),null)
-
message(nvarchar(max),null)
-
description(nvarchar(200),null)
然后我为它做了WCF wchich应该给我一个列表:
[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获取cource:
[DataMember]
public int id {get; set; }
所以在此之后我制作wp7应用程序获得列表框,也应该填写afert我点击button2
<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; if((propertyChanged!= null)){propertyChanged(this,new PropertyChangedEventArgs(propertyName)); }}
void client_GetNotesCompleted(object sender, GetNotesCompletedEventArgs e)
{this.Notes = e.Result; }
当我单击按钮2时,我的列表框不会被数据库中的记录填充 .
任何的想法 ? Plz有帮助吗?
1 回答
你使用什么样的绑定?回想一下,只有wshttpbinding不适用于WP7 . 另一方面,为什么不将这样的数据库与WCF数据服务公开为OData .
Check it out.
希望它有用,