我正在尝试从 Azure 上的 SQLDatabase 检索数据列表,并将其绑定(bind)到 Windows 10 通用应用程序中的 ListView 上。我所做的是创建一个用于检索数据的 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"处遇到错误,错误提示“ namespace “using: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>
我的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;
}
}
}
}
我认为我做的一切都是正确的,是什么导致了这个问题?非常感谢任何帮助,谢谢!
最佳答案
尝试一下:
将名为“whatevername”的引用添加到 XAML 文件中的命名空间 WcfService2,并在
x:DataType="data:NYPUnlocking"
中使用此前缀而不是“data:”将 returnResult 的声明替换为 var 类型,而不是
ServiceReference1.NYPUnlocking[]
类型
关于c# - 从 UWP 中的数据库检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37302160/