在 Window 上设置设计时 DataContext 会导致编译器错误? [英] Setting design time DataContext on a Window is giving a compiler error?

查看:93
本文介绍了在 Window 上设置设计时 DataContext 会导致编译器错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 WPF 应用程序的主窗口下面有以下 XAML,我正在尝试设置下面的设计时间 d:DataContext,我可以成功地为我的所有各种 UserControl 执行此操作,但是当我尝试在窗口上执行此操作时,它给了我这个错误...

I have the following XAML below for the main window in my WPF application, I am trying to set the design time d:DataContext below, which I can successfully do for all my various UserControls, but it gives me this error when I try to do it on the window...

错误 1 ​​属性DataContext"必须位于默认命名空间或元素命名空间http://schemas.microsoft.com/winfx/2006/xaml/presentation"中.Line 8 Position 9. C:\dev\bplus\PMT\src\UI\MainWindow.xaml 8 9 UI

<Window x:Class="BenchmarkPlus.PMT.UI.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:UI="clr-namespace:BenchmarkPlus.PMT.UI"
    xmlns:Controls="clr-namespace:BenchmarkPlus.PMT.UI.Controls"
    d:DataContext="{d:DesignInstance Type=UI:MainViewModel, IsDesignTimeCreatable=True}"
    Title="MainWindow" Height="1000" Width="1600" Background="#FF7A7C82">

    <Grid>
        <!-- Content Here -->
    </grid>

</Window>

推荐答案

我需要将 mc:Ignorable="d" 属性添加到 Window 标签.基本上我学到了一些新东西.Expression Blend/Visual Studio 设计器承认的 d: 命名空间前缀实际上被真正的编译器/xaml 解析器忽略/注释掉"

I needed to add the mc:Ignorable="d" attribute to the Window tag. Essentially I learned something new. The d: namespace prefix that Expression Blend/Visual Studio designer acknowledges is actually ignored/"commented out" by the real compiler/xaml parser!

<Window 
...
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
...
/>

以下内容来自

内森,亚当 (2010-06-04).WPF 4 Unleashed(Kindle 位置 1799-1811).萨姆斯.Kindle 版.

标记兼容性

标记兼容性 XML 命名空间(http://schemas.openxmlformats.org/markup-compatibility/2006,通常与 mc 前缀一起使用)包含一个 Ignorable 属性,该属性指示 XAML 处理器在无法解析为它们的 .NET 类型时忽略指定命名空间中的所有元素/属性/成员.(命名空间还有一个 ProcessContent 属性,该属性覆盖被忽略命名空间内特定类型的 Ignorable.)

The markup compatibility XML namespace (http://schemas.openxmlformats.org/markup-compatibility/2006, typically used with an mc prefix) contains an Ignorable attribute that instructs XAML processors to ignore all elements/attributes in specified namespaces if they can’t be resolved to their .NET types/members. (The namespace also has a ProcessContent attribute that overrides Ignorable for specific types inside the ignored namespaces.)

Expression Blend 利用此功能执行一些操作,例如向 XAML 内容添加可在运行时忽略的设计时属性.

Expression Blend takes advantage of this feature to do things like add design-time properties to XAML content that can be ignored at runtime.

mc:Ignorable 可以给出一个以空格分隔的命名空间列表,而 mc:ProcessContent 可以给出一个以空格分隔的元素列表.当 XamlXmlReader 遇到无法解析的可忽略内容时,它不会为其报告任何节点.如果可以解决可忽略的内容,则正常上报.所以消费者不需要做任何特殊的事情来正确处理标记兼容性.

mc:Ignorable can be given a space-delimited list of namespaces, and mc:ProcessContent can be given a space-delimited list of elements. When XamlXmlReader encounters ignorable content that can’t be resolved, it doesn’t report any nodes for it. If the ignorable content can be resolved, it will be reported normally. So consumers don’t need to do anything special to handle markup compatibility correctly.

这篇关于在 Window 上设置设计时 DataContext 会导致编译器错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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