视图中的XamlParseException [英] XamlParseException in View

查看:108
本文介绍了视图中的XamlParseException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个只显示标签的视图.

I've got a view that shows only a Label.

由于将标签文本绑定到viewmodel属性,因此将viewmodel正确注入到视图中.现在,如果我尝试在xaml中定义一个DataGrid,则会收到XamlParseException:

The viewmodel is injected correctly in the view because the text of the label is bound to a viewmodel property. Now, if I try to define a DataGrid in the xaml, I've got a XamlParseException:

{System.Windows.Markup.XamlParseException:找不到类型'DataGrid'. [线:16位:45] su System.Windows.Application.LoadComponent(对象组件,Uri resourceLocator) su Common.Views.FunctionalityView.InitializeComponent() su Common.Views.FunctionalityView..ctor(IFunctionalityViewModel viewModel)}

{System.Windows.Markup.XamlParseException: Type 'DataGrid' not found. [Line: 16 Position: 45] su System.Windows.Application.LoadComponent(Object component, Uri resourceLocator) su Common.Views.FunctionalityView.InitializeComponent() su Common.Views.FunctionalityView..ctor(IFunctionalityViewModel viewModel)}

但是,如果我在InitializeComponent();起作用之前定义了DataGrid myDg = new DataGrid().

BUT if I define a DataGrid myDg = new DataGrid() right before the InitializeComponent(); it works.

我已经检查了所有引用,但仍然找不到问题.

I've checked all references and still can't find the problem.

推荐答案

听起来您的默认名称空间被弄乱或丢失.没有xaml,很难说出应该做什么.

It sounds like your default namespace is messed up or missing. Without the xaml, it is hard to tell what you should do.

一个简单的解决方法就是创建一个新的UserControl,然后检查其根上定义的xmlns名称空间并将其与View的根元素进行比较.

An easy way to figure this out for yourself is to create a new UserControl, then examine and compare the xmlns namespaces defined on its root with the root element of your View.

WPF通过专门的名称空间定义来查找类型.它遵循格式

WPF locates types by a specialized namespace definition. It follows the format

clr-命名空间:[namespace](; assembly = [assembly name])

clr-namespace:[namespace](;assembly=[assembly name])

其中

[命名空间]

[namespace]

是包含类型定义的名称空间.而且,如果在与xaml文件所在的程序集不同的程序集中定义了类型,则必须在前言中包括该零件. [assembly name]是不带.dll扩展名的程序集的名称(例如,assembly=mscorlib会导入mscorlib.dll).要导入Int32类型并在您的xaml中使用它,您必须定义名称空间

is the namespace that contains the type definition. And, if the type is defined within a different assembly from the one where the xaml file is located, you have to include the part within the preface. [assembly name] is the name of the assembly without the .dll extension (e.g., assembly=mscorlib would import mscorlib.dll). To import the Int32 type and use it within your xaml, you'll have to define the namespace

xmlns:s="clr-namespace:System;assembly=mscorlib"

还存在一个程序集级属性允许您为程序集中的所有类型分配一个不同的命名空间.通常,这采用URL的形式. IIRC,这是传统的做法,而不是必要的做法.这就是为什么某些控件使用更传统的名称空间来标识的原因,例如

There also exists an assembly-level attribute which allows you to assign a different namespace for all types within an assembly. Typically, this takes the form of a URL. This is by tradition rather than necessity, IIRC. This is why some controls are identified with a more traditional namespace, such as

xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

这篇关于视图中的XamlParseException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆