這是我第一次構建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?都? –