C++ Parenting a WPF window from another assembly and handling events - c#

I've been unable to make this work because of what I believe is a glitch in Visual Studio, so I'd really appreciate if someone could attempt this situation and share what happens.
I have setup in a solution 2 projects:
- a C++ application which has been CLI enabled (.exe)
- a C#/WPF class library which has a .xaml form inside with a matching .cs window class (.dll)
I want to spawn the WPF window inside my C++ application, so I import its reference and create a new instance of the window and run under a new application context. Thats works fine.
I now want to make classes out of this window and handle different events inherited from protected functions in the C# window, so In the C++ assembly make a public ref class whom child is the .cs class of the .xaml powered window. This compiles fine.
ie:
public ref class myCPPWindow : myWPFWindow { ... };
I then change the window I spawn to the parent class which is located in the C++ assembly rather than the base class located in the C# assembly. Now I get an error on the InitializeComponent() part of the base C# class while loading the .xaml window that I require saying that it fails to load the .xaml window source from the C# assembly even though the base class works. Can anyone give an explanation/fix for this?

It looks like a common [library;user control]-[application;derived control] issue in WPF - I reproduced that even without C++. Without digging into explanation, general workaround is either aggregating "base" class or re-degisning base class to be templated control instead of user control (e.g. without .xaml file). If I understand correctly, your question is the same as The component does not have a resource identified by the uri question.

Related

c# windows forms create generic form [duplicate]

I'm using vb.net (vs2010). I'm moving some winforms to a dll. I have a form that inherits from the one which has some subs and functions (like a test app).
My original form is: (.designer)
Partial Class Form1(Of T)
Inherits System.Windows.Forms.Form
....
End Class
Form itself contains code and a toolbar.
My test form is: (.designer)
Partial Class TestForm
Inherits Form1(Of Class1)
I get "Cannot create an instance of Form1`1[T] because Type.ContainsGenericParameters is true" when VS try to load the designer. App is usable. I can build and run the project without errors, but I need to add controls and some code to each new form.
I tried many ways:
Visual Studio 2008 Winform designer fails to load Form which inherits from generic class
How can I get Visual Studio 2008 Windows Forms designer to render a Form that implements an abstract base class?
http://www.codeproject.com/Questions/419770/Csharp-reflection-GetValue-from-a-field-in-generic
http://madprops.org/blog/Designing-Generic-Forms/
All examples are for C#, and I don't know if I'm missing something...
Is this a bad design ? I know this is a VS bug but still it seems everyone fixed it by these links.
EDIT:
I'm building a DLL. Form1 is on this DLL and TestForm is in a new project. Those links works if I'm in the same project (a.k.a. the dll).
Thanks!
Is this a bad design ? I know this is a VS bug
Bad design, not a VS bug. What you are trying to do is fundamentally incompatible with the way the Winforms designer works. It has strong WYSIWYG support, the designer creates an instance of the form's base class and allows code in that base class to run at design time. Which is why, for example, you can set the BackgroundImage property and it is immediately visible in the designer. The Form.OnPaintBackground() method paints it. The designer is not involved at all, it just sets the property.
To make that work, it must be able to create the base class object. It can't in your code, it doesn't know what kind of T to use. Not an issue when you design Form1, the T isn't needed yet since it derives from Form and creating an instance of Form is not a problem. Big issue when you design TestForm.
You'd probably argue that it should use Class1 as the T. It doesn't, the odds that it can use Reflection to discover the generic type argument from TestForm are exceedingly low. That requires the type to be compiled first. That's a chicken-and-egg problem at design time, the TestForm class gets compiled after you design it, not before or while you design.
It's not like you completely cannot use your approach. It builds and runs just fine. You just have to live without design time support for TestForm. That's usually a deal breaker, you have to re-consider your design.

Insert a user interface in a DLL

In my project I have implemented this plugin manager:
https://code.msdn.microsoft.com/windowsdesktop/Creating-a-simple-plugin-b6174b62
In this way I can add .DLL file and make my project more modular.
I wanna know if in one of these .DLL plugin I can add a .xaml with user interface, and use it inside my main project to visualize the content of that xaml in my main GUI.
In this way I can make my app more modular not only by code library but also with user interface.
Thanks
If you create a project that contains WPF UserControl items, then as long as you expose those items through the DLL interface then you can utilise them in another project.
You should be able to verify this very easily by doing something like the following:
1) Within your 'DLL' project make a public class SquareControl, which is simply a UserControl under the hood, and specifically a canvas containing a red square of a fixed size.
2) Within your utilising project, reference the DLL.
3) Within your utilising project, in C# code somewhere create an instance of SquareControl, and check in the debugger that its properties are as you expect.
4) Then create a UserControl within your utilising project, and open VS Designer for that control. Within the empty Grid that has been created for you drop an instance of SquareControl, and you should be able to see this within Designer. Getting your xaml namespace definitions can be awkward the first time around but there's plenty of help available for that. Then fire up the application and see it there.

C# custom Windows Forms base class and designer

I am trying to implement the GUI part of a plug-in, which means that I have to inherit from a custom base class (which inherits from UserControl) included with the plugin assembly.
When implementing my own control, I would normally inherit from UserControl and going to the designer would be really straightforward (just double clicking on the solution explorer).
In order to be able to work with the designer, I do a first implementation using UserControl as base class.
The problem is that as soon as I change the base class into ApplicantTabControlPlugin (the custom base class provided by the plugin), I cannot open the designer for this control anymore. I.e., if I close the designer, it seems it is gone forever.
Is there any way to prevent this behaviour?
You should add
<SubType>Component</SubType>
to the project file entry of your base class.

Is there any fault in my assumption that the following class Should be usable in the Visual Studio Component Toolbox?

I have a large number of Windows forms components that I have created in one DLL.
I have historically had to import the project in which those reside into other solutions in order to utilize these components.
I am attempting to resolve that problem.
I currently have a definition as follows:
[Browsable(true)]
[EditorBrowsable(EditorBrowsableState.Always)]
[DesignerCategory("Provectusoft Forms")]
[DesignTimeVisible(true)]
[Designer(typeof(System.Windows.Forms.Design.WindowsFormsComponentEditor))]
public partial class Form : System.Windows.Forms.Form
{
...
}
And I'm under the impression that in some circumstance, at least one of these class attributes should provide the ability for the toolbox to ingest said components; however, I am encountering the specific error message of:
"There are no components in '<path>\Example.dll' that can be placed on the toolbox."
I'm curious if anyone has a resolution.
Thank you for your investment.
Try to use the ToolboxItem attribute for the form class.
See also: How to: Create a Toolbox Control That Uses Windows Forms
To ensure that all attributes were applied, right-click on the toolbox and check the "Show All" item. Then you should be able to see your form there.
But I believe that UserControl better fits the purpose.

Invoking a UI screen from within a class library function in WP7

I have recently started on Windows 7 app development, and I am stuck on the problem described below. Any help is greatly appreciated!!.
I am writing a WP7 class library function, which will be called from a Windows phone application. I want to be able to show a new screen to the user, after the function call has been made, invoked from the class library function. I also want this screen to be integrated with rest of the application UI properly. Like for ex., Clicking the back button on the UI screen, should take me to the UI screen that was present, before the call was made.
This WP7 class library has to be linked as a compiled library with the windows phone application. So keeping that in mind, I have following two questions.
1) Is it even possible, to launch a UI screen from a class library in Windows phone 7? If not, what else I could do here?
2) Is it possible to integrate this screen with rest of the application UI flow as described above.
Obviously you can include XAML pages in your library. To do that you have to include .pdb file which is generated along with the .dll for your library class. Copy both the .dll and .pdb file to your project and add reference to the .dll.
1) Yes. If you ever programmed Win32, then I understand your concerns. Handling UI in a dll was a bit problematic.
First to the class selection:
Popup class can be used to overlap current screen content. Its basic disadvantage is that it is not HW accelerated, hence unsuitable for complex screens or animations.
A better solution might be to store PhoneApplicationPage in your library (assembly). The result is the same as if the page was defined in the application assembly. This article describes how to navigate to another assembly.
When you navigate to another page (which is incidentally stored in another assembly), there is no shared UI context such as the main application window in Win32. The pages themselves are fully independent. URI of the first page is written in the manifest file. (Part of the xap file.) The application decides when to go to another page. Same as for browsers.
From the technical point of view:
Your class library produces an assembly (dll). This dll contains a) code (similarly to old good Win32 dlls), b) Xaml (exact copy of the Xaml code used in your library). It is the same as for your main assembly.
When you add a reference to the class library, that dll will be added to the xap file, i.e. to the installer. Go to your bin folder and look into the xap file. It is trivial as it is just a zip file.

Categories

Resources