简体   繁体   English

如何在一个listview uwp中包含多个c#类

[英]how to include more than one c# class into the same listview uwp

The Structure 结构

I'm creating a UWP app, and ASP WebService to access a SQL Server dbase. 我正在创建一个UWP应用程序和ASP WebService来访问SQL Server dbase。 Both UWP and WebService have identical Models of 7 base classes that are correspond to the tables of the database. UWP和WebService都具有7个基类的相同模型,这些基类对应于数据库的表。

The Issue 问题

The biggest issue is I'm a newbie dev, so my lack of insight is the Real Issue, but you cant solve that monumental task just yet.. The solvable issue is, How do I get more than one set of values inside of my ListView from multiple base classes. 最大的问题是我是一个新手开发者,所以我缺乏洞察力是真实问题,但你还是无法解决这个巨大的任务......可解决的问题是,我如何在我的内部获得多个值集来自多个基类的ListView。 In my example, I'm referencing public List<Device> Devices { get; private set; } = new List<Device>(); 在我的例子中,我正在引用公共List<Device> Devices { get; private set; } = new List<Device>(); List<Device> Devices { get; private set; } = new List<Device>(); but I don't know how to reference more than one class in the list.. What I would like to do is List some values from Device and others from Location. 但是我不知道如何在列表中引用多个类。我想要做的是从Device中列出一些值,从Location中列出其他值。

The Code 编码

DisplayDevices.xaml 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 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 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 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;

You will have to create a new class and add those Members which you want to Bind in xaml. 您必须创建一个新类并添加要在xaml中绑定的那些成员。 That means your DB Query will return the objects joining from many tables in a custom class like below : 这意味着您的数据库查询将返回从自定义类中的许多表加入的对象,如下所示:

MyBindedModel.cs MyBindedModel.cs

public class MyBindedModel
{
    public string FacilityName { get; set; }
    public string Hardware{ get; set; }
    public string HostName{ get; set; }
    //Many more depending on what you want to show on UI
}

Now in ViewModel you can use 现在在ViewModel中你可以使用

public ObservableCollection<MyBindedModel> MyData

DisplayDevice.xaml DisplayDevice.xaml

     <ListView ItemsSource="{x:Bind ViewModel.MyData}"
                      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="MyData">
                        <StackPanel Orientation="Vertical" Padding="5">
                        <TextBlock Text="{x:Bind 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM