Changing namespace of the WPF Project Template - wpf

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.

Related

VB WPF xaml file referencing My.Resources

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.

Convert UserControl to ResourceDictionary

I'd like to try out this globalization thing in WPF:
http://www.codeproject.com/Articles/515960/Globalization-in-WPF-using-ResourceDictionary
Unforunately the second step got me already it says: convert UserControl to ResourceDictionary
How can I make this happen?
Thanks!
First you create a simple UserControl and name it however you like.
Then you open the .xaml file and change <UserControl to <ResourceDictionary.
Then you have to open de .xaml.cs file of the UserControl and add the two attributes (the ones between brackets []) to the class:
[ExportMetadata("Culture", "en-US")]
[Export(typeof(ResourceDictionary))]
public partial class EnglishLanguage : ResourceDictionary
{
public EnglishLanguage()
{
InitializeComponent();
}
}
And add this using reference to the list of usings:
using System.ComponentModel.Composition;
The compiler will notify you that ExportMetadata, Export and the using reference could not be resolved. That's because you need to add the reference to the composition DLL to your project manually.
In the solution explorer under you project, right click References and click Add reference. A list of available DLL files will pop up under Assembly - Framework.
Check the box for System.ComponentModel.Composition, click OK and build your project.
Now you're all set ;)

inheriting user control from a base class

I've done lots of searching and found lots of answers but for some reason it's not working for me. I have a VB app in WPF. I want some common code for user controls.
So I make a base class like this:
Public Class cU
Inherits UserControl
Public Value As Double
End Class
And a user control, the Xaml starts like this:
<UserControl x:Class="UserControl3"
So all I have to do, as far as I can see, is to change this to:
<local:cU x:Class="UserControl3"
But although no error is shown in the Xaml window, I get and error in the error list:
"local" is an undeclared prefix. Line 1. position 2.' XML is not valid
(by the way, when I did exactly this in winrt it worked fine)
I tried changing it to local to Controls and also putting cU in a namespace called local but it doesn't change.
Add your namespace in your XAML for your window tag or control tag (depends whichever you are using).
<Window x:Class="YourNamespace.YourClass"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:YourNamespace">
then you can use the <local:UserControl3 /> tag
the YourNamespace in your case would be WpfApplication1

Where do XAML files that build as "Page" go?

I've added a XAML file to a Windows Phone 8 project. Its build action is "Page". I want to load the XAML as a text string (to feed into XamlReader.Load()). How can I accomplish this?
It's not available as a separate file in the XAP package; it's probably somewhere in the DLL.
When set to Page, the compiler will compile the XAML into BAML and add the BAML file as a resource to the assembly.
If you wish to get the original XAML back out from the BAML resource at runtime, then you will need to deserialize the BAML, and then serialize your object to XAML.
You can have a look at the Baml2006Reader, or a better option would be to use Application.LoadComponent which is what the InitializeComponent method uses internally. InitializeComponent is called by the partially generated class for your XAML code behind.
var uri = new Uri("/MyAppName;component/MyXaml.xaml", //Note extension: XAML, not BAML
UriKind.Relative);
Page rootObject = new Page(); //Assuming XAML root element is Page - it could be anything
Application.LoadComponent(rootObject, uri);
(assuming the root element of your XAML file is a Page).
You can then serialize the Page to a XAML string using the XamlWriter:
string xaml = XamlWriter.Save(rootObject);
Note that this is the XamlWriter in the System.Windows.Markup namespace, not System.Xaml. If your XAML has WPF types, then you should use this XamlWriter to avoid errors.

WPF error message: Partial declaration must not specify different base classes

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.

Resources