Remove unused references from all XAML files in a project? - wpf

I am using Visual Studio 2019 Professional with Resharper. Right clicking on a project and selecting the option "Analyze and Code Cleanup" and then selecting "Run Code Cleanup (Profile 1)" does the job good but it does not touch XAML files in the project.
The same goes for the option "Refactor | Remove Unused References" in the context menu when right clicking a project. The unused references inside XAML are not removed.
Is there a way to also remove unused references from XAML files in the selected project ?
Executing the following shortcut Strg+R,G inside a XAML file does sort and remove unused references, but how can I execute this shortcut an all XAML files inside a Project/Solution ?
Resharper is shown below, but it does not remove unused references inside XAML files.

Related

Why would a .g.vb file for a WPF form not be generated in Visual Studio 2019?

I have a form in a WPF application that is generating an error message:
"BC30451: 'InitializeComponent()'is not declared. It may be inaccessible due to its protection level"
InitializeComponent() is a function that is generated in the .G.VB file associated with a XAML file. If the .G.VB is not generated, there is no InitialzeComponent() function.
The steps that I have taken thus far are:
Compare the properties of the XAML file that is not generating a .G.VB to the properties of a XAML file that is producing a .G.VB file.
I tried a Build | Clean Solution; followed by a Build | Rebuild Solution.
Made sure that the namespace and class are the same in the VB and XAML x:Class.
Closed and re-started Visual Studio 2019.
Added a InitializeComponent() function to the class.
This will need to be documented so we know that this is typically automatically generated. Doing this leaves the code vulnerable to future changes made in the designer.
The properties for the XAML file are set as:
Build Action: Page
Copy to Output Directory: Do not copyist item
Custom Tool: XamlIntelliSenseFileGenerator
My questions are:
What prevents the .G.VB file from being automatically generated?
What is the best resource to diagnose the cause of this issue?
Additional Information
I added a partial class with an InitializeComponent() method. This function calls LoadComponent( uriLocator ) that loads the XAML referenced by the uriLocator string. This addresses the undefined InitializeComponent()... error in the short-term to get further information to identify the root cause.
Note that adding your own InitializeComponent() should not be seen as a solution. It should only be used as a stepping-stone to find out why the .G.CS or .G.VB files are not being generated. The problem is that a custom G.CS or G.VB must be updated manually with any change to the XAML.

Image path for ribbon application item

I got little problem with my image path for ribonn aplicaton menu item.
I got this piece of code:
<ribbon:RibbonApplicationMenuItem x:Name="MenuItemLogout" Header="Odhásit"
ImageSource="/Resources/logoutSmall.png"
Command="{Binding RelogUserCommand}"/>
The thing i want is: the right path of image. Because in this case, Visual Studio cant find that path. This are two different project. The one where i using this ribbon is called Z05.WPF.WeighingSystem in folder 01_LoadingPlace/ViewAnnouncedVehicles - in this folder i have particular xaml and view model.
And i have another project called Z05.DesktopApplication where i have folder called Resources and there i have all of my images. I also tried to use this path:
pack://application:,,,/Resources/
But its also doesnt work.
Any tips? Thanks! :)

Adding existing user control to WPF project

When I add an existing user control to a project, I go to "Add existing item" and then choose both the .xaml and the .xaml.cs files for the user control.
After adding, the .cs file seems to be dissociated from the .xaml file, although everything builds properly.
Is there a better way to add existing user controls, since the user control actually consists of two files (the .xaml view and the .xaml.cs code behind)?
you have to check your namespaces . i think the problem is that . after changing xaml namespace go to cs and change the name space and after that go to InitializeComponent() method definition and change its namespace too .
hope it work
I'm in VS2019 and come across the same problem - trying to reuse multiple User Controls from another project. Here is some finding/workaround -
After right click and select "Add -> Existing Item..." Select one
.xaml file (of your User Control), and do not include .cs files.
Click Add and both the .xaml and .cs file will be added, and in the
nicely associated way.
If multiple User Control .xaml files are added at the same time -
with or without the .cs files - this does not work. The .cs files won't
be associated with .xaml files.
Not any clean solution but hope this helps a bit.

XAML don´t know the Resource folder

I tried to set up a Icon in XAML from the Resources.resx but it cant find the resources.
Code:
....
xmlns:resx="clr-namespace:Admin_Overwatch.Properties"
Title="MainWindow" Height="400" Width="600" Icon="{x:Static
resx:Resources.TitelLogomRand1}">
Error:
"The name "Resources" doesen´t exist in the namespace...."
The courious thing is that in the autoformat it finds every icon in the Resources file. I have Rebuild it without any success and tried a new Resource folder also with no success.
Why doesn´t it find the Folder(s) ?
Edit:
I tried this tutorial also without any success, I got the same error, it can´t find the resources....
http://social.technet.microsoft.com/wiki/contents/articles/22420.binding-to-resources-resx-files-in-xaml.aspx
The Answer was to use the Assembly in addition to the normal clr:
xmlns:resx="clr-namespace:Admin_Overwatch.Properties;assembly=Admin-Overwatch"
xmlns:local="clr-namespace:Admin_Overwatch;assembly=Admin-Overwatch"
The Assembly name can be found by
Right Click on the Project name under Properties --> Application.
But the important part is that it is not possible to load the pictures from the resx in wpf that goes only in win forms.
see here:
How to use Resources.resx to link images
I had simmilar problem. Added a placeholder image to use as default ImageSource in XAML on startup. I created it trough Project+Right-Click->Properties>Resources>Add Resource>New Image etc.
Then got to Resource folder in the project, selected the image and down bellow in properties I just selected Build Action to Resource. And now it works!

Remove an old namespace from a g.cs File?

I previously had a subfolder in my WPF application project called "Controls". It contained a WPF user control. I decided to move that user control to my "Views" folder. Since the original folder was empty, I decided to delete it from the project.
Because the user control and folder is removed I receive a compilation error because the user control used the ProjectName.Folder namespace and now nothing references it. MainWindow.g.cs is what references ProjectName.Controls in a using statement.
I know that *.g.cs are generated by VS and can't be edited because it will be overwritten. What do I do to not allow that namespace to be written to the g.cs file? I tried cleaning my solution/project and rebuilding but nothing has worked.
I had a local reference to the Controls namespace in my Xaml code (MainWindow.xaml). I removed the reference, cleaned the project and produced a successful build.
In your user control file,
In your ClassName.xaml, you must change the namespace as shown below
<UserControl
x:Class="YourOldNamespace.ClassName"
...
...
/>
And in your ClassName.xaml.cs, you must change the namespace as shown below
using System;
using System.Windows;
namespace YourOldNamespace{
public class ClassName{ ....
}
In both the files, you must replace YourOldNamespace to some new namespace as needed.
I have had problems with g.cs files in my projects before too. Since they are auto generated, I tend to just delete the file manually and rebuild.
Dont forget too, that you must check to see if the Build Action property when you click on the affected XAML file is set to PAGE (instead of resource). This is useful to know when you copy a XAML from another project using copy-paste to save time.
Also look at App.xaml and all of your resource dictionaries. For whatever reason, VS 2012's replace in files / "Entire Solution" option didn't find the old namespace reference in App.xaml, had to manually change that. Fixed it for me.
Don't forget to change your Generic.xaml file too,
<ResourceDictionary
xmlns:local="clr-namespace:MyOldNameSpace">
</ResourceDictionary>

Resources