VS2012 - Add WPF existing user control to project - wpf

This seems like it should be pretty simple but I can't seem to make it happen. Lets say I have an existing project with a user control named uc1. I would like to use this user control in another project. I right-click the project name in the solution explorer and select add>existing item, change the drop down to all files and select the files uc1.xaml and uc1.xaml.vb. This of course adds the files to the project but there is no correlation between the xaml and the code behind file and there is no way to use the control. What is the proper way of doing this?

Reed's answer is a good architectural one. If you plan on creating a control that you will reuse in many projects then it's best to use a control library.
Your original question is valid in some situations though. Say you have some source code from the Internet that you've unzipped to your drive. This project contains a .XAML file and its linked .vb file that you want to add to a project.
As you seen, the Visual Studio Solution Explorer doesn't link the files when adding with the "Add Item" dialog. I think this is a bug. I find that if I reload the project, the affiliation is added.
Here's a workaround I use. I drag the files from Windows Explorer /File Explorer onto the project in Solution Explorer. That works correctly the first time.

This of course adds the files to the project but there is no correlation between the xaml and the code behind file and there is no way to use the control. What is the proper way of doing this?
Normally, you'd add a reference to the other project, and use the UserControl directly.
This allows you to build a single project with your UserControl, and use the resulting assembly (DLL) in multiple projects without duplicating the code.

If you want to reuse your user controls you need to create a new project and choose "Class Library" from the list of available projects. When compiled this class library can easily be used by any number of other projects and solutions simply by adding a reference to compiled DLL created when you build this class library.
Edit: As mentioned in other answer it's "WPF UserControl Library", not simple "Class Library"...

You just need to add the .xaml file and VS should auto add the code behind(nested). I've seen this not work a few times and as #Walt Ritscher said this is probably a bug.
I found simply restarting Visual Studio and reloading my solution worked.

Related

Failed to load toolbox item in VIsual Studio in WinForms C++

I am creating a windows form application in managed C++. To add a custom user control either one can create a new project or add CLR User control to same project. When I add a user control to same project it builds and shows control in toolbar. When I drag and drop it onto my main UI it throws up error.
"Failed to load toolbox item, It will be removed from Toolbox".
There is no error if I create a separate project under same solution and compile control as a DLL. Is there a way I can add a custom user control to same project and use it.
Also I looked up similar question on the topc but none of them has helped me. I have checked the files and I am building the project as Win32 and there is no way control being separately built as x64.
Had same problem after one or both of renaming project or/and changing platform, most posted "solutions" provided to other people asking the same question didn't seem to work.
What did work for me was to go into the project folder and delete the obj folder, and just to be safe the bin folder too, then build again. (Seems the so called Build->Clean menu item only does half of the required work.)

Could not load 'Telerik.Windows.controls.FixedDocumentViewers'

I'm working with WPF and Telerik controls. I encapsulated RadPDFViewer inside ControlTemplate (I did that before for RadGauge and it works good), after I added all the required assemblies.
In the designer(user control) I can see the component as a gray rectangle but when I run the application it gives me error as u see in the attached file and it doesn't show me the component.
When I use RadPDFViewer not inside the ControlTemplate, in regular window it works fine.
Why only 'Telerik.Windows.controls.FixedDocumentViewers' can't be loaded (BTW all my assemblies) are the same version.
Thank you
The error you are getting is obviously around missing dependencies. The information you have given is not enough to respond with a definitive answer. So I will give you a best guess
You need to ensure that you copy to the output folder the assemblies Telerik.Windows.Controls.FixedDocumentViewers has a reference to. Not all references will be required as .net only loads assemblies when it requires them.
I will assume you are using the standard binaries and not the noxaml binaries.
So, using JetBrains dotPeek (a free .net decompiler), I got the following.
Telerik.Windows.Controls.FixedDocumentViewers has a dependency on the following
Telerik.Windows.Control,
Telerik.Windows.Documents.Core and
Telerik.Windows.Documents.Fixed
Telerik.Windows.Documents.Core has a Dependency on
Telerik.Windows.Zip.
and Telerik.Windows.Documents.Fixed has a dependency on
Telerik.Windows.Documents.Core
All of the assemblies has references to the usual suspects.
Since you appear to be playing with PDF files, you may need to load Telerik.Windows.Documents.FormatProviders.Pdf and if so, it has a dependency on
Telerik Windows.Documents.
and the other dependencies are mentioned previously.
To solve your problem, I would add the first 3 to your solution (Telerik.Windows.Controls is probably already added), and then add each of the other assemblies in turn until you find what you require.
I hope this helps.
In the VS Solution Explorer, right-click the Reference and make sure Copy Local is set to True.
I solved it. It's huge solution so I have to add this assembly to the startup project.
Thanks a lot

Final steps in using MVVM to decouple GUI from business logic?

Just recently, I learned about using MVVM to decouple GUIs from the underlying model. I ended up learning as much as I could to convert my current application over to MVVM, and was largely successful. Now I need to figure out how to actually take a GUI generated in Blend and use it in place of my current GUI, which was designed in Visual Studio. I can't find any resources on the web for making this as seamless as possible. I'd like to know what you all have done and have had work for you.
My ultimate solution would be something that would allow me to, at runtime, select a skin from a menu and immediately have the GUI change from the current one to another that the user selects. Can anyone point me to posts that explain how to do this?
My current goal is less ambitious -- I'd like to be able to add my new Blend GUI into my Visual Studio project and when I compile, have the new Blend GUI appear. If I want to go back to the old GUI, I would have to recompile. For now, that is okay.
I've got my Blend project added to my VS2008 solution, and have set it to be the startup application. That works fine -- if I run the app, my new GUI appears instead of the old one. The problem now is that it needs DLLs that are actually in a different folder -- the bin\Debug folder of the original startup application. Am I supposed to leave my original GUI as the startup application, and then have its App codebehind load the other GUI?
Also, each of the respective GUIs needs a reference to the ViewModel. In my case, I was just instantiating it in my current GUI class. For the Blend GUI, I instantiated one there as well, since only one of the GUIs will be active. Is this where something like the Unity framework should be used?
Sorry about all of the possibly-incoherent questions, but I'm not quite sure how I should proceed from here. I feel like I'm so close to proving to myself that MVVM is the way to go from a GUI standpoint (I'm already sold on the testability bit).
All the examples I've seen dynamically switch GUI appearance by using some form of ResourceDictionary swapping. A few links:
Load XAML Resource Dictionaries at Runtime
WPF change theme/style at runtime
Hope that helps.
I found a mistake, where in one part of my code I was using the wrong property to get at the currently-running assembly's path. I am now using
System.Reflection.Assembly.GetExecutingAssembly().Location
Although this does work, it only works if I copy the exe from the Blend project's bin\Debug folder into my main application's bin\Debug folder. I will have to live with this by using a post-build event, I guess. I was so spoiled for the past several months working with .NET, where I didn't have to do this (like before in C++) because all of the referenced assemblies get automatically copied over. If I want to debug any code-behind, I also have to set the starting executable in the Blend project's settings, which is inconvenient as well, especially when working on different computers where the paths aren't set up the same. Any suggestions here would also be appreciated!

Linked Themes/Generic.xaml Files will not work in visual studio 2008

i have a project that i am doing and i need to share the code between silverlight and WPF Assembly problem is that even though the wpf assembly is the owner of that file
and the silverlight assembly only has a link to the file, all of the build actions are page everything is correct. if i make the silverlight assembly the owner then silverlight works and wpf doesnt, and currently with wpf being the owner i dont get any errors at all it just never styles the control like it cannot find it..
Note: both projects exists in the same solution.
this scenario builds and runs fine
wpf project
|__Themes
|__Generic.xaml
|__SomeControl.cs
this scenario builds and runs but will not display the control
if i change them from linked to normal it will work fine.
i just want to share this source code and not have multiple versions of the same file floating around.
SilverlightProject
|__Themes
|__"Linked"Generic.xaml
|__"Linked"SomeControl.cs
sorry for my corny Tree view representation
+++++++ UPDATE +++++++++
i have noticed when using any linked file regardless of if it is silverlight or WPF
the link file will not build into the Themes folder in the resource only the root.
i used reflector to see where my resources ended up after compilation of the assembly including the linked file and they ended up in the root , so with that being said. is there a way to prevent this or a fix for this if this is indeed non intended behavior .
i would really love to get this figured out as it has been driving me insane for a while now.
Silverlight XAML and WPF XAML do not have the same namespace - so they aren't directly reusable.
My mistake - you're right - now with Silverlight 3 the namespaces are the same:
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml
What is the Build Action in the Property Pane for the XAML?

Can I use a custom control in a stand-alone XAML file?

Quick intro: I have some Silverlight 1 content that people have been editing and putting into HTML pages. Unfortunately, this means a lot of repetative creation of Storyboards, etc. We can use Silverlight 2, but not everyone here is familiar with C#/has Visual Studio. What I would like to do is create some custom controls in C#, output to a DLL, then reference the DLL in the XAML file (just as if it was part of a Silverlight project in VS).
I've tried adding this:
xmlns:mycontrol="clr-namespace:MyControl;assembly=../../content_GLOBAL/controls/MyControl/MyControl"
in the Grid tag that is my root. I know the path to MyControl.dll is correct. When I actually try and use it, though (I add <mycontrol:MyControl></mycontrol:MyControl> to the grid) and I get a parser error. It all seems OK if I don't add the control, even if I leave in the xmlns.
I suppose on some level, this makes sense--looking for an assembly is useful if you are going to build something, and since this XAML isn't in VS, it isn't actually building anything. Is there another way that I can reference, and use a custom control in SL2 in a stand-alone XAML file?
Let me first make sure that I understand you correctly: You have a Silverlight app that doesn't use any managed code and is not packaged in a .xap file. The <object> tag refers to a loose xaml file.
This is the Silverlight 1 app model and can still be used in SL 2, and such apps are referred to as "v1-style apps" (even though they target SL 2 and may use features not present in v1). When an application in this form is used, the Silverlight plugin does not load any of the managed components of the runtime (e.g. the CLR, etc.), so referencing a dll from a v1 style app is not going to work.
However, there might be work-arounds to the challenge you are facing here. One solution might be to use a v2 style app that is packed into a .xap and uses managed assemblies. One of the reasons you gave for not doing this is that other developers one the team are not familiar with C#. This should not be an issue as C# is not a requirement for building apps in SL, you can continue to program against it in JavaScript. And if you are just writing JavaScript code, Visual Studio is not a requirement, since there is nothing to compile. They can just edit the .html/.js files and use the .xap/.dll files that were already compiled. Does this make sense?

Resources