I discovered that Window or UserControl can not inherit Caliburn's Screen base class. Am I forced to implement IScreen interface to every window/user control in my WPF application?
Imports Caliburn.Micro
Namespace Views
Public Class CustomView
Inherits Screen
End Class
End Namespace
Base class 'Screen' specified for class 'CustomView' cannot be
different from the base class 'UserControl' of one of its other
partial types.
Caliburn's Screen base class is meant to be used on view models.
Imports Caliburn.Micro
Namespace ViewModels
Public Class CustomViewModel
Inherits Screen
'...'
End Class
End Namespace
And then used to bind derived view models to the views
Imports ViewModels
Namespace Views
Public Class CustomView
Inherits Window
Public Sub New()
'...'
Dim viewModel As New CustomViewModel()
DataContext = viewModel
End Sub
End Class
End Namespace
Reference Caliburn.Micro Documentation: Screens, Conductors and Composition
Related
I am creating a login form that will be used by many different applications. The login will always have the same logic, so I'd like to bind a viewmodel and do all logic there (Retrieving login info from database, etc). I created a new UserControl, MainView and a ViewModel, MainViewModel both of which are in a Login namespace.
The form continues to run everything in the code-behind, but nothing in the VM.
Is there another way of binding that I am not aware of?
Code-Behind MainView.Xaml.vb
Imports Caliburn.Micro
Namespace Login
Public Class MainView
Public Sub New()
MsgBox("TEST code-behind")
End Sub
End Class
End Namespace
VM MainViewModel.vb
Imports Caliburn.Micro
Namespace Login
Public Class MainViewModel
Inherits PropertyChangedBase
Public Sub New()
MsgBox("TEST ViewModel")
End Sub
End Class
End Namespace
Xaml
<UserControl x:Class="Login.MainView"
xmlns:cal="http://www.caliburnproject.org"
xmlns:local="clr-namespace:cLogin.Login"
cal:Bind.Model="cLogin.Login.MainViewModel" (not sure if needed due to naming)
... >
EDIT
This is how I have added the UserControl as a separate window before the user is logged in, I can see the content, but none of the properties inside the ViewModel bind
Dim login As New Window
With login
.WindowStyle = WindowStyle.None
.ResizeMode = ResizeMode.NoResize
.SizeToContent = SizeToContent.WidthAndHeight
.Content = New MainView()
End With
login.ShowDialog()
Since you are creating the window explicitly, you also need to explicitly set its DataContext:
Dim login As New Window
With login
.WindowStyle = WindowStyle.None
.ResizeMode = ResizeMode.NoResize
.SizeToContent = SizeToContent.WidthAndHeight
.Content = New MainView()
.DataContext = New MainViewModel()
End With
You should also bind the attached Bind.Model property to the DataContext in the view:
cal:Bind.Model="{Binding}"
It looks like Caliburn.Micro's WindowManager have problem with WPF Windows, that have constructor parameter although that parameter's type is registered in Caliburn.Micro IoC. Is there better way to put that needed parameter into Window except using stinking service locator IoC.Get(Of T) in constructor?
Namespace Views
Class MainWindowView
Private _eventAggregator As IEventAggregator
Public Sub New(eventAggregator As IEventAggregator)
_eventAggregator = eventAggregator
End Sub
End Class
End Namespace
Sub ShowMainWindowView()
'Everything is correctly registered in Ioc...
Dim windowManager As New WindowManager
Dim viewModel As New MainWindowViewModel
windowManager.ShowDialog(viewModel)
'Exception is thrown about absence of parameterless constructor of MainWindowView
End Sub
I tried to reproduce this problem again in new, clear project and it works - it is only needed to have type of view (Window) registered in IoC container and WindowManager automatically injects required dependencies in view's constructor.
I have a Class in my main WPF application which has a Property defined in the class as follows:
Public Class AppExample
Public PropertyName As CollectionViewSource
The project solution also inherits a Class Library (separate project but included in the solution and using the Inherits statement) - in the Class Library I want to do the same thing but I get an error.
Public Class ClassLibraryExample
Public PropertyName as CollectionViewSource
this results in:
Type 'CollectionViewSource' is not defined
How do I fix this?
Add the refernce of PresentationFramework.dll to your class library. It has namespace System.Windows.Data which contains CollectionViewSource
When you are using CollectionViewSource you have to use Data namespace(System.Windows.Data).
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
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.