unable to cast view model to view model interface

Dec 29, 2010 at 3:59 PM
Edited Dec 29, 2010 at 4:18 PM

Hi, I have problem with create and show new window with window manager from shell view model.

My shell is here:


public class ShellViewModel : Conductor<object>, IShellViewModel
private IWindowManager _windowManager;

public ShellViewModel(IWindowManager windowManager)
_windowManager = windowManager;

public void ShowChatScreen(DetailData oponent, BitmapImage avatarImage)
var chatScreen = IoC.Get<IChatViewModel>();

//here I get error



I use method ShowChatScreen in main view model - messenger view model. If I call method ShowChatScreen I get this error:

Unable to cast object of type 'Spirit.ViewModels.ChatViewModel' to type 'Spirit.ViewModels.IChatViewModel'.


   at Caliburn.Micro.IoC.Get[T]() in C:\Users\Jan\Documents\Visual Studio 2010\Projects\C#\Pokec_Messenger\ver.beta\Pokec__Messenger\Caliburn_Micro_Completed\CaliburnMicro\src\Caliburn.Micro.Silverlight\IoC.cs:line 33
   at Spirit.ViewModels.ShellViewModel.ShowChatScreen(DetailData oponent, BitmapImage avatarImage) in C:\Users\Jan\Documents\Visual Studio 2010\Projects\C#\Pokec_Messenger\ver.beta\Pokec__Messenger\Spirit_v1.0\ViewModels\ShellViewModel.cs:line 44
   at Spirit.ViewModels.MessengerViewModel.OpenChatScreen() in C:\Users\Jan\Documents\Visual Studio 2010\Projects\C#\Pokec_Messenger\ver.beta\Pokec__Messenger\Spirit_v1.0\ViewModels\MessengerViewModel.cs:line 100

Messenger view model class look like this:


public class MessengerViewModel : Screen, IViewModelIdentity,
IMessengerViewModel, IHandle<Rp>

private IShellViewModel _shell;

public void OpenChatScreen()
_shell.ShowChatScreen(_service.DetailData(Account, _selectedFriend.Value.Nick), AvatarImage);


New new model class- chat screen is here:


    public interface IChatViewModel
void Initialize(DetailData oponent, BitmapImage avatarImage);

public class ChatViewModel : Screen, IViewModelIdentity, IHandle<Rp>

public void Initialize(DetailData oponent, BitmapImage avatarImage)
Oponent = oponent;

AvatarImage = avatarImage;

_yourRp.AzetId = Oponent.Info

ScreenIdentity = Oponent.Info


What I do wrong? Thank for advice. ChatViewModel is WPF Window.

Dec 29, 2010 at 4:07 PM

Your ChatViewModel does not actually implement the interface. Add the interface and it all should work.

Dec 29, 2010 at 4:07 PM
Edited Dec 29, 2010 at 4:07 PM

You are not implementing IChatViewModel, so even if you are implementing the Initialize method, you cannot cast the retrieved object to the interface.

Note that the error is not on the commented line, but the one before, since IoC.Get<T>() fails during cast.


Edit: Rob was faster! :)

Dec 29, 2010 at 4:36 PM

Mr. Einsenberg, thank....I'm the shame, it's an error of carelessness.

Dec 29, 2010 at 5:02 PM

No worries, I've made the same mistake multiple times myself :)

Dec 29, 2010 at 6:37 PM

I just learning...in school we have only C, my teacher can’t give me any advice about wpf, C# ...they live in stone age :)