Usercontrols that inherit from abstract class - c#

I've got a usercontrol that inherits from an abstract class. Basically looks like this.
class SimpleSlideView : View
{
}
public abstract class View : UserControl
{
}
The project compiles and runs fine. I can take the usercontrol (from the toolbox) and drag it into a form and it is displayed in the designer correctly. However, if I try and open the SimpleSlideView control itself in the designer I receive the following error:
The designer must create an instance
of type 'Animation.View' but it cannot
because the type is declared as
abstract.
What have I missed?

You can find possible solution here: How can I get Visual Studio 2008 Windows Forms designer to render a Form that implements an abstract base class?

Related

Winforms Inhereted Control preventing the designer from working

I have 2 forms that inherit a control from the class below:
public class AInbox: Form
{
public FlowLayoutPanel InboxItems;
}
The forms inherits as such:
public partial class Inbox : AInbox
{
...
}
In the Designer.cs file i commented out the original "InboxItems" control declaration and everything compiles and runs fine... except the GUI designer. When i open the Designer I get the error "The variable 'InboxItems' is either undeclared or was never assigned."
Is there any way to use this inheritance and still have the designer work?
I'd recommend against inheriting a form with generated code (like you're doing with Inbox).
If you want the child class (Inbox) to add additional controls, I wouldn't use the designer directly on the child class, because I don't think the visual studio form designer will play nicely when half of the form was designed in the parent class. If you need to reuse certain parts of your form in a different form, you might want to consider moving that part of the form to a separate user control. You can use the designer on this user control and later put the user control in the forms.
If you just need to have the same form, keep an instance of the form in your other class. Move your logic away from your form (view) and in your other class (controller).

How to make a XAML view (a page) inherit from a class, that uses UserControl as generic type

I have an issue, and I don't know if I can do that, or if there is another way to do that. I have an abstract class called "BasePage", it is my .NET Standard library, so I can't access to System.Windows namespace. Its declaration is:
public abstract class BasePage<VR> where VR : new()
EDIT: The class above has a public property to access to VR
So now, for each platform (WPF, Xamarin, etc) I have to create its own implementation of the class begin VR the content control of each platform (UserControl for WPF, for example). I already did it and this is:
public class WindowsBasePage : BasePage<UserControl>
Now, every time I want to create a page to add content to a window, I have to create something like this:
public partial class UserPassPage : WindowsBasePage
{
public UserPassPage()
{
InitializeComponent();
}
}
The problem becomes when I have to set it in the XAML file (the View). I can create it and it builds:
<local:WindowsBasePage x:Class="Bitture.AppManager.Manager.UserPassPage"
[...]
xmlns:local="clr-namespace:Bitture.AppManager.Manager"
mc:Ignorable="d" >
</local:WindowsBasePage>
but I can't add components (like buttons, text, grids, stackpanels, etc). I want to know If I can do it with my current code or there is something I have to change. Because I have to access to the generic type WindowsBasePage that inherits from
class WindowsBasePage : BasePage<UserControl>
I am not sure what you are trying to do here but you can't add UIElements to a custom class of yours that doesn't inherit from any of the common WPF base classes such as for example Panel or ContentControl, and expect it to be rendered as a UserControl or some other WPF control.
This won't work. WPF knows about how to render WPF controls but it doesn't know how to render a custom BasePage<UserControl>.

Custom Generic UserControl doesn't appear in Toolbox

I need to create a custom User Control with generics because I have a BindingSource with a data source of type T
public partial class ABMControl<T> : UserControl
{
public ABMControl()
{
InitializeComponent();
this.bindingSource.Datasource = typeof(T);
}
}
In the form designer the custom user control does not appear in toolbox because is generic.
What is the solution?
It's expected behavior for toolbox.
When dropping a control from toolbox onto your form, you are commanding the designer to create an instance of that control. You cannot create an instance of GenericControl<T> without determining T. Instead you need an instance of GenericControl<SomeClass>.
So it completely makes sense the generic control doesn't appear in toolbox because it has no usage in designer and designer doesn't know what type should it use for generic parameter when creating instance.
Also about designer, considering this post: Generic Base Class for UserControl starting from VS2015.1, Windows Forms Designer shows classes which have generic base classes without any problem. The following class will be shown in designer without any problem:
public class SomeClassControl:GenericControl<SomeClass>
{
}
For older versions of Visual Studio, use the workaround which is described in the linked post:
public class SomeClassControl:SomeClassControlBase
{
}
public class SomeClassControlBase:GenericControl<SomeClass>{}

Detect a form class in the inheritence tree

I have a winforms application, in which I need sometimes to indirectly inherits From the class Form
Example
public class FormTest : TemplateForm
{
}
public class TemplateForm : Form
{
}
The problem is that the design of FormTest is disabled because it didn't inherit directly from Form class.
When iclick into Show Call Stack I had
at System.ComponentModel.Design.Serialization.CodeDomDesignerLoader.EnsureDocument(IDesignerSerializationManager manager)
at System.ComponentModel.Design.Serialization.CodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager manager)
at Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager serializationManager)
at Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomDesignerLoader.DeferredLoadHandler.Microsoft.VisualStudio.TextManager.Interop.IVsTextBufferDataEvents.OnLoadCompleted(Int32 fReload)
So, How can I change this behaviour and make the design enable , I mean how can I make the design of the form enable when the Form class is detected in its inheritence tree?
The feature you're asking for is Visual inheritance, make sure you have given at least protected access for the controls you need to access in the derived class. Then you'll be able to access them in designer.
Note:Nevertheless some controls like DataGridView, TableLayoutPanel etc prevents them from being edited through derived class via designer. Am not sure why.

Have a WPF Control Class being a template class

is there a way to have a WPF UserControl Class to be a class with a Template type?
e.g.
public partial class MyControl : UserControl
should be:
public partial class MyControl<MyData> : UserControl
as I always get compile errors that MyControl than has no reference to InitializeComponents which is in the automatic generated part of the class.
The problem is, that I can't tell in the xaml part of the class that the usercontrol is of type MyControl<MyData>. I even tried MyControl<MyData> ...
No, you can't declare a generic type in XAML. From http://social.msdn.microsoft.com/forums/en-US/wpf/thread/02ca0499-80af-4c56-bb80-f1185a619a9e:
Hello, you can use generic as long as
you don’t use XAML. But unfortunately,
if you want to use XAML to define your
control, you can’t use generic…
You can create a control in XAML that inherits from a generic type by putting a x:TypeArguments attribute on the root tag, but the control itself must be concrete.

Categories

Resources