在UWP中从数据库中检索数据

时间:2016-05-18 14:10:55

标签: c# wcf azure win-universal-app uwp

我正在尝试从Azure上的SQLDatabase检索数据列表,并将其绑定到我的Windows 10通用应用程序的列表视图中。我所做的是创建一个WCF服务来检索数据,然后在Azure上发布之后,在我的应用程序上调用此服务。

我当前的代码中存在错误。 这是我的IService1.cs代码

       for pic in car.mediaRelationship {

    }

Service1.svc.cs:

namespace WcfService2
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IService1
    {
        // TODO: Add your service operations here
        [OperationContract]
        List<NYPUnlocking> NYP_GetLockerStatus();
    }


    // Use a data contract as illustrated in the sample below to add composite types to service operations.
    [DataContract]
    public class NYPUnlocking
    {
        [DataMember]
        public int lockerID { get; set; }
        [DataMember]
        public string lockStatus { get; set; }
    }

}

然后我在Azure上发布它,并在我的UWP应用程序上添加对azure网站的服务引用。以下代码是我的XAML和cs代码。

我在x处遇到错误:DataType =“data:NYPUnlocking”,错误说“名称”NYPUnlocking“在命名空间中不存在”使用:NYPUnlock.ServiceReference1“。

namespace WcfService2
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
    public class Service1 : IService1
    {
       public List<NYPUnlocking> NYP_GetLockerStatus()
        {
            SqlConnection conn = new SqlConnection("Copy ADO.net connection string");
            string sqlStr = "SELECT * FROM dbo.lockerStatus";
            SqlCommand cmd = new SqlCommand(sqlStr, conn);
            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            List<NYPUnlocking> ret = new List<NYPUnlocking>();
            while (dr.Read())
            {
                NYPUnlocking unlock = new NYPUnlocking()
                {
                    lockerID = Int32.Parse(dr["lockerID"].ToString()),
                    lockStatus = dr["lockStatus"].ToString()
                };

                ret.Add(unlock);
            }
            conn.Close();

            return ret;
        }
    }
}

我的cs文件在等待task1时出错“无法将类型'System.Collections.ObjectModel.ObservableCollection'隐式转换为'NYPUnlock.ServiceReference1.NYPUnlocking []'

<Page.Resources>
    <DataTemplate x:DataType="data:NYPUnlocking" x:Key="UserTemplate">
        <StackPanel Orientation="Horizontal" Padding="0,0,0,0">
            <TextBlock FontSize="12" Text="{x:Bind lockerID}" HorizontalAlignment="Left" Width="200" />
            <TextBlock FontSize="12" Text="{x:Bind lockStatus}" HorizontalAlignment="Right" Width="200" />
        </StackPanel>
    </DataTemplate>
</Page.Resources>

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" Width="354">
        <ListView x:Name="lvLocker" ItemTemplate="{StaticResource UserTemplate}"/>
        <TextBox x:Name="tbError" TextWrapping="Wrap" Text="Error"/>
    </StackPanel>
</Grid>

我认为我正在做的一切正确,可能导致问题的原因是什么?非常感谢任何帮助,谢谢!

1 个答案:

答案 0 :(得分:0)

试试:

  • 将名为“whatevername”的引用添加到XAML文件中的命名空间WcfService2,并在x:DataType="data:NYPUnlocking"

  • 中使用此前缀而不是“data:”
  • 将returnResult的声明替换为var类型而不是ServiceReference1.NYPUnlocking[]类型