use vb.net class library in c# Project - c#-to-vb.net

I'm trying to use a vb class in my C# project. I create a new vb project (with this vb class), compiled it and added a reference from the c# project to the vb project.
When I'm trying to get into to vb class from C# class I can't find it. When I'm trying to "using" the Namespace I get Error. I can't reach this class no meter what!! any suggestions?

Is the class public? If not, you won't be able to see it from your C# project.
Check you've got the namespace right, noting that VB projects have a default namespace which is automatically prepended to whatever's in the source, IIRC. When in doubt, use something like Reflector or ILDASM to see what's in the assembly.

As you didn't post any code, I can only guess - is the class in the vb project marked as public?

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.

Intillisense fails on undefined namespace in XAML on Visual Studio 2010

I must be brain dead. I have tried for six hours now (with Google) and can not solve this very very simple problem.
In Visual Studio 2010, I created a project. (right-click on solution, Add new project...).
Inside the new project, I created a new folder of name Helpers. To the folder I added a class for a converter.
Inside the project, I added a new XAML window (in WPF). The Target Framework is .Net Framework 4.0
Everything has been built (multiple times) without compilation errors. The platform target build for the project is X86.
The problem: Intellisense does not see the namespace for the class under the Helpers folder and so the Visual Designer fails. The namespace in the Helpers folder is:
Chaos.UI.Helpers
and trying to reference this namespace in the XAML like:
xmlns:converters="clr-namespace:Chaos.UI.Helpers"
ofcourse results in the dreaded:
Undefined CLR namespace. The 'clr-namespace' URI refers to a namespace 'Chaos.UI.Helpers' that is not included in the assembly.
These are clean files in the same project. The only added code is in the converter class contained in the Chaos.UI.Helpers namespace.
How do I fix this? (I need the designer to work).
Anybody, Plz help
Try use assembly
xmlns:converters="clr-namespace:Chaos.UI.Helpers;assembly=Chaos.UI"

Visual Studio WPF Assembly and DLL solving

I'm improving an existent WPF project with a TCP/IP protocol that sends and receive small objects. I'm using Visual Studio 10 but I'm stuck.
I'm using BinaryFormatter to serialize the objects and until I used to send and receive with two classes in the same project I had no problem, but when I separated listener and sender (TCPListener and TCPCLient) on runtime i had this error:
"Can't find Assembly 'ProjectName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'".
Googling I found out that the problem could be solved creating an external dll called from main program, but I don't know how to pass the MainWindow to the dll because the library not even can find WPF types (such as MainWindow, Combobox or Textbox)
Can you help me with this dll problem or do you have any ideas to solve the assembly reference problem?
First of all, you shouldn't pass the MainWindow to a class you intend to serialize. This data class should actually not have any dependencies on the GUI. In fact, if you want to serialize a class, its public interface should in my opinion only contain primitive types or classes defined in the same assembly.
However, for a quick fix, and if you don't want to rewrite your project completely, I found that the easiest way to get the dependencies for a WPF library right was to start a new WPF application project and then change its project type to WPF Class library in the application tab of the project properties.

How to avoid XAML namespace import in every Window

Is it somehow possible to avoid xmlns:Engine="clr-namespace..." if i need the same namespace in each of my application windows?
Thanks
I'm agree with HCL, I think it can't be done and adding the namespace will not consume more resources.
I will give you an alternative. You can change the the window template (or create one of your own) to make it include the namespace for you.
In my computer, for VS2010, the path of the template is:
C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates\VisualBasic\WPF\3082
Note that one is for vb.Net, there is a CSHARP directory as well.
Edit:
As far as I Know, the templates are for each computer, not for each project, but when you edit the template you can use conditionals, for example:
$if$ ($targetframeworkversion$ >= 3.5)using System.Linq;
You can use the $rootnamespace$ constant to check for a specific project and then add the namespace or not. Maybe a constant with the current project name exists, i don't know, but $rootnamespace$ should be enought for this pourpose.
If it's your own the sourcecode to the assembly you using in the Engine namespace you can accually totaly remove the prefix requerment and the demand of adding something. With this little trick you just append your own namespaces to the xaml namespace (that allready is included).
In your "Engine" project (not the project where you have the xmlns:Engine line, can't be the same project where you use it, it will not work then) make sure you have the WindowsBase reference added and then add the following in the the AssemblyInfo.cs/AssemblyInfo.vb file (c#: found in the Properties folder) (vb: you need to use the "Show all Files" button and then you will find it in the My Project folder):
c#:
[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "The.Class.Namespace")]
vb:
<Assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "The.Class.Namespace")>
You can do this several times if you project have classes in more then one namespace you wanna use in the other projects xaml.
No, there is no way to avoid this. However declaring a namespace does not mean that it consumes resources (at runtime).

Recover WinForms from dll

We think most of the source has been recovered through .NET Reflector. A utility which export the dll as C# source.
The only missing part is WinForms which is not included when .NET Reflector export the dll source.
Is there any way to get the WinForms recover from dll?
Thanks.
If it's not in the dll, you can't get it out :-)
But possible some class files are the forms you're searching for. Maybe you have to add a reference to System.Windows.Forms.dll to your project.
From Visual Studio 2005 on, Windows Forms classes are partial classes split into a Designer generated part (MyForm.designer.cs) and your actual implementation (MyForm.cs). You can see that in Explorer when you create a new Form from scratch.
In order to resolve your problem you would have to split up the class exported by Reflector the same way that Visual Studio does: Place the InitializeComponent() method and all control declarations into the MyForm.designer.cs file and the remaining code into the MyForm.cs file.

Resources