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 @   FredGrit  阅读(45)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· DeepSeek在M芯片Mac上本地化部署
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
历史上的今天:
2016-08-23 WPF,ComboBox,取汉字首字母,extBoxBase.TextChanged
点击右上角即可分享
微信分享提示