Navigating between WPF's and WinForms in the same application - wpf

I'm new to visual basic and I am currently coding in visual studio 2015. I am teaching myself visual basic (roughly 2-3 weeks). I work for a boat dealership, and I'm attempting to create an application that we can use at a boat show where Customers can come up to a computer and basically pick out a boat they want with all the additional options (any fishing rod holders, different color haul, motor etc).
So far:
I am using visual basic and decided I wanted to use WPF's. I currently have my program start up on the default the main MainWindow. Inside that MainWindow is where I host all of my future 'pages'
Title="MainWindow" Height="850" Width="1200" Source="ChooseYourAdventure.xaml" ResizeMode="NoResize" WindowStartupLocation="CenterScreen" WindowState="Maximized">
From here I have the customer choose which boat they are interested in and they click a button which then switches them to a new 'page' being hosted in MainWindow still. On the new 'page' they choose the motor, and once again click a button and go to a new 'page' where they finally choose the trailer type they are interested in.
This is where things get difficult for me
After the trailer 'page' I want to be able to take in the customers information(First name, last name, etc...) and add it to a database. Since I am unfamiliar with visual basic and especially with WPF's I have no clue what the strategy here is. Everything I've googled thus far always shows how to add records to a database through the use of 'Forms'.
What I had in mind was using WPF's for most of the application then switching to a 'Form' for when I needed to enter in the customers information for the database
My question is: Is it possible to navigate from a WPF 'page' into a 'Form'?

You can create your main project as a WPF Application and your other project as a Windows Forms Application and set its output type as Class Library.
Then in main project (WPF Application) add a reference to other project (Windows Forms Application).
Now you can use all of the forms and classes of other project in your main project. To do so, it is enough to instantiate a form of other project in and show it.
For example if Windows Forms Application contains a Form1, you can use this code in button click handler of your main project:
//Instead of NameSpace put the namespace of Form1
var f= new NameSpace.Form1();
f.Show();
Further more, if you want to use a UseControl created by windows forms in your wpf forms, you can use WindowsFormsHost

Related

Best Starting Point for WPF Revit Add in

I am wanting to rewrite one of my Revit Add ins so that it utilizes WPF with MVVM because I like the look and functionality of WPF better than Windows Forms.
I have used the Revit Template Wizzard from Jeremy Tammik for the Form based add in, but adding a WPF user control seems not to work (a run time error that the xaml resource cannot be found).
I found a WPF MVVM revit add in example (AddMaterials, here is the github link, which will add materials from an Excel spreadsheet) but it does not follow what I am expecting to see at the top level.
Revit Add ins have an app.cs file which tells Revit how to register and access the DLL (ribbon panel buttons etc).
A windows WPF app will have app.xaml as the top level entry point.
The Add Materials project has neither which tells me that it must be
a class library, however the views are not using UserControls
rather they are Windows which I prefer. However Visual Studio does not
let you add a Window for a Class Library type project.
The third issue is easily solved by simply copying windows from a WPF application project into a class library project. But I don't really understand how the class library will instantiate in Revit without following the app.cs code from the template. Is anyone else creating add-ins this way, and if so can you let me in on any tricks or discussions that will help? Has anyone created a WPF Revit addin template for Visual Studio?
When I add a WPF window and try to instantiate it I get an error that it cannot find the xaml resource (System.IO.IOException: Cannot locate resource 'xxxx.xaml'). I have tried to fix this according to advice found when googling for this error, but to no avail. I am thinking it comes from being in a form based project, and that I may have to just start with a new project without the form stuff.
I have now verified that indeed you can start with the Revit AddIn Wizzard and use WPF . . . I started from scratch and copied in a window created in another project and got it to run (after adding the various references, namespaces, etc). So my problem seems to just be with the original project which already had a bunch of form stuff added.
Yes, I'm using WPF to create Revit Addins. It works well. You can easily create your own WPF template from the SDK samples:
Start with one of the Autodesk-provided SDK samples. I used the "DockableDialogs" sample. I know this one works, your mileage may vary with the others. If you're looking for windows rather than docked panes in the UI, another sample (perhaps the AddMaterials sample) is probably simpler.
I used Visual Studio to turn the sample into a template. File - Export Template -> select "DockableDialogs" or other WPF sample project.
Create a new project based on the template you just created. This was the easiest method I could find to get the WPF internal bits wired up correctly.
I'm not specifically familiar with the AddMaterials project, but to clarify your bullet points.
Revit Addins - It's not the file name (app.cs) but rather they must extend IExternalApplication or IExternalCommand. If you are creating a xaml interface (rather than just running a command from a ribbon button) you'll use 'IExternalApplication' as your entry point. Look for something like this in the sample:
public class ThisApplication : IExternalApplication ...
I don't used a top level app.xaml, but instead have page.xaml pages which are called by the Revit app. In my case these are Pages rather than Windows, which extend the IDockablePaneProvider class. These must be registered with the application which can then can be show, hide, etc your Panes. I imagine this is simpler with Windows, but haven't done it myself. For the dockable panes, your xaml.cs should start out something like:
public partial class MainPage : Page, Autodesk.Revit.UI.IDockablePaneProvider ...
Yes, the project is a class library in the sense that it is a collection of classes, at least one of which extends IExternalApplication or IExternalCommand. Remember that you're not creating a standalone application, but adding functionality to an existing Windows application (Revit). Revit will instantiate the ThisApplication class and then call its .OnStartup() method when the Revit application starts. This shouldn't stop you from adding .xaml or .cs files to the project, though. I can do it using VS Community 2015 using Ctrl-Shift-A.
Hopefully this gets you started - I've been able to implement a WPF UI in Revit without any prior WPF experience, and I'm not even a real programmer, so it's definitely possible. Good Luck!
addendum
If you want to add WPF elements to an existing revit addin, you can follow the instructions here: How can I connect xaml and xaml.cs files
Ultimately I found it easier to migrate my addin code into a template made from a working sample, you may want to try this approach as well.

How to navigate between WPF and WinForms

I'm new to visual basic and i'm currently trying - and at the moment I've done my entire program using WPF's because I wanted to easily switch through different pages which i've hosted in a single window. Well I've come to a halt because I'm creating a page currently where a customer can enter their information and add it a data base.
Now, I have little experience in coding in visual basic(self taught for 2-3 weeks) and I have no clue how I would go about adding a database, and adding to the data base using WPF's. I have seen some examples of people adding a database, and adding to a data base using Forms.
I was curious if I am able to using WPF's(page) for most of my program, and then switch to a Form when I want to add the customer to a database, then switch back to a WPF(page)?
Yes you can...
If your main project is WPF you need WindowsFormsHost and if your main project is WinForm you need ElementHost. For more information you can read this tutorial.

Adding a WPF form to a library in visual studio express

I managed to create a WPF project, but for some reason, when instead I am in a library project and I try 'Add Item', then go to WPF, there is only one thing I can add, which is a user control.
I am not clear what a user control is, but clearly it has no 'Show' method.
If instead I tried copying the mainwindow from my WPF project to the library project, but then it would not compile (and I added missing references).
Am I doing something wrong, or is Visual Studio Express for Desktop just not featuring a WPF form?
EDIT: it seems I am after a WPF window instead (as per the Mainwindow : window class that gets generated when creating a new WPF app), not a form.

Adding WPF Windows to an XNA Game Project

I'm creating an experimental game (which would eventually become a map editor for a game) and I'd like to include a WPF window in the same project which would communicate with the game logic. This is not another render-xna-game-in-wpf-window question, I've read tons of those, I want to spawn a seperate WPF window independent from the XNA game window, but in the same program, in the same project. When I click Add Item and select WPF, all it gives me as an option is a User Control, no window or any other options etc. I've tried referencing PresentationCore but no lock either, am I missing something? I am not super with interoperability, so forgive me if what I tried is stupid. I also don't want "hacky" solutions (like creating a window and a borderless control and setting their location same etc) if it can't be done just tell it so, but I'm sure there is a relatively "clean" way of doing it since they'll be completely seperate, think of it like a very simple MVC: M Game Logic, V XNA Window, C WPF Window. And don't offer me Forms, I know how to go with forms, but I want WPF, I just work with WPF/XAML and WPF style controls more easily.
Being able to add a Window from the Add Item menu is controlled by a Visual Studio project type GUID. Have you tried to create the window xaml and cs files manually (or create it in another, test solution and Add Existing file)? Or just create a Window instance in code and show that? Also, Window is in PresentationFramework, not PresentationCore.

Reusing a Control in another Windows Forms Project

I've got lots of web experience but am a total novice when it comes to Windows Forms so this might be easy to answer:
I have Project1 which I've added a reusable control to which allows filtering and searching of data and presenting results.
I now want to use the same control in another Project.
When I open Project1 and select my form the reusable control appears on the toolbar so I can drag it onto any form in the project, but how do I get this to happen in my new Project?
You can add a reference to the (binary) control:
menu Project/Add Reference/tab Browse
Then it should turn up in the Toolbox (menu View/Toolbox).

Resources