【问题标题】:The name xxx DataProvider does not exist in the namespace名称空间中不存在名称 xxx DataProvider
【发布时间】:2020-03-16 08:33:06
【问题描述】:

我对 xaml 不是很熟悉。有人可以帮助我。为什么“名称空间中不存在名称 MercerCataloDataProvider”。据我所知,所有东西都在同一个命名空间中并且应该可用吗?我能做什么?


using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WPFOnboarding01
{
    public class MercerCatalogDataProvider
    {
        private MainDataSetTableAdapters.MercerJobCatalogTblTableAdapter adapter;

        private MainDataSet dataSet;

        public MercerCatalogDataProvider()
        {
            dataSet = new MainDataSet();
            adapter = new MainDataSetTableAdapters.MercerJobCatalogTblTableAdapter();
            adapter.Fill(dataSet.MercerJobCatalogTbl);
        }

        public DataView GetMercerCatalog()
        {
            return dataSet.MercerJobCatalogTbl.DefaultView;
        }
    }
}

块引用

    <Window x:Class="WPFOnboarding01.TestBinding"
        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:WPFOnboarding01"

        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        mc:Ignorable="d"
        Title="TestBinding" Height="450" Width="800">
    <Window.Resources>
        <!-- create an instance of our DataProvider class -->
        <ObjectDataProvider x:Key="MercerCatalogDataProvider"

            ObjectType="{x:Type local:MercerCatalogDataProvider}"/>
        <!-- define the method which is invoked to obtain our data -->
        <ObjectDataProvider x:Key="MercerJobCatalog"

          ObjectInstance="{StaticResource MercerCatalogDataProvider}"

          MethodName="GetMercerCatalog"/>

    </Window.Resources>

Error Message

【问题讨论】:

  • 尝试忽略它并单击F5,它运行了吗? XAML 设计器经常出错。或者它还没有合法地构建这个变更项目。
  • 尝试将 x:Key="MercerCatalogDataProvider" 更改为其他键名。
  • 项目构建,但设计器视图不起作用。
  • X:key 更改没有帮助
  • 您使用的是什么版本的 Visual Studio?

标签: c# wpf visual-studio xaml data-binding


【解决方案1】:

不确定这是否有帮助。但是每当我遇到这个问题时,我都会执行以下操作。 1.禁用项目代码并重建

  1. 如果步骤 1 不起作用,请关闭 VS。从项目中删除所有 obj、bin 文件夹。打开并重建项目。它应该可以正常工作。

【讨论】:

    【解决方案2】:

    首先,我们应该知道以下格式:

     <ObjectDataProvider x:Key="MercerCatalog" ObjectType="{x:Type local:MercerCatalogDataProvider}" MethodName="GetMercerCatalog" /> 
    

    Key - 将 Key 属性设置为 CarData。

    ObjectType - 数据提供者将根据 ObjectType 属性实例化一个对象。

    MethodName - 数据提供者将在实例化对象后调用此方法。

    以我的经验,当我定义类并使用它时,它会显示这个错误。您可以重建您的应用程序来解决此问题。

    您可以参考以下示例(Visual Studio 2019 16.2.3):

    XAML:

    <Window.Resources>
            <ObjectDataProvider x:Key="MercerCatalog" ObjectType="{x:Type local:MercerCatalogDataProvider}" MethodName="GetMercerCatalog" />  
        </Window.Resources>
         <Grid>
            <DataGrid Grid.Row="0" CanUserAddRows="False" ItemsSource="{Binding Source={StaticResource MercerCatalog}}"  >    
          </DataGrid>
        </Grid>
    
    
    
     public class MercerCatalogDataProvider
        {
            public ObservableCollection<User> ListItems { get; set; }
            public MercerCatalogDataProvider()
            {
                ListItems = new ObservableCollection<User>
                {
    
                  new User { Name = "Item1"},
                  new User { Name = "Item2"},
                  new User { Name = "Item3"},
                  new User { Name = "Item4"}
                };
            }
    
            public ObservableCollection<User> GetMercerCatalog()
            {
                return ListItems;
            }
        }
        public class User
        {
            public bool Add { get; set; } = false;  
            public string Name { get; set; }
    
        }
    

    【讨论】:

    • 很好的解释,谢谢。不幸的是,它仍然不起作用。
    • 我可以部分解决这个问题。我创建了一个新项目。添加了一个新窗口并创建了“DataProvider”类。然后重新构建项目并在 XAML 中添加绑定。幸运的是,我目前还没有参与我的项目,并且可以处理将这些东西接管到一个新项目中。即便如此,我还是想知道在 Visual Studio 这样的专业软件中怎么会这样。 WPF 的任何替代品?
    猜你喜欢
    • 1970-01-01
    • 2015-12-13
    • 1970-01-01
    • 1970-01-01
    • 2016-11-06
    • 1970-01-01
    • 2013-02-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多