WPF Project with Custom XAML Controls - c#

I am new to WPF coding. I have a project that has custom controls coded as xaml's with C# code-behinds. I try to import these files into another project and when I try to use these xaml controls in my main view file, Visual Studio cannot find the namespace that the imported xaml's and C# code-behinds are attached to.
I tried changing the namespace to be the same as my current project. I tried restarting Visual Studio 2013. Neither of these worked.
I tried adding the project with the custom controls to my solution. The imported project can read the custom xaml's, but my main project still can't reference them. I tried dragging the files over to the main project, and they still won't work.
I tried rebuilding the controls xaml's and C# code-behinds in my new project it still says:
The type 'local:ClickSelectTextBox' was not found. Please verify that that you are not missing an assembly reference and that all reference assemblies have been built.
This item is under the appropriate namespace and in the current project.

In visual studio go to TOOLS -> choose Toolbox Items. this will allow you either choose from an array of different components or browse around to find a dll that you would like to use.
EDIT:
Oh so you are trying to take customized xaml files that you already have written and modify them in a separate project? I would suggest one of two things.
1: right click your project name -> add existing, and add the xaml and xaml.cs file to your project at the same time.
if for some reason that does not work you can also try
2: creating a new xaml control (with the same name of the control you want to drop in) in your project and copy pasting the xaml code into that control, this should autogenerate the codebehind designer shell you are going to need. you can then go into the xaml.cs file and drop whatever business logic you are looking for. It is not the cleanest solution but sometimes the WPF editor gets a little funky when importing xaml files

I know it's an old post but still somebody may find it useful:) So! ... Make sure
1)You don't have public class outside of root namespace, check this
link
2)You are not mixing targets of your assemblies (unless you can't 100% avoid that)
For example, if you are referencing something like SQLite.Core NuGet (that has both x86 and x64 versions of SQLite.Interop.dll built in) in a project that is AnyCPU, sometimes it's easier to set application's target to x86 or x64 to solve the x86/x64 paths issues - but then you may get all these "type not found/control not built/assembly not found" and all that sorts of nonsense from Designer even despite your app builds and runs ok.
Hope that helps

Try downloading ReSharper's trial version, install it and then open up your code again. One of the awesome features Resharper has with XAML code is that it will automatically map objects to their appropriate namespace. I think this will make it easier for you and will show you a ton of ways to do things better. When I was first learning WPF it was honestly a godsend to use Resharper.

Related

Item Templates Sometimes Missing in Visual Studio 2017

Using Visual Studio 2017, sometimes new item templates are missing, and it seems to be tied to the particular project to which I'm trying to add an item.
Although both show up as "C#" projects...
...in "Logging", for WPF items, I have only UserControl (WPF) as an option:
However, in "SanityCheck", I have a much-more-full list of WPF options:
The only difference I can easily see so far is that the one ("Logging") is a Class Library project while the other ("SanityCheck") is a Windows Application project. However, changing Logging to Windows Application doesn't make more options available, and I ought to be able to create a Window in a Class Library, right?
I also noticed that Logging didn't contain all the same references as SanityCheck, so I added all the ones that seemed possibly-related, and that also didn't make any difference in the set of templates available.
So how do I get my Logging project to let me add a WPF Window?
Thanks to a comment from ASh and the list referred to from this question, I determined that the cause was a missing <ProjectTypeGuids> block in the Logging.csproj file. I copied the one from the SanityCheck.csproj file and now all the C# WPF templates are available. So far I haven't encountered any negative side-effects to this solution. Time will tell.

Failed to load toolbox item. It will be removed from the toolbox

I have a WinForm application. I also have created my own User Control for it. Everything worked fine. Until today that I received the error message when I try to add it back to my program (I never removed it. Visual Studio did).
Failed to load toolbox item #UserControlName. It will be removed from
the toolbox.
I have it in my solution explorer but it vanishes from my toolbox after this error.
I also receive the warning that says:
'#UserControlName' is never assigned to, and will always have its
default value null.
And when I look at my designer code, that is true. But I haven't done anything with my designer code. My user control is defined there but not instantiated. How can I put it back?
Does it have anything to do with the fact that I changed my build platform from x32 to x64? If that is the case I will be surprised because the program was working fine after that change.
I was suffering from the same issue for one of our applications and could not find a solution. So I created the user and custom controls manually. After searching the web again I finally found why the designer is failing for this particular project. The answer is that project is 64 bit and Visual Studio is still does not have a 64 bit version, it is still 32 bit. So the designer could not load the controls in 64 bit applications or in class libraries. Read Microsoft's article about this.
There was an article on Visual Studio web site but it was removed. See the the answer in Visual Studio support forum.
https://social.msdn.microsoft.com/Forums/vstudio/en-US/77e10b58-43cc-4aab-919f-888f14f99571/x64-class-library-of-user-controls?forum=csharpgeneral
I had the same issue lately. As this (nor any other answer on this site and on the internet) actually helped me, I found out the way to fix it.
Just clean the files and rebuild the solution. As simple as that.
I was having same issue but I find the solution:
Click on "Project_Name" with left mouse and click "Build" then you can add the UserControl to your WinForm.
Change to AnyCPU and rebuild the project.
Visual Studio has problems with controls being 64bit.
You don't have to necessarily insert the user-control manually. I had the same situation an there was a reason for it.
In my case, it failed because the EXE assembly was compiled to 'mixed-mode' by using CLR support type /clr. After changing it to 'managed-only' by setting type /clr:pure, it worked.
For details, see my answer here at SO.
After many hours of head scratching with this problem with a large custom control with few changes from a previous working copy in source control I copied all the code into a new control name and file and it all worked.
This was to identify the problem line(s) of code since the debugger would not co-operate. The copied control (along with heaps of support code and modules) worked okay.
So these are the very simple steps that fixed my original code
In solution explorer rename the file (I just added an s at the end)
Rebuild
Test the control can now be added to a plain form
Rename the control back to its original name
Step 4 might be optional for you but if you have source control and it's a in a library you would want to do this.
This brought the control back onto all my forms that weren't working (so far as I can tell). It seems the problem is in VS that somehow remembers it doesn't like it?
Hope this helps and I hope I find this message next time I get this when I have forgotten the fix :)
P.S. Clean, rebuild and/or re-run the solution is the old stand by for this problem but this time it was just one custom control in a whole (DLL) class of them. Hope this helps.
For me, after adding the userControl, I rebuild the application first then refreshed the items on the tool box by going to projects => Refresh project toolbox items
Right click - Rebuild Solution fixed it for me!
Mostly caused by 32 bit / 64 bit architecture. Before Visual Studio 2022 the VS built in 32 bit, therefore cannot show 64 bit components.
Solution 1:
Create a new solution configuration in Configuration Manager with name: "Debug_FormDesign" or anything else.
Set the Configuration to the above name for all projects and set the platform to "AnyCPU".
Now open all projects Compile settings step by step, select the above configuration and change the Compile -> Target CPU option to AnyCPU.
Close all opened windows.
Clean solution.
Restart VS.
Select the "Debug_FormDesign" configuration in toolbar as active.
Rebuild solution.
Open the form designer -> Should work now.
You can easily switch back to the default "Debug" configuration after finish with the GUI.
Solution 2:
Use Visual Studio 2022.
At times like these, you'll be forced to tinker with the designer code. As long as you don't make drastic changes in the designer code, you shouldn't break anything. To play it safe, re-instantiate the object where the other controls are instantiated (near the top of the page). The designer should fill in the blanks for properties and such.
This should also return the control to the toolbox.
Check in your form.designer file if there is a left-behind property with that type.
It happened to me several times.
After I removed that line and rebuilt the project, everything started working.
In my case it helped to manually include the header files of the created user controls at the top of the Form where these controls are to be used.

Cannot Place User Control on Form

I've created a C# WinForms application using VS2010. I'm new to creating user controls so I created a new user control (as part of the same project).
When I rebuild the project, the new control appears in the toolbox. And when I drag the control from the toolbox onto a form, I get the following error.
Failed to load toolbox item 'TagGroup'. It will be removed from the toolbox.
This happened the only other time I created a user control as well. I've searched the web but most answers I found seemed related to having the control in a separate assembly. (Note that I found plenty of questions with the same problem I'm having.)
Can anyone suggest where I should look next?
My application need to be 64-bit. In order to use custom user controls in the designer I just added a new project to my solution. This new project use the "AnyCPU" setting and contains all my user controls.
My solution contains the following projects:
MyApp which is my main project (Windows Form Application) compiled in 64-bit and referencing my second project
MyApp.UI.UserControls (class library) is compiled for "Any CPU" and contains all my user controls
Works like a charm and it's clean
By the way, there is a Microsoft support article about that issue.
Action: You attempt to use a 64-bit component within the Microsoft Visual Studio Integrated Development Environment (IDE).
Error cause: This behavior is by design. Visual Studio is a 32-bit process, and therefore can only execute 32-bit modules. While Visual Studio allows you to add a reference to a 64-bit assembly, it cannot actually JIT compile it to 64-bit and execute it in process.
Resolution:
Rebuild the assembly using the "AnyCPU" setting. This would allow
the component to run within a 32-bit process (such as Visual
Studio), or in a 64-bit process.
Add the assembly as a reference and load the control dynamically at
run-time. Although you still would be unable to use the
control within any designer inside Visual Studio, you can still
write the code needed to instantiate the control and set it's
properties accordingly.
Source: http://support.microsoft.com/kb/963017
I finally figured this one out.
The project I'm working with uses two class-library assemblies. Although these have nothing to do with the control I'm discussing, I looked and saw both libraries have Platform Target in the Properties|Build tab set to "Any CPU".
On the other hand, my application had this setting set to "x64". By changing my application's setting to "Any CPU", I can now place my user controls onto my forms.
Go figure...
I had this problem too, but the answer couldn't fit for me. My project has some issues it can only target x86 and x64 separately. In other words, I can't use AnyCPU configuration (that's because I reference different libraries for each configuration, since those libraries are not fit to AnyCPU).
The solution I came up with was: when I need to use the form designer, I change the setting to x86. Do the job, then set back to x64 and test. The problem occurs only with the designer, but the solution builds and runs fine.
I had this problem in VS2015 and the solution turned out to be simple.
I had created a user control by cutting and pasting a few existing controls from a form (with the aim of grouping them into the custom control). The custom control was okay (no compile errors), however the removal of the controls from the existing form meant that the application wouldn't compile. Of course not being able to add the new control meant that I couldn't update the code referencing the previous controls with code referencing the custom control.
All I did was hack and slash (commenting-out, creating temporary controls, etc.) so that the entire application would compile. After it compiled I found that I could drag the custom control onto the form (without the error that prompted this question). I then had to unhack and unslash so that the code properly referenced the new custom control.
Same problem here. I am guessing it is related to the fact that the VS2010 installed on x64 OS is still a 32bit program in the heart.
An alternative solution one might want to try is simply open TheFormThisUserControlIsSupposedToBeAddedTo.Designer.cs and use code to add the user control. Basically, you are doing the dirty work that the Designer is supposed to do.
It is not as difficult as it sounds, esp. considering that there are probably plenty of sample code in that file already (e.g. the buttons you added using Designer). The only difficult part is figuring out the right coordinates in the form to place the control.
The end result is that you can not see the user control in Desinger, but they are added on debugging/running.
I also experienced this problem but the cause was different. In my case a component (form) constructor or Load event invoked a method elsewhere that used reflection to find all classes that implemented a certain interface.
While that works fine at runtime, it generated the above mentioned exception at Design Time. (Type Initialization exception with a Type Load Exception as inner exception).
An hour of confused, weary troubleshooting at 3:36AM is again easily resolved with a fresh mind the next day!
I fixed a simple spelling mistake in the control's filename, so it now matched the UserControl name, cleaned and re-built and Bobs your weird uncle's cat. :]
GO project property then 'Build' tab and check 'Target Platform' was 'AnyCPU'.

Changes are not happening until a rebuild is performed

I have a large windows application.
Recently when I make any change in the source code and run the project normally with or without debug, the changes are not made.
I tried to rebuild it, and everything goes fine,
now every change I made to the source code I have to rebuild the project then run it so that the changes are taking effect.
also I can't debug until I rebuild the solution.
my project is attached to source safe control.
any help is appreciated.
You frequently see this happen when a project gets deselected from the "Current Build Configuration" Dialog in visual studio. Try checking there to see if your project is unchecked. I think you can get to that menu by clicking the dropdown that says Debug/Release, and clicking manage/edit.
This is an old question but I happened to face a similar challenge. Well, for the benefit of someone who may experience the same here is how my problem was solved. I had a multi-project solution where some project depended on the source code from the other projects. Just before giving up and continue with the rebuild thing, I decided to check on the Build Dependencies=>Project Dependencies(Right-Click on the project). I found that the problematic code was from a Class Library that was not part of the dependencies(Unchecked). When I Included the project in the dependencies my problem vanished into thin air.
If you are referencing a .dll whose code is in another project you will need to build the project before the changes are reflected even if both projects are in same solution. sadly there is no running away from it.
Go to your configuration Manager and make sure build checkbox is selected for your project.
other thing you can try is:
Go to tools > Options>> A window will pop up> Under Project and Solutions > select Build and Run
Make sure Before building is set to Save all changes
(Copied from answer by amit dayama at https://stackoverflow.com/a/32820701/3195477)
I have faced the same problem because I have done some unsafe modifications upon the project names and have used find/replace upon the solution. Which resulted in a hidden exception in the android manifest file and exactly at the package name.
I avoid all types of headaches, I have created a new blank project with the desired name and namespaces and then moved the functional files, resource files and then modified static files like manifest and so on.
Never use unsafe method for project data configuration & modification

How to automatically convert VS2003 classes to partial Designer.cs files?

I'm upgrading a project from Visual Studio 2003 to 2008 and wish to automatically generate XXX.Designer.cs files and update the corresponding XXX.cs original one.
Any automatic way to do it?
UPDATE: I'm referring to WinForms. Of course, I know the old style works but i'm looking for a way to go to the new style without doing it by hand.
There is a macro out there floating that does this. I have used it and it works great. Start by looking at Nathan Jones Blog. If you dont have much luck then download a zip of the macro source (I posted) from VSCONVERT - PasteBin
The original classes will continue to work - even with the Visual Studio designer. The code the VS puts in the Designer classes is simply meant to remove the designer "noise" into a separate file so you can focus on the non-visual code in your class.
Once you migrate the project to VS2008 using the wizard, you can open up your forms in VS and continue to edit them just like you did before. Any new forms that you create will get Designer backer files.
We had this problem.
Basically the designer file should contain nothing but a set of object declarations (i.e. the objects on the form) and the InitializeComponent() call. If you've done any fancy work on your constructor that might be there too.
Simply create a file called MyForm.Designer.cs and add it to the project. VS2008 will automatically place it underneath MyForm.cs. From there, if you create the partial class heading and the namespace, then copy across all the declarations and Initializecomponent call into the designer file, then it will work.
But because if you had to, people would edit initializecomponent and also edit the default constructor, VS2005 and 2008 couldn't automatically upgrade these files as all sorts of strange things might occur. I guess from a project point of view, what benefit do you think your working code will derive from the separation?
I'm not sure I understand, but I'll make a guess.
Was this an ASP.NET project in VS2003? Did it have .Designer.cs files in it? Is it the case that you've done an upgrade but you find that you have no .Designer.cs files now?
If so - you may have inadvertently changed your Web Application Project into a Web Site "project". This sort of "project" was introduced in VS2005, so you would not have seen it yet. These aren't real projects (no .csproj file), and they have many other differences.
Otherwise, if your project didn't used to have .Designer.cs files, then why do you want them now?
In general, the way to create designer files is to open the file in a designer, wait a few moments for the designer to finish what it's doing (updating the screen, etc); and then saving the file. You might possibly have to "touch" the original in order to get the designer to reevaluate things.

Categories

Resources