【问题标题】:how to include more than one c# class into the same listview uwp如何在同一个listview uwp中包含多个c#类
【发布时间】: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


【解决方案1】:

您必须创建一个新类并添加要在 xaml 中绑定的成员。这意味着您的数据库查询将返回从自定义类中的许多表加入的对象,如下所示:

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
}

现在您可以在 ViewModel 中使用

public ObservableCollection<MyBindedModel> MyData

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>

【讨论】:

  • 这将有助于在类中添加需要在我创建的每个列表中的所有内容。下一个要思考的问题是如何接受 WebService JSON 调用并将它们分开?我将代码添加到原始帖子以供参考。
  • 因为即使你我在应用程序中将它们混合在一起,它们仍然需要被拉取,并作为单独的表发布到 SQL。
  • @ekgcorp 您始终可以使用 Linq Projections 从更大的集合中获取所需的字段。
猜你喜欢
  • 1970-01-01
  • 2021-05-08
  • 2018-01-17
  • 1970-01-01
  • 2020-01-03
  • 2011-05-06
  • 1970-01-01
  • 2016-05-10
  • 1970-01-01
相关资源
最近更新 更多