根级别的数据无效。第1行,第1位.XML无效

时间:2016-07-06 14:50:06

标签: wpf

我的代码中有两个错误:

  • 根级别的数据无效。第1行,第1位。
  • 标记文件无效。指定扩展名为.xaml的源标记文件。

为什么?

MainWindow.xaml:

<Window x:Class="TestWpfApplication.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Eight Ball Answer" Height="328" Width="412" >
    <Grid Name="grid1">
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <TextBox VerticalAlignment="Stretch" HorizontalAlignment="Stretch"     Margin="10,10,13,10" Name="txtQuestion"      TextWrapping="Wrap" FontFamily="Verdana" FontSize="24"     Grid.Row="0">[Place question here.]</TextBox>
        <Button VerticalAlignment="Top" HorizontalAlignment="Left"     Margin="10,0,0,20" Width="127" Height="23" Name="cmdAnswer"     Click="cmdAnswer_Click" Grid.Row="1">Ask the Eight Ball</Button>
        <TextBox VerticalAlignment="Stretch" HorizontalAlignment="Stretch"     Margin="10,10,13,10" Name="txtAnswer" TextWrapping="Wrap"     IsReadOnly="True" FontFamily="Verdana" FontSize="24" Foreground="Green"     Grid.Row="2">[Answer will appear here.]</TextBox>
        <Grid.Background>
            <LinearGradientBrush>
                <LinearGradientBrush.GradientStops>
                    <GradientStop Offset="0.00" Color="Red" />
                    <GradientStop Offset="0.50" Color="Indigo" />
                    <GradientStop Offset="1.00" Color="Violet" />
                </LinearGradientBrush.GradientStops>
            </LinearGradientBrush>
        </Grid.Background>
    </Grid>
</Window>

MainWindow.xaml.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace TestWpfApplication
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void cmdAnswer_Click(object sender, RoutedEventArgs e)
        {
            this.Cursor = Cursors.Wait;            

            txtAnswer.Text = "AAA";
            this.Cursor = null;
        }
    }
}

更新1:

  1. 此页面是通过复制和粘贴方法创建的,而不是由设计师创建的。
  2. MainWindow.xaml将属性BuildAction设置为Page。
  3. 有用吗?

2 个答案:

答案 0 :(得分:1)

更新:解决方法是创建一个新项目并复制自己的东西。

你的.xaml似乎有效,你确定错误是指MainWindow.xaml吗?

答案 1 :(得分:0)

另一种解决方案:

  1. 创建xml文件Foo.xml

  2. 将xaml文件的内容复制到Foo.xml xml文件

  3. 复制xaml文件名

  4. 删除xaml文件

并且您应该检查您的csporj文件,并确保新的xaml文件将作为Page包含在内并进行编译 1.将Foo.xml xml文件名重命名为xaml文件名