C# 无法强制转换类型为';Windows.UI.Xaml.Controls.Grid';输入';Windows.UI.Xaml.Controls.ListView';
我有一个带有两个xaml渲染的UWP应用程序,这个程序名为SongFix.xaml(Estandar视图) 如果我在SongFix.xaml上删除名为lstanzas的列表视图,应用程序将在我的电脑上正确部署 如果错误在标准视图中,为什么会在PC部署中发生此错误? 如果标准视图在我的手机上正常运行,那么它有什么问题 注意:我使用的是Visual Studio Community 2015及其更新2 在这个版本的VS中,这可能是一个Bug吗 编辑1SongFix.g.cs文件C# 无法强制转换类型为';Windows.UI.Xaml.Controls.Grid';输入';Windows.UI.Xaml.Controls.ListView';,c#,wpf,xaml,uwp,C#,Wpf,Xaml,Uwp,我有一个带有两个xaml渲染的UWP应用程序,这个程序名为SongFix.xaml(Estandar视图) 如果我在SongFix.xaml上删除名为lstanzas的列表视图,应用程序将在我的电脑上正确部署 如果错误在标准视图中,为什么会在PC部署中发生此错误? 如果标准视图在我的手机上正常运行,那么它有什么问题 注意:我使用的是Visual Studio Community 2015及其更新2 在这个版本的VS中,这可能是一个Bug吗 编辑1SongFix.g.cs文件 namespace
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;
}
}
}
namespace Tablaturas.Views
{
部分类SongFix:
全局::Windows.UI.Xaml.Controls.Page,
全局::Windows.UI.Xaml.Markup.IComponentConnector,
全局::Windows.UI.Xaml.Markup.IComponentConnector2
{
///
///连接()
///
[全局::System.CodeDom.Compiler.GeneratedCodeAttribute(“Microsoft.Windows.UI.Xaml.Build.Tasks”,“14.0.0.0”)]
[全局::System.Diagnostics.DebuggerNonUserCodeAttribute()]
public void Connect(int connectionId,对象目标)
{
交换机(连接ID)
{
案例1:
{
this.LstStanzas=(全局::Windows.UI.Xaml.Controls.ListView)(目标);
}
打破
违约:
打破
}
这是。_contentLoaded=true;
}
[全局::System.CodeDom.Compiler.GeneratedCodeAttribute(“Microsoft.Windows.UI.Xaml.Build.Tasks”,“14.0.0.0”)]
[全局::System.Diagnostics.DebuggerNonUserCodeAttribute()]
公共全局::Windows.UI.Xaml.Markup.IComponentConnector GetBindingConnector(int-connectionId,对象目标)
{
全局::Windows.UI.Xaml.Markup.IComponentConnector returnValue=null;
返回值;
}
}
}
我怀疑connectionId
是按照XAML中元素的顺序递增的。因此,在第一个文件中,lstanzas
的id为1,因为它是第一个命名元素。在第二个XAML中,GrdFixEditor
是第一个命名元素,并获取id 1,因此转换错误。如果我的理论是正确的,那么删除x:Name=“GrdFixEditor”
(离开网格,只删除名称)应该可以修复错误。我怀疑connectionId
是按照XAML中元素的顺序递增的。因此,在第一个文件中,lstanzas
的id为1,因为它是第一个命名元素。在第二个XAML中,GrdFixEditor
是第一个命名元素,并获取id 1,因此转换错误。如果我的理论是正确的,那么删除x:Name=“GrdFixEditor”
(离开网格,只删除名称)应该可以修复错误。如果错误在标准视图下,为什么会在PC部署中发生此错误?
有一个特殊文件,名为SongFix.g.cs
(应该在您的obj
文件夹中)它是根据SongFix.xaml
自动生成的。它包含属性和控件之间的映射(这就是为什么在控件上放置x:Name
时,可以从代码隐藏中的属性访问此控件)。相同的SongFix.g.cs
用于SongFix.DeviceFamily Desktop.xaml`(即使它是从SongFix.xaml
生成的。其结果是,如果您在SongFix.xaml
中放置一个列表视图lstanzas
,则在SongFix.g.cs
中会有一个lstanzas
属性,类型为listview
。如果是这样,在SongFix.DeviceFamily Desktop.xaml>中,您将同一名称用于不同类型的控件(例如,Grid
),您将遇到类型不匹配,因为SongFix.g.cs
中的lstanzas
属性仍将是ListView
类型,也就是说,我无法解释为什么会出现此问题,因为lstanzas
在两个XAML文件中的类型相同。也许您应该尝试重新构建解决方案(这与简单的构建不同)强制Visual Studio重新生成您的SongFix.g.cs
filetk。我理解您的观点,我清理了整个解决方案并多次重新构建,但问题仍然存在。哦,我没有意识到UWP中有这个connectionId
东西(很确定WPF中没有)我猜想,connectionId
是按照XAML中元素的顺序递增的。因此,在您的第一个文件中,lstanzas
具有id 1,因为它是第一个命名元素。在第二个XAML中,GrdFixEditor
是第一个命名元素,并获取id 1,因此转换错误。如果我的理论正确rect,然后删除x:Name=“GrdFixEditor”
(离开网格,只需删除名称)应该修复错误如果错误在标准视图中,为什么会在PC部署中发生此错误?
有一个名为SongFix.g.cs
的特殊文件(应该在obj
文件夹中)它基于SongFix.xaml
自动生成。它包含属性和控件之间的映射(这就是为什么在控件上放置x:Name
时,可以从代码隐藏中的属性访问此控件).相同的SongFix.g.cs
用于SongFix.DeviceFamily Desktop.xaml`(即使它是从SongFix.xaml
生成的。其结果是,如果您在SongFix.xaml
中放置一个列表视图lstanzas
,则在SongFix.g.cs
中会有一个lstanzas
属性,类型为listview
。如果是这样,在SongFix.DeviceFamily Desktop.xaml>中,您将对于不同类型的控件(例如,Grid
),如果名称相同,则类型不匹配,因为SongFix.g.cs
中的lstanzas
属性仍然是Li类型
<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'.
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;
}
}
}