I've been trying to get my head around XAML and it's namespaces and I just cannot seem to get the logics of things. For example, I'm trying to animate a gridrow's GridLength property following a tutorial. I can get the sample code to run just fine. But trying to actually port the features into my own code seems to be impossible. I always run into odd errors which doesn't occur in the source code. Animating the GridLength property isn't possible, so I need to create a new .vb file containing the code making it possible.
But trying to actually reach what's in there I can't get to work.
In my Window.xaml file I have this at the top:
xmlns:gridanim="clr-namespace:HDI_WPF_GridAnimation_vb"
Which targets a GridLengthAnimation.vb file. In that file you have this:
Imports System.Windows.Media.Animation
Imports System.Windows
Public Class GridLengthAnimation
Inherits AnimationTimeline
Back in the Window.xaml, getting contact with "GridLengthAnimation" is impossible:
<gridanim:GridLengthAnimation></gridanim:GridLengthAnimation>
I get an error message saying:
The name "GridLengthAnimation" does not exist in the namespace "clr-namespace:HDI_WPF_GridAnimation_vb".
What is causing the issue?
I dont see your nameSpace declaration around your class
Imports System.Windows.Media.Animation
Imports System.Windows
Namespace HDI_WPF_GridAnimation_vb
Public Class GridLengthAnimation
Inherits AnimationTimeline
If the HDI_WPF_GridAnimation_vb namespace is not in the same assembly as Window.xaml, you need to reference the assembly by adding it like this :
xmlns:gridanim="clr-namespace:HDI_WPF_GridAnimation_vb;assembly=asmName"
Related
Why do I have this error message for a UserControl:
Partial declaration of
MyNamespace.MyUserControl must not
specify different base classes
Just because I inherited from another UserControl class I created in another namespace whereas this other namespace is referenced in the XAML as
xmlns:my="clr-namespace:ReferedNamespace;assembly=ReferedNamespace"
Little to go on here, but this usually happens when the code behind and the xaml file do not inherit from the same base class.
Since we do not have all the details concerning your problem,
I'll create a situation that will cause the same exception to be thrown,
this might help you understand your problem.
As an example, just create new WPF application using Visual Studio,
The XAML might look something like this:
<Window x:Class="WpfApplication1.MainWindow" .....>
The code behind would then contain something like this:
public partial class MainWindow : Window
{
//Code here
}
Note the 'partial' modifier here. It means this class (MainWindow) might
not be defined in a single file but spread out across multiple files,
in this case the XAML (.xaml.cs) and the CS (.cs) files.
Now add a new UserControl to the solution. It will be named UserControl1.
Without making any changes to the XAML, change the code behind for the MainWindow:
public partial class MainWindow : UserControl1
{
//Code here
}
Now you'll get the exception you questioned about.
Look for something like this in your code, if you still can't find a solution,
please provide more code.
look to both your .cs and .xaml files at this parts
in .xaml file :
<Window x:Class="BUSsAsDesign.GUI.IGPopUP" > ...... </Window>
in .cs file :
namespace BUSsAsDesign.GUI
{
public partial class IGPopUP : Window
{
//code here
}
}
Now if u wanna change Window to UserControl
change
<Window x:Class="BUSsAsDesign.GUI.IGPopUP" > ....... </Window>
<!--**becomes**-->
<UserControl x:Class="BUSsAsDesign.GUI.IGPopUP" > ....... </UserControl>
namespace BUSsAsDesign.GUI
{
public partial class IGPopUP : Window
{
//code here
}
}
//**becomes**
namespace BUSsAsDesign.GUI
{
public partial class IGPopUP : UserControl
{
//code here
}
}
- i hope it`s useful :) .
I wanted to add this piece of information. In Visual Studio 2017, There is a bug which I haven't had time to dig into with much depth, but it causes this error. (from the OP)
Overview: When declaring a partial class as an x:Class for a resource xaml file, it will cause this error, and after removing the declaration, the error will persist.
To reproduce this bug, (or if you are here because you are getting this error message)
Step 1: Create a new WPF Project.
Step 2: Create a new Resource File
Step 3: Change the resource files name-space to the projects main namespace (remove the bit at the end.)
Step 4: In the resource files declaration/header section, add an x:Class declaration specifying your MainWindow (default startup window object) as the Class for that resource file. (EG: x:Class=MainWindow)
Step 5: Clean/Rebuild Solution
Step 6: You should now be getting this error from the OP. So remove the x:Class Declaration from your header on the Resource File, and save/clean/rebuild the project.
If the bug was reproduced, the error should remain, even though you have removed the x:Class declaration from the resource file.
The only solution I could find for this bug, is to delete the projects Bin folder, and any other folder with builds in it.
After deleting them, Clean/Rebuild your project, and the error should be gone.
I add a TextBlock to the MainWindow in XAML. And I would need to change the TextBlock Text in a separate class resided in a separate .cs file. I tried the following:
private static fooNameSpace.MainWindow tW1;
tW1 = this;
tW1.textBlock1.Text = "This is a paragraph";
It worked if the class is reside in the same file as the MainWindow class, But it throws me an null exception if the class is reside in a separate file. I have already added the using fooNameSpace; Still doesn't work
I can't figure out the right way to make a reference from a separate file class to the MainWindow and it's Control. Tips anyone?
thanks,
To answer my question - use internal instead of public.
// in MainWindow.xaml.cs internal
internal static fooNameSpace.MainWindow tW1;
// in foo.cs
MainWindow.tW1.txtBlock1.Text = "This is a paragraph";
the internal keyword allows other class in other cs file to get access to MainWindow controls.
But I'm not so sure about using internal to solve this problem as it allow my other class to get access to everything else in my MainWindow...any better option out there?
You mentioned XAML, so I will assume you are talking about a WPF application. the .xaml and .xaml.cs files go hand in hand. If you need to access anything in that "control" you will need to instantiate it or need its reference in the outside class.
As for the error, you declare the tw1 but it is not instantiated - which is the reason you are getting a Null exception error. Doing tw1 = this is also won't work.
I have read multiple posts on the subject but still cannot manage to make it work.
I want 2 user controls slidertype1 and slidertype2 which should inherit from slidercommontype, all are in same namespacecommon, can someone knows the syntax for this simple use case ?
Inspiring from http://jamescrisp.org/2008/05/26/wpf-control-inheritance-with-generics/
I tried:
<namespacecommon:slidercommontype x:Class="namespacecommon.slidertype1">
but I got namespacecommon:slidercommontyp doesn't exist in xml namespace.
As long as the base class doesn't have a XAML file associated with it, it's pretty easy. Trying to incorporate the visual aspect of the user control using XAML is not really a supported scenario.
Having said that, just create your class SliderCommonType (although I would call it SliderBase or something.)
namespace MyControls {
public class SliderBase : UserControl {
}
}
Then create your two controls based on it. I'll show one example and the other should be obvious.
<Local:SliderBase x:Class="MyControls.SliderType1"
xmlns:Local="clr-namespace:MyControls">
</Local:SliderBase>
And the code-behind would look like this:
namespace MyControls {
public class SliderType1 : SliderBase {
}
}
The key point being that your XAML file has to reference the base class which requires changing the <UserControl> element to <Local:SliderBase> which in turn requires a XAML namespace import.
When you add a UserControl using the default template, you can just change the code it creates to reflect the above changes. It's much easier than trying to create it from scratch.
One last thing to note - you will need your application to compile successfully before you can use the visual designer on your derived controls. This is because the designer needs to be able to instantiate SliderBase at design-time.
I am using the NavigationFramework in Silverlight 3, and am running into issues where the constructor of the UserControl in the xaml I am loading is not being called, and I believe this is because the UserControl in the xaml I am calling is actually derived from another user control.
I have stepped through the debugger with specific break points and the constructor is being ignored completey.
I have MyWindowBlue which is of type uctrlBaseMyWindow.
The constructor for uctrlBaseMyWindow is being called when the xaml is 'navigated to' but the constructor for MyWindowBlue is being ignored.
This is not the case if I add the user control via markup directly.
Anyone else have this issue?
The code I am using to navigate to the MyWindowBlue is
this.MyContentFrame.Navigate(new Uri("/Controls/uctrlMyWindowBlue.xaml", UriKind.Relative));
Has anyone run into this or could offer any help?
Thanks
Found the error in my code.
This was due to an error in my XAML. I had moved the user control to a different folder, and so the c# code behind had a type of the same name in one location in the namespace, but the XAML markup had the type in a different location (the original folder), and so the partial class with the constructor was not linked to the type I was actually instantiating.
I fixed the type reference to point to the proper location in the namespace and now the partial class code behind is linked back up.
When I modify the xaml's cs's I will have to go in and manually modify the corresponding *.g.cs file. And it seems to get overwritten every time I rebuild.
So my question is, what is the proper way to change the namespace on a WPF application that has been generated by the WPF project template?
Since the .g.cs files are generated from the .xaml files, besides changing the namespace in the .xaml.cs files, you also have to change the namespace in the .xaml files.
For example, the main window in one of my projects is declared like this in mainwindow.xaml:
<Window x:Class="Penov.Playground.MainWindow">
The corresponding mainwindow.xaml.cs file contains:
namespace Penov.Playground
{
public class MainWindow
}
If I want to change the namespace from Penov.Playground, I need to change it in both places. This would result in a .g.cs files generated on the next build with the new namespace.