简体   繁体   English

主 window 上的“DataContext is not set”消息

[英]“DataContext is not set” message on main window

I'm getting the XLS1106 "DataContext is not set" message on my main window XAML in Visual Studio.我在 Visual Studio 中的主 window XAML 上收到 XLS1106“DataContext is not set”消息。 As far as I can tell, I haven't used anything related to that and nothing is broken.据我所知,我没有使用任何与此相关的东西,也没有任何问题。 I would just supress the message, but I'm not sure why it's there.我会压制这条消息,但我不确定它为什么会在那里。

Here is my whole XAML:这是我的整个 XAML:

<Window x:Name="MyWindow" x:Class="Whiteboard.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:Whiteboard"
        mc:Ignorable="d"
        Title="Untitled - CFWhiteboard" Height="450" Width="800" MouseDown="WindowMouseDown" MouseMove="WindowMouseMove" KeyDown="WindowKeyDown" KeyUp="WindowKeyUp" Icon="icons8-interactive-whiteboard-100.png" Loaded="WindowLoaded" Closing="WindowClosing">
    <Window.Resources>
        <ResourceDictionary>
            <FrameworkElement x:Key="CursorRectangle" Cursor="Resources/Cursors/rectangle.cur"/>
            <FrameworkElement x:Key="CursorSquare" Cursor="Resources/Cursors/lockrect.cur"/>
            <FrameworkElement x:Key="CursorEllipse" Cursor="Resources/Cursors/ellipse.cur"/>
            <FrameworkElement x:Key="CursorCircle" Cursor="Resources/Cursors/lockellipse.cur"/>
        </ResourceDictionary>
    </Window.Resources>
    <Canvas x:Name="MainCanvas"/>
</Window>

The message is a false positive.该消息是误报。 The fix for the problem is available in the latest preview version of Visual Studio: Visual Studio 的最新预览版中提供了该问题的修复程序:

Microsoft Solution - Bhavya Udayashankar [MSFT] Closed - Fixed ··· Microsoft 解决方案 - Bhavya Udayashankar [MSFT] 已关闭 - 已修复···

A fix for this issue has been released!已发布此问题的修复程序! Install the most recent preview release from https://visualstudio.microsoft.com/downloads/ .https://visualstudio.microsoft.com/downloads/安装最新的预览版。 Thank you for providing valuable feedback which has helped improve the product.感谢您提供有助于改进产品的宝贵反馈。

See issue "XLS1106 on virgin C# WPF.Net application" on the Visual Studio Developer Community site.请参阅 Visual Studio 开发人员社区网站上的问题“原始 C# WPF.Net 应用程序上的 XLS1106”。

In the simplest applications without any data bindings set up, there is no need for a data context to be set, and the warning given can be safely ignored.在没有设置任何数据绑定的最简单的应用程序中,不需要设置数据上下文,并且可以安全地忽略给出的警告。

Note that you should NOT follow the advice of the comment above telling you to add DataContext = this;请注意,您应该遵循上面告诉您添加DataContext = this; to the window's constructor for real-world applications.到实际应用程序的窗口构造函数。 Write a proper view model object type, and create an instance of that to set as your DataContext reference.编写一个正确的视图 model object 类型,并创建一个实例以设置为您的DataContext引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM