I have four UserControls in my WPF Application - e.g.
VisualA, VisualB, VisualC, VisualD
I want each of them need to inherit a generic "Player" Class which contains a heap of shared code -. e.g. methods, timers etc
So far this is what I have tried in my Control's XAML
<UserControl x:Class="VisualA"
And here is what I have in a separate Class file.
Partial Public Class VisualA
Inherits Player
End Class
Public Class Player
Inherits UserControl
End Class
In my Window, I'm referencing the UserControl as normal:
<local:VisualA></local:VisualA>
But, I'm getting the following error:
Base class 'System.Windows.Controls.UserControl' specified for class
'VisualA' cannot be different from the base class 'Player' of one of
its other partial types
What am I doing wrong?
I was also under the impression any code (i.e. methods) inside the inherited class (Player) would be able to access the Controls in the UserControl by referencing by name - is that correct?
The base class in the XAML is still set to UserControl. Change it to Player. Also note that the namespace for the Player type will have to be defined. i.e:
<BaseClasses:Player x:Class="VisualA"
xmlns:BaseClasses="clr-namespace:MyProject.BaseClasses"
... all your other namespaces used
Related
I'm trying to use a C# class derived from UserControl as the base class for some xaml pages' layout root, so I can share some common functionality. I.e. the backing class is like:
public class BaseView : UserControl
{
// Some virtual functions I want in common ...
}
public class MyView : BaseView
{
// Overidden functions ...
}
The xaml file then references the class like:
<jt:BaseView x:Class="ns.MyView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:jt="clr-namespace:ns"
...>
</jt:BaseView>
This seems to work when running, but in the xaml editor, the code is all underlined with blue squiggly lines, with a fly-over message of something like "Cannot create instance of BaseView".
If I don't do this, and use UserControl in the xmal, I get errors because the partial class generated from the xaml then has the UserControl base class and not my custom base class.
How do I get the editor to know about my custom base class (i.e. get rid of the blue underlines)?
The most common cause for a design time "Cannot create instance..." sort of error is that the constructor of the element is doing something that doesn't make sense or is unavailable at design time.
I tried to derive from the Selector class cause I need a similar functionality as the ListBox but it is no ListBox.
I had a look at the signature of the Selector class and it is (http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.selector(v=vs.95).aspx)
public abstract class Selector : ItemsControl,
ISupportInitialize
But the problem is that the constructor is internal. So it is not possible to derive from this class outside the assembly (ListBox and ComboBox are in this assembly).
I now derived from the ListBox to achieve my goal, but my question is:
Why has the selector class an internal Constructor?
Because the Selector class is abstract. You can't create instances of abstract classes, and the easiest way to make sure you can't even do that by mistake (in a regular way) is to not make a constructor available.
I don't see an entry for the constructor on the MSDN, but my bet is that it's probably a protected constructor, not an internal one.
But from what I can see, nothing stops you from deriving from Selector, and create your custom implementation.
Edit:
Reflector shows the constructor to be internal indeed, so no deriving...
I want this usercontrol to be Created via MEF but also to have the usercontrol positioned and properties set on it in Blend... How can I force the composition of the imports that the usercontrol requires when I have a 'Concrete' reference to the usercontrol ?
What happended to PartsInitializer.Satisfy ?
Export for the UserControl
[Export(typeof(IWOFlyOutFilterMenuView))]
public partial class FlyoutTab_WOsViewFilter : UserControl,IWOFlyOutFilterMenuView,IPartImportsSatisfiedNotification
No Imports in the Containing view as it is a concrete instance.
Thanks
Greg
You can force the composition when you have an instance like this:
Import the namespace System.ComponentModel.Composition (to include the extension method SatisfyImportsOnce
Create a CompositionContainer
Call SatisfyImportsOnce on the composition Container and supply it the instance of the usercontrol.
Take care,
Martin
I defined in my code two classes: a "Person" class with public "Age" and "Name" property, and a "People" class that inherits from Generic.List(of T).
The code for People class is as followed:
Public Class People
Inherits Collections.Generic.List(Of Person)
...
End Class
What I want to achieve is to directly initialize the People class, and add individual Person to it in XAML, i.e.:
<local:People x:Key="Familty">
<local:Person Age="11" Name="John" />
<local:Person Age="12" Name="John2" />
...
</local:People>
But I keep getting an error in XAML saying:
The type 'People' does not support direct content.
Any idea as for how to solve this problem?
Thank you very much!
What exactly do you want to do?
It seams that you try set a content to a control (that must be a ContentControl / or inherited class). Also please notice that you are setting the Content in xaml, that means the it must be a UIElement at least.
If you want to represent a list of people, please set a dataTemplate to that dataType and have a visual representation, then set the ItemsSource (of People which should be a items Control) to a list (or observable collection) of people.
You should consider separating the UI from the model.
So, what exactly are you trying to do ?
I am new to WPF and have created a WPF User Control Library
I added a Base class that looks like this
public class TControl : UserControl
{
}
and want all of my controls to inherit from it.
I have a Control called Notification which looks like
public partial class Notification : TControl
{
public Notification()
{
InitializeComponent();
}
Works fine except when ever i recompile the hidden partial class where InitializeComponent() is defined gets regenerated and inherits from System.Windows.Controls.UserControl
this gives me an
Partial declarations of 'Twac.RealBoss.UserControls.Notification' must not specify different base classes
error,
is there anyway to force the generated class to inherit from my base class?
Your XAML file probably has:
<UserControl x:Class="YourNamespace.Notification" .... >
Try changing this to:
<Whatever:TControl x:Class="YourNamespace.Notification" xmlns:Whatever="clr-namespace:YourNamespace" />
The error you are getting is because the use of UserControl in the XAML tells the compiler to produce a partial class inheriting from UserControl, instead of inheriting from your class.
You can completely remove the ": TControl":
public partial class Notification : TControl
{
}
and write:
public partial class Notification
{
}
instead, since the base class is defined in the XAML part, as Paul wrote.