我正在关注Head First C#第3版,在第一章中它要求我更改TextBlock的文本,但是当我尝试时我得到以下错误:
InvalidCastException:无法将类型为“Windows.UI.Text.TextDecorations”的对象强制转换为 type'System.Windows.TextDecorationCollection”。
本书建议我使用Visual Studio Express 2012 for Windows 8,我正在遵循这些建议。
XAML代码:
<common:LayoutAwarePage
x:Name="pageRoot"
x:Class="Save_The_Humans.MainPage"
DataContext="{Binding DefaultViewModel, RelativeSource={RelativeSource Self}}"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Save_The_Humans"
xmlns:common="using:Save_The_Humans.Common"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Page.Resources>
<!-- TODO: Delete this line if the key AppName is declared in App.xaml -->
<x:String x:Key="AppName">Save The Humans</x:String>
</Page.Resources>
<!--
This grid acts as a root panel for the page that defines two rows:
* Row 0 contains the back button and page title
* Row 1 contains the rest of the page layout
-->
<Grid Style="{StaticResource LayoutRootStyle}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="160"/>
<ColumnDefinition/>
<ColumnDefinition Width="160"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="140"/>
<RowDefinition/>
<RowDefinition Height="160"/>
</Grid.RowDefinitions>
<!-- Back button and page title -->
<Grid Grid.ColumnSpan="3">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Button x:Name="backButton" Click="GoBack" IsEnabled="{Binding Frame.CanGoBack, ElementName=pageRoot}" Style="{StaticResource BackButtonStyle}"/>
<TextBlock x:Name="pageTitle" Grid.Column="1" Text="{StaticResource AppName}" Style="{StaticResource PageHeaderTextStyle}"/>
</Grid>
<Button x:Name="startButton" Content="Start!" HorizontalAlignment="Center" Grid.Row="2" VerticalAlignment="Center"
RenderTransformOrigin="0.562,0.526"/>
<ProgressBar Grid.Column="1" HorizontalAlignment="Left" Height="10" Margin="475,37,0,0" Grid.Row="2" VerticalAlignment="Top" Width="100"/>
<StackPanel Grid.Column="2" Margin="33,37,50,85" Orientation="Vertical" Grid.Row="2">
<ContentControl Content="ContentControl" HorizontalAlignment="Left" VerticalAlignment="Top"/>
</StackPanel>
<Canvas Grid.Column="1" HorizontalAlignment="Left" Height="100"
Margin="475,175,0,0" Grid.Row="1" VerticalAlignment="Top" Width="100"/>
<TextBlock Grid.Column="2" HorizontalAlignment="Left" Margin="24,2,0,0" Grid.Row="2" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Style="{StaticResource SubheaderTextStyle}"/>
<VisualStateManager.VisualStateGroups>
<!-- Visual states reflect the application's view state -->
<VisualStateGroup x:Name="ApplicationViewStates">
<VisualState x:Name="FullScreenLandscape"/>
<VisualState x:Name="Filled"/>
<!-- The entire page respects the narrower 100-pixel margin convention for portrait -->
<VisualState x:Name="FullScreenPortrait">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="backButton" Storyboard.TargetProperty="Style">
<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PortraitBackButtonStyle}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<!-- The back button and title have different styles when snapped -->
<VisualState x:Name="Snapped">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="backButton" Storyboard.TargetProperty="Style">
<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource SnappedBackButtonStyle}"/>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="pageTitle" Storyboard.TargetProperty="Style">
<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource SnappedPageHeaderTextStyle}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Grid>
</common:LayoutAwarePage>
源代码
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
// The Basic Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234237
namespace Save_The_Humans
{
/// <summary>
/// A basic page that provides characteristics common to most applications.
/// </summary>
public sealed partial class MainPage : Save_The_Humans.Common.LayoutAwarePage
{
public MainPage()
{
this.InitializeComponent();
}
/// <summary>
/// Populates the page with content passed during navigation. Any saved state is also
/// provided when recreating a page from a prior session.
/// </summary>
/// <param name="navigationParameter">The parameter value passed to
/// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested.
/// </param>
/// <param name="pageState">A dictionary of state preserved by this page during an earlier
/// session. This will be null the first time a page is visited.</param>
protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
{
}
/// <summary>
/// Preserves state associated with this page in case the application is suspended or the
/// page is discarded from the navigation cache. Values must conform to the serialization
/// requirements of <see cref="SuspensionManager.SessionState"/>.
/// </summary>
/// <param name="pageState">An empty dictionary to be populated with serializable state.</param>
protected override void SaveState(Dictionary<String, Object> pageState)
{
}
}
}
错误文字:
InvalidCastException: Unable to cast object of type 'Windows.UI.Text.TextDecorations' to type 'System.Windows.TextDecorationCollection'.
堆栈跟踪
at Microsoft.Expression.DesignSurface.Tools.Text.TextBlockEditProxy.Instantiate()
at Microsoft.Expression.DesignSurface.Tools.Text.TextEditProxy.AddToScene(Boolean visible)
at Microsoft.Expression.DesignSurface.Tools.Text.TextToolBehavior.AddEditProxyToScene(TextEditProxy textEditProxy, Boolean visible)
at Microsoft.Expression.DesignSurface.Tools.Text.TextToolBehavior.FindOrCreateEditProxy(SceneNode textElement, Boolean active)
at Microsoft.Expression.DesignSurface.Tools.Text.TextToolBehavior.BeginTextEdit(SceneNode textElement)
at Microsoft.Expression.DesignSurface.Tools.Text.TextToolBehavior.EditDifferentElement(SceneNode element, Boolean returnFocus)
at Microsoft.Expression.DesignSurface.Tools.Text.TextToolBehavior.OnAttach()
at Microsoft.Expression.DesignSurface.Tools.EventRouter.PushBehavior(ToolBehavior newActiveBehavior)
at Microsoft.Expression.DesignSurface.View.SceneView.TryEnterTextEditMode(Boolean textElementOnly)
at Microsoft.Expression.DesignSurface.SceneCommands.EditTextCommand.Execute()
at Microsoft.Expression.Utility.Commands.CommandTarget.ExecuteCommand(String commandName, CommandInvocationSource invocationSource)
at Microsoft.Expression.Utility.UserInterface.CommandBarButtonBase.<>c__DisplayClass1.<Execute>b__0()
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
InnerException: None
为什么我得到两个减号?!
答案 0 :(得分:0)
我错误地解决了这个问题,在我收到错误后我改变了XAML代码中的Text值,问题解决了,但是当我点击编辑文本时我仍然会收到错误。
答案 1 :(得分:0)
我遇到了同样的问题,事实证明,这是因为我使用Windows 10。
就我而言,您不能在较早或较新的操作系统上构建Windows 8应用程序-总而言之,此Visual Studio是为Windows 8专门设计的,因此我认为这是有道理的。
无论如何,解决该问题的方法是使用本书的WPF版本。您可以在作者的Github页面上免费找到它-> https://github.com/head-first-csharp/third-edition。
所以我要做的是按照书中的说明安装适用于Windows的Visual Studio 2013 Express,并按照此方法进行操作。
首先要构建相同的Alien Invasion应用程序,然后当我不得不重命名文本块时,一切工作都很好。其他步骤也是如此。