【问题标题】:Connecting SQL Azure to Windows Phone 7 using WCF使用 WCF 将 SQL Azure 连接到 Windows Phone 7
【发布时间】:2012-04-19 13:49:06
【问题描述】:

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

我在 SQL Azure 中创建了一个名为 dbo.Messenger 的表,其结构:

  • id(PK,非空)
  • 类别 (nvarchar(30), null)
  • 消息 (nvarchar(max), null)
  • 描述 (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 获得了这样的结果:

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

因此,在此之后,我制作了 wp7 应用程序,这些应用程序获得列表框,在我单击按钮 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; 私人无效 RaisePropertyChanged(字符串属性名称) { PropertyChangedEventHandler propertyChanged = this.PropertyChanged; 如果 ((propertyChanged != null)) { propertyChanged(this, new PropertyChangedEventArgs(propertyName)); } }

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

当我单击按钮 2 时,我的列表框没有被数据库中的记录填充。
任何想法 ?请帮忙?

【问题讨论】:

  • 您并不真正知道您的代码是否无法获取注释,或者屏幕是否无法响应更改。我会将 button2_Click 更改为仅将 this.Notes 设置为静态值。
  • 我说的是 button2_Click 中的类似内容:this.Notes = new List{ new NoteDto { id = 1, category = "Foo" }};调试 101 ..找出失败的部分。然后找出原因。

标签: c# wcf windows-phone-7 azure-sql-database


【解决方案1】:

您使用的是哪种绑定?回想一下,只有 wshttpbinding 不适用于 WP7。另一方面,为什么不使用 WCF 数据服务将此类数据库公开为 OData

Check it out.

希望对你有帮助,

【讨论】:

  • 我使用默认绑定,自创建项目(使用 asp.net 4.0)以来,web.config 中没有任何更改。 WCF 有效,因为我尝试使用它来获取一条记录来填充文本框,这没关系,仅使用 List 解决此问题。我可以尝试 oData,但正如我所见,我需要 Visual Studio 2010,而不是使用我喜欢的 Express :)
  • 我认为默认绑定是 MEX,它既不受支持。尝试使用 WCF WCF 编辑器添加 basichttpbinding 或仅添加:
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-13
  • 1970-01-01
  • 2013-01-15
  • 2011-06-20
相关资源
最近更新 更多