簡體   English   中英

在UWP中從數據庫中檢索數據

[英]Retrieving data from database in UWP

我正在嘗試從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“在命名空間中不存在”使用: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文件在等待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;
            }
        }
    }
}

我想我正在做的一切正確,可能導致問題的原因是什么? 非常感謝任何幫助,謝謝!

試試看:

  • 將名為“whatevername”的引用添加到XAML文件中的命名空間WcfService2,並在x:DataType="data:NYPUnlocking"使用此前綴而不是“data:” x:DataType="data:NYPUnlocking"

  • 將returnResult的聲明替換為var類型而不是ServiceReference1.NYPUnlocking[]類型

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM