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.
Related
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! :)
Currently I add an image in XAML this way :
Put the file in the project's folder, in a sub-directory named "Resources",
Set its "Build Action" property to "Resource",
Add in my XAML file : <Image Source="/Resources/myImage.png />
And it works great.
But in SO I keep seeing people writing this instead :
<Image Source="pack://application:,,,/MyApplicationNamespace;component/Resources/myImage.jpg" />
Also recently, I've found that in Project Properties -> Resources, you can add files like images, texts...
So which of these 3 possibilities should I use ?
The Image Source="/Resources/myImage.png syntax used in your XAML to refer to an image in a sub folder is actually equivalent to the following syntax pack://application:,,,/Resources/myImage.jpg and this is one of the variations used to access binary resources in XAML.
Since the first two options are the same written differently this leaves us with the third option.
When to use the resource file ?
I usually tend to use them when i have different resources assemblies that are used as satellite assemblies which are in turn used for localization to different cultures. Also they can be used when you want to access those resources and switch them at run-time.
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>
I have a listview(lstViewOwner) which should display the folder and its files.
I select the path by browsing.. once I give the path to a textbox, based on the path, it should open the folders and its files in a hierarchial way.. I mean
Folder1
Excel1.xls
Excel2.xls
Excel3.xls
Folder2
Excel1.xls
Excel2.xls
Actually my question is 2 fold.
1. How to create a hierarchial way of displaying in the ListView(If i select the folder up one level.. it should disply the children)
2. How to open the files by clicking on it in the ListView.
This is the WPF Application and using C#.
Please help me
Thanks
Ramm
ListView doesn't lend itself to representing hierarchical data.
You need to either:
process you hierarchy first, presenting it to ListView as a flattened collection (perhaps with the name or the tag holding its place in the hierarchy).
use a hierarchy aware control, such as TreeView. You could make the TreeView look like a list-view if you wanted.
For launching, assuming you want to run the associated application, you need to put a clicked/double-clicked handler for the control, this in turn should call ShellExecute with the filename selected.
I'm learning WPF.
I want to provide my own Main method in my App.xaml.cs rather than getting one generated for me in App.g.cs. However I keep getting conflicts because I haven't found out how to stop an additional Main from being generated.
Is there a setting in my project file or elsewhere that controls this?
I found the answer here. http://learnwpf.com/post/2007/12/13/How-can-I-provide-my-own-Main%28%29-method-in-my-WPF-application.aspx
It is:
The way WPF knows to create the Main() method for a particular xaml file is through the build action property set for App.xaml - it has a build action of ApplicationDefinition. By changing this to Page WPF won't create the Main method and you can provide your own in a regular class file you add to the project.
However in the comments to the above blog, a comment notes there may be issues with blend and it references: http://blogs.msdn.com/expression/archive/2008/04/09/creating-a-wpf-blend-project-that-loads-resources-in-code.aspx . I don't fully understand the issues yet.
You can also just create a separate class (for example, Entry) which is responsible for bootstrapping your application. Then go to project settings and set your startup object to Entry. That way you don't even have to disable the autogenerated method.
The easiest way is to set the Build Action in the Properties window from ApplicationDefinition to Page for App.Xaml.
Then you can define your own entry point.
I found a solution:
Copy the data from your app.xaml file
Delete app.xaml file and re-create with the same name
Create `main` method in .cs file, and paste your old copied code into it
One way is to forgo defining an Application-derived class in XAML, so you can manually define the Main method with your custom requirement
The Easy way just create a class like Startup.cs with build action to compile
and remove ApplicationDefinition from App.xaml convert that to page
and remove it from any other file in the application