0

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?

I've added a reference to the other project, and used the same namespace, but still no joy.

Thanks!

Base 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.

Edit: Upon receiving @Liju answer, I set my TreeView to use the 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. (i.e. Answer for "The component does not have a resource identified by the URI Apparently, this has been a problem since 2008.

Edit 2:

Using @Liju's second answer, I tried setting the ResourceDictionary, but it says it fails to load the resource.

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
Community
  • 1
  • 1
Bob.
  • 3,894
  • 4
  • 44
  • 76

2 Answers2

1

You can use the x:FieldModifier attribute as Public x:FieldModifier="public"

The default fieldmodifier value is 'not public'.

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

Liju
  • 91
  • 4
  • Hi, thanks for the answer, but I'm still getting an error from inheriting the XAML to another assembly. – Bob. Feb 21 '13 at 12:55
1

I assume you are getting this error at runtime. If then, load the treeview resource explicitly in the derived constructor
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!

Liju
  • 91
  • 4
  • Hi, I tried this, but it didn't seem to work. I put the ResourceDictionary in ImportedTree, and tried in App.xaml in the start-up project and now I get a different error. – Bob. Feb 21 '13 at 20:01
  • Something to watch on - you have a space in the namespace "View Tree" pack://application:,,,/myLibrary;component/Material/View Tree/ViewingTree.xaml – Liju Feb 22 '13 at 01:42
  • Yup, I know. The space is there. Not sure why it isn't working still. – Bob. Feb 22 '13 at 12:25
  • It seems like you are getting the error during the resource dictionary initialize, please see if the below code executes fine. If not the issue might with the uri you have provided. .ResourceDictionary treeResource = new ResourceDictionary { Source = new Uri(@"pack://application:,,,/<>;component/<>") }; – Liju Feb 22 '13 at 18:05
  • @Bob this seems to be an issue by design. So if the usercontrol is defined by xaml, we cannot extend the usercontrol by any means. x:FieldModifier="public". Helps you in setting access modifers on UI elements, but it will not help when you trying to access these elements thru a derived implementation. Here I suggest to try creating custom controls, following links might be helpful. http://msdn.microsoft.com/en-us/library/ms745025.aspx http://msdn.microsoft.com/en-us/magazine/cc163421.aspx – Liju Feb 22 '13 at 22:14
  • I actually posted a link in my question when I was doing some research, and its a bug from 2007 not being able to inherit XAML from a different assembly. I was really hoping you would be able to get around that. :P – Bob. Feb 23 '13 at 00:14
  • Try custom controls, if you have any question, i will try to help. – Liju Feb 23 '13 at 02:16