这是我第一次构建UWP应用程序,并且我一般都是C#/ Windows的新手。我正尝试在用户界面中使用透视。我希望枢轴标题来自连接的usb设备的ObservableCollection
,其类别为Device_Info
。每个USB设备都有一个名为HardwareRevNumMajor
的属性,我希望将其显示为每个支点标题。我的XAML看起来像这样:uwp xaml DataBinding ObservableCollection内部的嵌套属性
<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
对象被添加到Devices
的ObservableCollection我得到一个错误。 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_Info
见由注释掉的TextBlock
代码。
我的视图模型是这样的:
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和shor在应用程序崩溃之后。
我敢肯定,当我尝试在xaml中稍后显示文件信息的东西时会出现更多错误,但是我真的很感谢只是让pivot header正确显示。就像我说的,我对这个很陌生,所以我假设存在一些问题,我会很感激任何建议/澄清什么阻止了我无法显示Dev_Info
对象的属性。
谢谢!
'灾难性故障'是xaml框架中相当普遍的错误。隔离各个组件以试图找出失败的原因。您能否将''成功绑定到可观察集合之外的单个Device_Info?如果您注释掉HeaderTemplate,上述xaml是否工作?如果你注释掉ItemTemplate?都? –