【问题标题】:Retrieving data from database in UWP在 UWP 中从数据库中检索数据
【发布时间】:2016-05-18 14:10:55
【问题描述】:

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

我当前的代码中有错误。 这是我的 IService1.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; }
    }

}

Service1.svc.cs:

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;
        }
    }
}

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

我在 x:DataType="data:NYPUnlocking" 处出错,错误提示“名称 "NYPUnlocking" 在命名空间 "using:NYPUnlock.ServiceReference1" 中不存在。

<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>

我的 cs 文件在 await task1 出现错误,提示“无法将类型 'System.Collections.ObjectModel.ObservableCollection' 隐式转换为 'NYPUnlock.ServiceReference1.NYPUnlocking[]'

namespace NYPUnlock
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
            getStatus();
        }
        public async void getStatus()
        {
            try
            {
                ServiceReference1.Service1Client client = new ServiceReference1.Service1Client(ServiceReference1.Service1Client.EndpointConfiguration.BasicHttpBinding_IService1);
                var task1 = client.NYP_GetLockerStatusAsync();
                ServiceReference1.NYPUnlocking[] returnResult = await task1;
                lvLocker.ItemsSource = returnResult;
            }
            catch(Exception ex)
            {
                tbError.Text = ex.Message;
            }
        }
    }
}

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

【问题讨论】:

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


    【解决方案1】:

    试试看:

    • 在 XAML 文件中向命名空间 WcfService2 添加一个名为“whatevername”的引用,并在 x:DataType="data:NYPUnlocking" 中使用此前缀代替“data:”

    • 将returnResult的声明替换为var类型而不是ServiceReference1.NYPUnlocking[]类型

    【讨论】:

    • 感谢您的快速回复,我不太明白您第一点的意思。将类型更改为 var 确实可以解决问题,谢谢!
    猜你喜欢
    • 2019-11-19
    • 2021-12-14
    • 1970-01-01
    • 2013-01-01
    • 2017-05-21
    • 1970-01-01
    相关资源
    最近更新 更多