我有一个带有两个xaml的UWP应用程序呈现这个名为 SongFix.xaml (estándarview)
<Page
x:Class="Tablaturas.Views.SongFix"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Tablaturas.Views"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:cvt="using:Tablaturas.Code"
xmlns:ctr="using:Tablaturas.Controls"
xmlns:data="using:Tablaturas.Model"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Pivot>
<PivotItem>
<PivotItem.Header>
<TextBlock Text="Fix Info"/>
</PivotItem.Header>
<Grid>
</Grid>
</PivotItem>
<PivotItem>
<PivotItem.Header>
<TextBlock Text="Harmonize"/>
</PivotItem.Header>
<Grid>
<ListView x:Name="LstStanzas"
ItemsSource="{Binding SongsViewModel.SongFixViewModel.FixStructure}"
ItemsPanel="{StaticResource StanzaItemTemplate}"
Grid.Column="2"
HorizontalContentAlignment="Stretch"
ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemTemplate>
<DataTemplate>
<ctr:StanzaEditorControl StanzaToEdit="{Binding }" FixStructure="{Binding ElementName=LstStanzas,Path=ItemsSource}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</PivotItem>
</Pivot>
<TextBlock Text="{Binding Warnings.Message}" Visibility="{Binding SongsViewModel.SongFixViewModel.Warnings.WarningVisible,Converter={StaticResource BoolToVis}}" Style="{StaticResource WarningMessage}" Grid.Row="1"/>
<SplitView IsPaneOpen="{Binding SongsViewModel.SongFixViewModel.SyllabeEditorIsOpen}"
CompactPaneLength="0"
OpenPaneLength="300" PaneBackground="#FF641414" PanePlacement="Right">
<SplitView.Pane>
<Grid>
<ctr:SyllabeEditorControl SyllabeViewModel="{Binding SongsViewModel.SongFixViewModel.SyllabeEditorModel,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"/>
</Grid>
</SplitView.Pane>
</SplitView>
</Grid>
</Page>
这个名为 SongFix.DeviceFamily-Desktop.xaml
<Page
x:Class="Tablaturas.SongFix"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Tablaturas"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:cvt="using:Tablaturas.Code"
xmlns:ctr="using:Tablaturas.Controls"
xmlns:data="using:Tablaturas.Model"
mc:Ignorable="d">
<Grid x:Name="GrdFixEditor" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="2*"/>
<ColumnDefinition Width="1.5*"/>
<ColumnDefinition Width="4*"/>
</Grid.ColumnDefinitions>
<Grid>
</Grid>
<ListView x:Name="LstStanzas"
ItemsSource="{Binding SongsViewModel.SongFixViewModel.FixStructure}"
ItemsPanel="{StaticResource StanzaItemTemplate}"
Grid.Column="2"
HorizontalContentAlignment="Stretch"
ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemTemplate>
<DataTemplate>
<ctr:StanzaEditorControl StanzaToEdit="{Binding }" FixStructure="{Binding ElementName=LstStanzas,Path=ItemsSource}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
这是我的代码隐藏
namespace Tablaturas.Views
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class SongFix : Page
{
public SongFix()
{
this.InitializeComponent();
}
}
}
现在我的问题是当我部署到手机时一切正常,但是当部署到我的电脑时它会抛出这个错误:
An exception of type 'System.InvalidCastException' occurred in Tablaturas.exe but was not handled in user code
Additional information: Unable to cast object of type 'Windows.UI.Xaml.Controls.Grid' to type 'Windows.UI.Xaml.Controls.ListView'.
如果我删除了 SongFix.xaml 中名为 LstStanzas 的ListView,则应用会在我的电脑上正确部署。
如果错误处于标准视图,为什么在PC部署中会出现此错误? 标准视图有什么问题,如果它在我的手机上正常运行?
注意:我正在使用Visual Studio Community 2015和更新2
这个版本的VS可能是这个Bug吗?
编辑1 SongFix.g.cs文件
namespace Tablaturas.Views
{
partial class SongFix :
global::Windows.UI.Xaml.Controls.Page,
global::Windows.UI.Xaml.Markup.IComponentConnector,
global::Windows.UI.Xaml.Markup.IComponentConnector2
{
/// <summary>
/// Connect()
/// </summary>
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Windows.UI.Xaml.Build.Tasks"," 14.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
public void Connect(int connectionId, object target)
{
switch(connectionId)
{
case 1:
{
this.LstStanzas = (global::Windows.UI.Xaml.Controls.ListView)(target);
}
break;
default:
break;
}
this._contentLoaded = true;
}
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Windows.UI.Xaml.Build.Tasks"," 14.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
public global::Windows.UI.Xaml.Markup.IComponentConnector GetBindingConnector(int connectionId, object target)
{
global::Windows.UI.Xaml.Markup.IComponentConnector returnValue = null;
return returnValue;
}
}
}
答案 0 :(得分:3)
我怀疑connectionId
按照XAML中元素的顺序递增。因此,在您的第一个文件中,LstStanzas
的ID为1,因为它是第一个命名的元素。在第二个XAML中,GrdFixEditor
是第一个命名元素并获取id 1,因此转换错误。如果我的理论是正确的,那么删除x:Name="GrdFixEditor"
(离开网格,只删除名称)应该修复错误。