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(); } } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 没有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