Note that there are some explanatory texts on larger screens.

plurals
  1. POTreeView from parent XAML class does not exist in "current context" when in different project but same namespace
    text
    copied!<p>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". However, the other child classes that exist, which are located inside the same project do not have the same error. Why is this?</p> <p>I've added a reference to the other project, and used the same namespace, but still no joy.</p> <p>Thanks!</p> <p>Base XAML:</p> <pre><code>&lt;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" &gt; &lt;DockPanel&gt; &lt;TreeView Name="ViewTree" /&gt; &lt;/DockPanel&gt; &lt;/UserControl&gt; </code></pre> <p>Parent code-behind:</p> <pre><code>namespace myLibrary.Material.ViewTreeSpace { public partial class ViewingTree : Usercontrol { public string TreeName = String.Empty; public ViewingTree(){ } } </code></pre> <p>Child class in different project:</p> <pre><code>namespace myLibrary.Material.ViewTreeSpace { public class ImportedTree : ViewingTree { public ImportedTree() : base() { ViewTree.Items.Clear(); // &lt;- This line gives me error TreeName = "My imported Tree in another project."; // &lt;- This works } } </code></pre> <p>Edit: I've also noticed that variables that exist in code-behind can be referenced, only UIElements from the cannot.</p> <p>Edit: Upon receiving @Liju answer, I set my TreeView to use the <code>x:FieldModifier="public"</code>. However, I get the below error now.</p> <pre><code>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 </code></pre> <p>From what I could find, it isn't possible to inherit a XAML class from a different assembly. (i.e. <a href="https://stackoverflow.com/a/10647528/1466627">Answer for "The component does not have a resource identified by the URI</a> Apparently, this has been a <a href="http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/e92390eb-bbfa-42fb-baa9-2286444c0dca" rel="nofollow noreferrer">problem since 2008</a>.</p> <p>Edit 2:</p> <p>Using @Liju's second answer, I tried setting the ResourceDictionary, but it says it fails to load the resource.</p> <pre><code>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 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload