【发布时间】:2017-09-30 05:10:20
【问题描述】:
结构
我正在创建一个 UWP 应用程序和 ASP WebService 来访问 SQL Server 数据库。 UWP 和 WebService 都有相同的 7 个基类的模型,对应数据库的表。
问题
最大的问题是我是一个新手开发者,所以我缺乏洞察力是真正的问题,但你还不能解决这个艰巨的任务.. 可解决的问题是,我如何获得一组以上的值在我的 ListView 内部来自多个基类。在我的示例中,我引用了 public List<Device> Devices { get; private set; } = new List<Device>();,但我不知道如何在列表中引用多个类。我想做的是列出 Device 中的一些值和 Location 中的其他值。
守则
DisplayDevices.xaml
<ListView ItemsSource="{x:Bind ViewModel.Devices}"
SelectionChanged="{x:Bind ViewModel.deviceList_SelectionChanged}"
x:Name="deviceList" Margin="50,0,0,50"
Header="{x:Bind ViewModel.RouterName}"
RelativePanel.RightOf="EditViewSP">
<ListView.ItemTemplate>
<DataTemplate x:DataType="model:Device">
<StackPanel Orientation="Vertical" Padding="5">
<TextBlock Text="{x:Bind ViewModel.FacilityName }" />
<StackPanel Orientation="Horizontal">
<TextBlock Text="{x:Bind Hardware}" />
<TextBlock Text="{x:Bind HostName}" Margin="10,0,0,0"/>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Device.cs
namespace RapidDeploy.Models
{
public class Device
{
[Key]
public int HostName { get; set; }
public string DriveModel { get; set; }
public string DriveSN { get; set; }
public string OldDriveSN { get; set; }
public bool Server { get; set; }
public string RouterName { get; set; }
public string IP { get; set; }
Location.cs
namespace RapidDeploy.Models
{
public class Location
{
[Key]
public int Id { get; set; }
public string FacilityName { get; set; }
public string Country { get; set; }
public string Region { get; set; }
public string State { get; set; }
public string City { get; set; }
public int BuildingNum { get; set; }
public string Address { get; set; }
DisplayDeviceViewModel.cs
private Device _ActiveDevice;
private int _hostName;
public int HostName { get { return _hostName; } set { Set(ref _hostName, value); } }
var uriL = new Uri("http://localhost:2463/api/Locations/");
var uriD = new Uri("http://localhost:2463/api/Devices/");
var JsonResponseD = await client.GetStringAsync(uriD);
var devicesResult = JsonConvert.DeserializeObject<List<Device>>(JsonResponseD);
Devices = devicesResult;
var JsonResponseL = await client.GetStringAsync(uriL);
var locationsResult = JsonConvert.DeserializeObject<List<Location>>(JsonResponseL);
Locations = locationsResult;
【问题讨论】:
-
向类添加一个通用接口并创建一个列表
。也许 IListable 是一个说明目的的名称。 ToString 显然很有帮助.. -
或者你可以创建
DeviceOrLocationViewModel并使用它。恕我直言,直接绑定到视图内的模型不是一个好主意。 -
我愿意接受建议。从一开始,我就想学习最有效的应用 MVVM 方法的方法。因此,如果有办法将 DataType 重定向到 ViewModel,请分享。
-
TaW,你有解释你建议的代码示例或链接吗?
-
位置和设备之间的链接在哪里?
标签: c# xaml listview uwp template10