I'm having trouble getting the xaml portion of the VB version of a working C# WPF project to work.
In the top section of the VB project xaml file:
xmlns:resources="clr-namespace:My.Resources"
The first error message for the xaml file is:
"Undefined CLR namespace. The 'clr-namespace' URI refers to a namespace 'My.Resources' that could not be found."
A typical reference to 'resources' within the xaml file:
<TextBlock FontSize="32" Text="{x:Static resources:Resources.Foo_ProjectName}" Style="{StaticResource HeaderTextBlock}" />
The VB project has no "root namespace". If I add a root namespace 'Foo' and change the above directive to
the following, it makes no difference - same message about the undefined CLR namespace:
xmlns:resources="clr-namespace:Foo.My.Resources"
Overview of the Resources.Designer.vb file (auto-generated so I have no control over it):
Namespace My.Resources
...
Public Module Resources
...
Public ReadOnly Property Foo_ProjectName() As String
Get
Return ResourceManager.GetString("Foo_ProjectName", resourceCulture)
End Get
End Property
...
End Module
End Namespace
The working C# version of this project:
xaml file:
xmlns:resources="clr-namespace:Foo.Properties"
Partial code from the Resources.Designer.cs file
namespace Foo.Properties {
public class Resources {
...
public static string Foo_ProjectName {
get {
return ResourceManager.GetString("Foo_ProjectName", resourceCulture);
}
}
...
}
}
I don't think resources in My.Resources are compatible with reference from WPF xaml based on some testing. It seems like the problem is that the code generator used for it puts the strongly-typed references to the resources into a Module (equivalent to a static class in C#) rather than a Class (equivalent to a class in C#), and the xaml compiler only supports the latter. This seems to be a function of the use of a special "VbMyResourcesResXFileCodeGenerator" for My.Resources compared with a "ResXFileCodeGenerator" for any other .resx file. I suspect this is a difference in behavior for the default resources in a C# project compared with the My.Resources in a VB project.
The workaround to this is to not use My.Resources for the purpose. Instead, use a custom .resx file in the project. You will then be able to refer to the resources from xaml as you would expect.
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.
for example:
I have:
public class MyPage : XXXPage
{
.....
public class HahaConverter: IValueConverter
{
.........
....
}
}
In my xaml, can I do this:
<Page.Resources>
<????:HahaConverter :Key="dateConverter" />
<Page.Resources>
I just wondering how to get ????. I could not reference myself?
Thanks
Nested classes will require a fully qualified reference, so you will need to add another xmlns entry to the xaml referencing your parent class namespace. e.g.
xmlns:ValueConverters="clr-namespace:YourAppName.MyPage;assembly=YourAppName"
In VS 2010 once you start typing auto-complete/intellisense should start listing your project namespaces*.
Then reference your value converter with
<ValueConverters:HahaConverter x:Key="dateConverter">
*Note: It is usually recomended to stick to the one-class one-file standard as tools, coders (and VS) cope better. You will likely wind up with a large collection of Value Converters for Silverlight, might as well start a library for them :)
Wondering how to accomplish setting the Style xaml with the code in F#. The code is simple enough:
this.DefaultStyleKey <- typeof<MyControl>
In a C# project the build options allow you to mark the XAML as a resource custom build command of: MSBuild:Compile
I don't see it in the properties panel, so I tried to add it by hand to the project file myself...
Any ideas? The application loads - the custom control has no output (but the code executes).
Thanks
UPDATE:
I checked the manifests and the resource was included as expected between my project and the project I am porting... Looking for a next step.
UPDATE 2:
Well it may be included in the manifest OK - but it is not being "compiled" as the C# version of the project throws an error in the build process when I malform the XML while the F# version allows the malformed XML to be brought into the application.
UPDATE 3:
Loading the XAML is fine now (i guess) however I am having some issues with the properties of the control:
static member ItemsProperty : DependencyProperty =
DependencyProperty.Register(
"Items",
typeof<MyMenuItemCollection>,
typeof<MyMenu>,
null);
member this.Items
with get () : MyMenuItemCollection = this.GetValue(MyMenu.ItemsProperty) :?> MyMenuItemCollection
and set (value: MyMenuItemCollection) = this.SetValue(MyMenu.ItemsProperty, value);
The problem occurs on access:
for menuItem in this.Items do
let contentElement: FrameworkElement = menuItem.Content
where I get a null pointer exception on this.Items; however I have it initialized in the constructor:
do
this.Items <- new CoolMenuItemCollection()
The C# style of compilation of XAML files is not supported by the F# tools for Visual Studio, so there is no way to get the same behavior as in C#. I think you have two options:
Create a C# project with XAML files and reference F# library which implements the core functionality (or reference C# library from F# and load user interface from the C# library in your F# application)
Use XamlReader object (see MSDN) and load the XAML file (embedded in resources in the simple way) programmatically. You won't get any of the C#-compiler generated features (e.g. named properties for all objects with x:Name), but otherwise, it should work in the usual way.
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"
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.