1

这是我第一次构建 UWP 应用程序,而且我通常是 c#/Windows 的新手。我正在尝试在 UI 中使用 Pivot。我希望枢轴标头来自ObservableCollection连接的 USB 设备,这些设备按类进行Device_Info。每个 USB 设备都有一个名为的属性HardwareRevNumMajor,我希望将其显示为每个数据透视表头。我的 xml 看起来像这样:

<Page
x:Class="usb_test.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:usb_test"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:code="using:usb_test"
mc:Ignorable="d">
<Page.DataContext>
    <local:View_Model/>
</Page.DataContext>

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Pivot x:Name="pivot1" Title="Pivot" Opacity="0.99" ItemsSource="{Binding Devices}">
        <Pivot.HeaderTemplate>
            <DataTemplate>
                <TextBlock>
                    <TextBlock.Text>
                        <Binding Path="Device_Info.HardwareRevNumMajor"/>
                    </TextBlock.Text>
                </TextBlock>

                <!--<TextBlock Text="{Binding HardwareRevNumMajor}">
                </TextBlock>-->
            </DataTemplate>
        </Pivot.HeaderTemplate>
        <Pivot.ItemTemplate>
            <DataTemplate>
                <ItemsControl ItemsSource="{Binding DeviceFiles}">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="{Binding numBlocks}"/>
                                <TextBlock Text="{Binding syncTime}"/>
                            </StackPanel>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </DataTemplate>
        </Pivot.ItemTemplate>
    </Pivot>
</Grid>

一旦一个Device_Info对象被添加到DevicesobservableCollection 我得到一个错误。Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED)). 然后,如果我在设计器中单击 MainPage.xaml 文件,我会看到:

TargetException: Object does not match target type.
StackTrace:
at System.Reflection.RuntimeMethodInfo.CheckConsistency(Object target)
at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, 
BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture)
at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, Object[] index)
InnerException: None

您可以从上面的 xaml 中看到,我尝试了多种方法来显示注释掉的代码HarwareRevNumMajor中存在的属性。Device_InfoTextBlock

我的 ViewModel 看起来像这样:

namespace usb_test
{
    public class View_Model
    {
        public ObservableCollection<Device_Info> _devices;
        public ObservableCollection<File_Info> _deviceFiles;
        public CoreDispatcher _dispatcher;
    public View_Model() {
        _devices = new ObservableCollection<Device_Info>();
        _deviceFiles = new ObservableCollection<File_Info>();
    }

    public ObservableCollection<Device_Info> Devices
    {
        get
        {
            return _devices;
        }
    }

    public ObservableCollection<File_Info> DeviceFiles
    {
        get
        {
            return _deviceFiles;
        }
    }

在我的 MainPage.xaml.cs 我有这个:

namespace usb_test
{
    public sealed partial class MainPage : Page
    {
     Device_List devList = new Device_List();
     Device_Structure deviceStruct = new Device_Structure();
     View_Model viewModel = new View_Model();
     public MainPage()
     {
         this.InitializeComponent();
         viewModel._dispatcher = Dispatcher;
         this.DataContext = viewModel;
         devList.devices.CollectionChanged += this.OnCollectionChanged;
         deviceStruct.deviceFiles.CollectionChanged += this.OnCollectionChanged;
...

这行代码将设备添加到列表中:

await viewModel._dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() => { devList.devices.Add(devInfo); });

此行成功,并将一个Dev_Info对象添加到设备 observableCollection 中,并在应用程序崩溃后不久。

我确信当我稍后尝试在 xaml 中显示文件信息时会出现更多错误,但我真的很感激让数据透视表头正确显示。就像我说的那样,我对此很陌生,所以我假设存在许多问题,对于阻止我无法显示Dev_Info对象属性的任何建议/澄清,我将不胜感激。

谢谢!

4

2 回答 2

0

感谢以上建议。我取得了一些进展。

我像这样重写了我的xaml:

<Page
x:Class="usb_test.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:usb_test"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:code="using:usb_test"
mc:Ignorable="d">

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Pivot x:Name="pivot1" Title="Pivot" Opacity="0.99" ItemsSource="{x:Bind viewModel.Devices}">
        <Pivot.HeaderTemplate>
            <DataTemplate x:DataType="local:Device_Info">
                <TextBlock Text="{x:Bind HardwareRevNumMajor}"></TextBlock>
            </DataTemplate>
        </Pivot.HeaderTemplate>
        <Pivot.ItemTemplate>
            <DataTemplate x:DataType="local:Device_Info">
                <ListView ItemsSource="{x:Bind devStruct.deviceFiles}" Grid.Row="1">
                    <ListView.ItemTemplate>
                        <DataTemplate x:DataType="local:File_Info">
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Foreground="Red" Text="{x:Bind fileName}" HorizontalAlignment="Center" Margin="0,0,20,0"/>
                                <TextBlock Foreground="Red" Text="{x:Bind numBlocks}" HorizontalAlignment="Center" Margin="0,0,20,0"/>
                                <Button Content="Download File" Click="{x:Bind onDownloadClick}" HorizontalAlignment="Center" Margin="0,0,20,0">
                                </Button>
                            </StackPanel>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
            </DataTemplate>
        </Pivot.ItemTemplate>
    </Pivot>
</Grid>

你会注意到我拿出了

<Page.DataContext>
  <local:View_Model/>
</Page.DataContext>

从我以前的 xaml 代码。

然后我ItemsSource="{x:Bind viewModel.Devices}">在 Pivot 中使用。在我的 MainPage.xaml.cs 中定义,是我在上面创建viewModel的类的一个实例。View_Model然后从那里我添加了DataTypea DataTemplateDevice_Info然后我可以轻松地访问Device_Info对象的属性,在这种情况下是HardwareRevNumMajor

PivotItemTemplate 有点混乱。该类Device_Info有一个名为的属性,它devStruct是我创建的一个类 ( Device_Struct)。的实例Device_Struct具有File_Info对象的 ObservableCollection。所以这个集合告诉我们设备上的文件,它被称为deviceFiles. 这就是我想在 Pivot Items 中使用的集合。我不确定是否有一种方法可以在不将 a<ListView>插入 XAML 的情况下执行此操作,但这是我发现的有效方法。有了这个,我现在可以显示有关每个枢轴项上的文件的信息。

我也从 using 切换Bindingx:Binding. 老实说,我不确定为什么会有所帮助,但是当我使用 Binding 时,我得到了:Object reference not set to an instance of an object. 如果有人对此有更好的理解,我很想听听。再次感谢!

于 2017-08-18T19:34:59.283 回答
0

您的项目来自Devicestype Device_Info。在您DataTemplate将 text 属性绑定到Device_Info.HardwareRevNumMajor. 由于上下文是您的项目 (Device_Info),因此您尝试访问属性 Device_Info。

如果你写:

<TextBlock Text="{Binding HardwareRevNumMajor}" />

您尝试访问HardwareRevNumMajorDevice_Info 中可能存在的属性。

于 2017-08-18T13:52:14.407 回答