Problem with casting activeX object onto my object - winforms

I'm trying to use Windows From as ActiveX control inside SAP B1. Inside that control I host WPF control. It works great except when I try to cast activeX object onto my class so I can use all the methods on my class. But when I try to do that I get an exception. What is wrong?
I did registration and all that stuff. I can even display that control inside SAP with some handcrafted data (initialized in control constructor). Although I'm not sure if this is more general problem and not SAP-specific.
Welcome any suggestions and thanks in advance.
The message I got is:
Unable to cast COM object of type 'System.__ComObject' to class type 'CapacityCalendarControl.CapacityCalendar'
[Guid("9A2F1CCB-8DC0-4CE5-B3C7-403A1A8CDCE2")]
[ProgId("kamil.calendar.control")]
[ClassInterface(ClassInterfaceType.AutoDual)]
[ComVisible(true)]
public partial class CapacityCalendar : UserControl
{
//internals...
}

A System.__ComObject (a COM wrapper) cannot be cast to a .NET class. The simplest way for you is to use TLBImport (Type Library Import Tool), I think, if your activeX is equipped with a .TLB (external or embedded).
See here for more: Tool for creating .NET wrappers for a COM DLL?

Related

vb.net Imports needed for WPF Window class?

I have a class library with extension methods and I'm trying to write an extension method that works with a Window class, from a WPF app.
When I try writing the extension to accept a parameter of type Window, I get an error that this type can't be found;
I can't seem to find an assembly to import that will resolve this error, so I thought I'd post it here to see if I'm doing something wrong.
As an example, here's a simple extension I'd like to be able to use:
<Extension>
Public Sub helloworld(window As System.Windows.Window) 'Error- type system.windows.window is not defined
...
End Sub
But I'm getting the error that the type system.windows.window is not defined.
I've even tried adding a reference to this class library to the System.Windows assembly, and adding an imports at the top of the screen, but it doesn't help.
I ended up taking a look at the references used on a normal WPF app project, and realized that there's a reference to PresentationFramework.... which is verbiage similar to...Windows Presentation Framework (WPF) ;)
I added this reference to my class library, and suddenly everything just worked.

Fix XamlObjectWriterException, Cannot create unknown type in F#, FsXaml, WPF app?

I am getting this error in a WPF app that is built with F#, FsXaml, and Elmish.WPF.
System.Xaml.XamlObjectWriterException:
Cannot create unknown type '{http://schemas.componentone.com/winfx/2006/xaml}C1ProgressIndicator'.
at System.Xaml.XamlObjectWriter.WriteStartObject(XamlType xamlType)
at FsXaml.InjectXaml.from(String file, Boolean loadFromResource, Object root)
A Views project contains XAML and includes a reference to C1.WPF.4.dll.
The XAML declares an instance of a control from C1.WPF.4.dll.
However, there is no F# code that uses any types from C1.WPF.4.dll.
Consequently, Views.dll does not contain a reference to C1.WPF.4.dll, as confirmed by IL Spy.
Consequently, when I build App.exe, C1.WPF.4.dll is not included in the output folder.
Consequently, I get a run-time error.
While I could include a reference to C1.WPF.4.dll in my App project, I would rather not.
Is there a better way to do this?
To get this to work:
In your XAML file, replace the XAML namespace with a CLR namespace. For example, instead of xmlns:c1="http://schemas.componentone.com/winfx/2006/xaml" use something like xmlns:c1="clr-namespace:C1.WPF;assembly=C1.WPF.4".
This is required because when App.exe is running, it may not have loaded the controls library yet. It is in the controls library that an attribute creates the mapping from XAML namespace to CLR namespace. By explicitly stating the CLR namespace and assembly, the XamlReader can find the class to instantiate.
Create a dummy instance in the Views code. The dummy instance is an instance of something from the controls library. This creates the dependency in Views.dll on the controls library.
// force the compiler to add a reference to C1.WPF.4.dll
let private forceC1DllReference = C1.WPF.Anchor.TopLeft
type MainWindowView = XAML< "MainWindow.xaml" >
This second one feels like a hack. I wonder if there is a better way to say, "Please add a reference to this DLL even if I'm not using types from it."

How do you get the WPF / XAML designer to work with custom classes with dependencies?

I'm using a custom class to perform validation in a WPF app and have defined a class which inherits from ValidationRule. The class has a dependancy which I would like AutoFac to inject at runtime, but the Visual Studio 2010 XAML designer needs a parameterless constructor in order to work.
It seems the only solution is to create a DependencyProperty for the dependency and bind it in XAML, but that means the dependency is publicly accessible and mutable, neither of which I want.
Is there any way to pass a dependency in to a class you intend to use in XAML which will do all of the following?
Not break the designer.
Keep the dependency private.
Make sure the dependency is set before the class is used.
How badly do you want to do it?
The features in XAML 2009 provide the following elements which could help you:
x:Arguments would allow you to pass an argument to the constructor. Can this argument be somehow bound to a property of your view model?
x:FactoryMethod allows you to specify the method which creates the object. I assume this can be a method of your window or usercontrol, which would have access to your dependency container.
But here's the problem: XAML 2009 can only be loaded at runtime via a XAML Reader, and can not be compiled. (See Can't get the new 2009 XAML primitives working, why?).
So global static objects (eurgh) might still be the less painful solution.
Later Edit: I reread your question, and realised you had put "Not break the designer" as one of you requirements. This excludes XAML 2009 from the possible solutions.
This is an excellent question. I used techniques demosontrated here to keep the designer working: http://jonas.follesoe.no/2008/07/24/youcard-re-visited-implementing-dependency-injection-in-silverlight/
It not only keeps it working but you can use the technique to put mocks into the designer. Very handy.

WPF Binding data class programmatically

I have a data object (lets call it employee) who has many get/set methods to manipulate/store various parameters.
This object is a singleton object used and referenced in many places in the WPF app.
I am now building a view using Microsoft Expression Blend and would like to bind using Blend cool interface every of my control to the various method of my data object.
When I try it, it will always create a static version of my employee data class.
Is there a way to benefit from the nice user interface of blend to bind parameters and then afterward in the code, set my singleton employee data class as the reference to use?
best,
You can tell blend to use a given type for design-time bindings, and then use whatever you like at runtime. The syntax looks like this:
d:DataContext="{d:DesignInstance data:Employee}"
Read here for more info.

Custom Control and Visual Studio 2008 SP1

I've created a custom control (a class that inherits from Control). When I put it on a Form I can work with it on Visual Studio IDE. It shows me an error and I don't see the form.
The error message is this: La variable 'ctrlImagen' no está declarada o no se asignó nunca.
It's a winform for a Compact Framework app.
How can I solve this? (it the class inherits from UserControl it works perfectly)
Sounds crazy, but Visual Studio is selectively executing code for your control in the designer. First thing to check is your constructors. Make sure you have an empty, default constructor, that is public, even if you never plan on using it. After that make sure any code you have tied to layout events (such as resize) are good to go, these are likely the culprits, as thats where I always find problems when my custom controls don't work in the designer.
It sounds like you have a bug in the code for your Control. This sounds like a runtime error that is preventing the control from rendering.
Re-read your code and look for potential null-pointer exceptions, unassigned variables, stack overflows, etc. The bug is lying in their somewhere.
Any chance your user control doesn't have a public default constructor? I can get a similar error "The variable 'userControlX' is either undeclared or was never assigned' if the constructor isn't public.
Would need more info, though.
It seems a design time exception has happened in initialization logic that gets executed before your form initialization logic executed. A good example would be a NullPointerException fired by the default constructor (or one or more methods it invokes) of a user control that contained by your form.
I put this on constructor to solve the problem:
this.ClientSize = new Size(21, 21);
The beging of my class is this:
public class ControlMapa : Control
{
public ControlMapa()
{
this.ClientSize = new Size(21, 21);
...
Thank you!

Resources