【问题标题】:DataContext class not found in XAML Designer (while everything looks fine)在 XAML 设计器中找不到 DataContext 类(虽然一切看起来都很好)
【发布时间】:2019-06-17 13:35:48
【问题描述】:

我正在尝试通过

在 XAML 文件中设置我的数据上下文
<Window x:Class="LocationScout.SettingsDeleteWindow"
    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:ViewModel="clr-namespace:LocationScout.ViewModel"
    mc:Ignorable="d"
    Title="Delete" Height="315" Width="350" 
    WindowStartupLocation="CenterScreen">

<Window.DataContext>
    <ViewModel:SettingsDeleteDisplayItem/>
</Window.DataContext>

但是,XAML 编辑器显示错误“名称“SettingsDeleteDisplayItem”在命名空间“clr-namespace:LocationScout.ViewModel”中不存在。

视图模型类对我来说看起来不错:

namespace LocationScout.ViewModel
{
    public class SettingsDeleteDisplayItem : BaseObservableObject
    {
         private long _countryAreaCountToDelete;

         public long CountryAreaCountToDelete
         {
            get => _countryAreaCountToDelete;
            set
            {
                _countryAreaCountToDelete = value;
                OnPropertyChanged();
            }
        }
    }
}

构建解决方案工作正常,没有错误。任何想法?非常感谢。

【问题讨论】:

  • 设计师因误报错误而享有(当之无愧的)可怕声誉。如果你的程序运行,你只需要忽略它。
  • 哦。这是在设计师?重建你的项目。如果这不起作用,请重新启动 Visual Studio 并重建您的项目。如果做不到这一点,请尝试将 ViewModel 更改为小写,如 xmlns:viewModel
  • @RobertHarvey - 重建没有帮助,但我改为小写并重新启动 Visual Studio - 现在它可以工作了。非常感谢。真的是一个但在设计师...
  • 这可能是一个问题的原因是因为ViewModel(大写)出现在您的命名空间声明中。
  • 我觉得这听起来很合理......

标签: c# wpf xaml namespaces datacontext


【解决方案1】:

@RobertHarvey 在评论中建议,我更改为小写并重新启动 Visual Studio。现在可以了。

只是重建解决方案没有帮助(我之前尝试过)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-08
    • 2019-08-21
    • 1970-01-01
    相关资源
    最近更新 更多