WPF ystem.Windows.Markup.XamlParseException HResult=0x80131501 Message='Specified class name '' doesn't match actual root instance type 'System.Windows.Window'.

System.Windows.Markup.XamlParseException
  HResult=0x80131501
  Message='Specified class name 'WpfApp268.MainWindow' doesn't match actual root instance type 'System.Windows.Window'. Remove the Class directive or provide an instance via XamlObjectWriterSettings.RootObjectInstance.' Line number '1' and line position '9'.
  Source=PresentationFramework
  StackTrace:
   at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
   at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, Boolean skipJournaledProperties, Uri baseUri)
   at System.Windows.Markup.XamlReader.Load(XamlReader xamlReader, ParserContext parserContext)
   at System.Windows.Markup.XamlReader.Load(XmlReader reader, ParserContext parserContext, XamlParseMode parseMode, Boolean useRestrictiveXamlReader, List`1 safeTypes)
   at System.Windows.Markup.XamlReader.Load(Stream stream, ParserContext parserContext, Boolean useRestrictiveXamlReader)
   at System.Windows.Markup.XamlReader.Load(Stream stream, ParserContext parserContext)
   at System.Windows.Markup.XamlReader.Load(Stream stream)
   at WpfApp268.MainWindow..ctor() in D:\C\WpfApp268\MainWindow.xaml.cs:line 31

  This exception was originally thrown at this call stack:
    [External Code]

Inner Exception 1:
XamlObjectWriterException: 'Specified class name 'WpfApp268.MainWindow' doesn't match actual root instance type 'System.Windows.Window'. Remove the Class directive or provide an instance via XamlObjectWriterSettings.RootObjectInstance.' Line number '1' and line position '9'.

 

 

The solution as the above suggested,"Remove the Class directive"

 

//Former
<Window x:Class="WpfApp268.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp268" Topmost="True"
        mc:Ignorable="d"
        Title="MainWindowXamlReader.Load()" Height="450" Width="800">
    <Grid>
        <Button Content="Load" Width="200" Height="100" Background="Black"/>
    </Grid>
</Window>



//Updated
<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp268" Topmost="True"
        mc:Ignorable="d"
        Title="MainWindowXamlReader.Load()" Height="450" Width="800">
    <Grid>
        <Button Content="Load" Width="200" Height="100" Background="Black"/>
    </Grid>
</Window>

 

 

 

 

using System;
using System.Collections.Generic;
using System.IO;
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.Markup;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp268
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Window win = null;
            using(FileStream fs=new FileStream("MainWin.xaml",FileMode.Open,FileAccess.Read))
            {
                win=(Window)XamlReader.Load(fs);
                win.Show();
            }
        }
    }
}

 

posted @ 2024-08-23 20:22  FredGrit  阅读(6)  评论(0编辑  收藏  举报