简体   繁体   English

来自父XAML类的TreeView在不同的项目中但在相同的命名空间中不存在于“当前上下文”中

[英]TreeView from parent XAML class does not exist in “current context” when in different project but same namespace

So, I created a child class based off a base XAML (see below), but I created the child class in another project and Visual Studio gives me an error on the TreeView saying "The name "ViewTree" does not exist in the current context". 所以,我创建了一个基于XAML基础的子类(见下文),但我在另一个项目中创建了子类,Visual Studio在TreeView上给出了一个错误,说“当前上下文中不存在名称”ViewTree“ ”。 However, the other child classes that exist, which are located inside the same project do not have the same error. 但是,存在于同一项目内的其他子类没有相同的错误。 Why is this? 为什么是这样?

I've added a reference to the other project, and used the same namespace, but still no joy. 我添加了对其他项目的引用,并使用了相同的命名空间,但仍然没有乐趣。

Thanks! 谢谢!

Base XAML: 基础XAML:

<UserControl x:Class="myLibrary.Material.ViewTreeSpace.ViewingTree"
             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"
             mc:Ignorable="d" Background="#00000000" >
    <DockPanel>
        <TreeView Name="ViewTree" />
    </DockPanel>
</UserControl>

Parent code-behind: 父代码隐藏:

namespace myLibrary.Material.ViewTreeSpace {
    public partial class ViewingTree : Usercontrol {
        public string TreeName = String.Empty;
        public ViewingTree(){ }
}

Child class in different project: 不同项目的子课:

namespace myLibrary.Material.ViewTreeSpace {
    public class ImportedTree : ViewingTree {
        public ImportedTree() : base() {
            ViewTree.Items.Clear(); // <- This line gives me error
            TreeName = "My imported Tree in another project."; // <- This works
        }
}

Edit: I've also noticed that variables that exist in code-behind can be referenced, only UIElements from the cannot. 编辑:我也注意到代码隐藏中存在的变量可以被引用,只有来自的UIElements不能。

Edit: Upon receiving @Liju answer, I set my TreeView to use the x:FieldModifier="public" . 编辑:收到@Liju答案后,我将TreeView设置为使用x:FieldModifier="public" However, I get the below error now. 但是,我现在得到以下错误。

System.Exception: The component 'myLibrary.Material.ViewTreeSpace.ImportedTree' does not have a resource identified by the URI '/myLibrary;component/material/view%20tree/viewingtree.xaml'.
at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
at myLibrary.Material.ViewTreeSpace.ViewingTree.InitializeComponent()
at myLibrary.Material.ViewTreeSpace.ViewingTree..ctor()
at myLibrary.Material.ViewTreeSpace.ImportedTree..ctor() in c:\..\ImportedTree.cs:line 51

From what I could find, it isn't possible to inherit a XAML class from a different assembly. 根据我的发现,不可能从不同的程序集继承XAML类。 (ie Answer for "The component does not have a resource identified by the URI Apparently, this has been a problem since 2008 . (即答案“组件没有URI标识的资源显然,这是自2008年以来的一个问题

Edit 2: 编辑2:

Using @Liju's second answer, I tried setting the ResourceDictionary, but it says it fails to load the resource. 使用@ Liju的第二个答案,我尝试设置ResourceDictionary,但它说它无法加载资源。

System.InvalidOperationException: ResourceDictionary LoadFrom operation failed with URI 'pack://application:,,,/myLibrary;component/Material/View Tree/ViewingTree.xaml'.
at System.Windows.ResourceDictionary.set_Source(Uri value)
at myLibrary.Material.ViewTreeSpace.ImportedTree..ctor() in c:\..\ImportedTree.cs:line 59

You can use the x:FieldModifier attribute as Public x:FieldModifier="public" 您可以将x:FieldModifier属性用作Public x:FieldModifier =“public”

The default fieldmodifier value is 'not public'. 默认的fieldmodifier值为'not public'。

http://msdn.microsoft.com/en-us/library/vstudio/aa970905(v=vs.90).aspx http://msdn.microsoft.com/en-us/library/vstudio/aa970905(v=vs.90).aspx

I assume you are getting this error at runtime. 我假设您在运行时收到此错误。 If then, load the treeview resource explicitly in the derived constructor 如果是,则在派生的构造函数中显式加载treeview资源
Sample code: 示例代码:

    ResourceDictionary treeResource = new ResourceDictionary
    {
        Source = new Uri(@"pack://application:,,,/<<AssemblyName>>;component/<<Path to the xaml to load>>")
};
    this.Resources.MergedDictionaries.Add(treeResource);

Hope this helps! 希望这可以帮助!

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

相关问题 命名空间中不存在XAML类 - XAML class does not exist in namespace 项目编译正常时,XAML中的“在命名空间中不存在”错误 - “Does not exist in the namespace” errors in XAML when project compiles fine 当前上下文中不存在该类 - Class does not exist in current context 出现错误:“名称 InitializeComponent 在当前上下文中不存在”,在每个 xaml.cs class(Xamarin Forms)中 - Getting error: "The name InitializeComponent does not exist in the current context", in every xaml.cs class (Xamarin Forms) 初始化类成员时,名称XXX在当前上下文中不存在 - The name XXX does not exist in the current context when initializing class member 创建新的XAML文件时,当前上下文中不存在名称“ InitializeComponent” - The name 'InitializeComponent' does not exist in the current context when creating new xaml file 使用相同方法,名称“在当前上下文中不存在” - The name '' does not exist in the current context" in the same method 在当前上下文中不存在类属性分配? - Class property assignment does not exist in current context? 当前上下文中不存在名称“类” - The name "class" does not exist in the current context 类型或名称空间名称“ K”在当前上下文中不存在 - The type or namespace name 'K' does not exist in the current context
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM