Question

Hi I have a beginner problem. I have shell (it is wpf window) and in this shell is screen (it is an user control / view model).

I would like open new window from view model, not show user control in shell.

So I create new window - ChatView

<Window x:Class="Spirit.Views.ChatView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:extToolkit="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit.Extended" Title="ChatView" Height="545" Width="763">
    <Grid Margin="4,4,4,4">
     </Grid>
</Window>

Export ChatViewModel with MEF.

 public interface IChatViewModel
    {

    }

    [Export("ChatScreen",typeof(IChatViewModel))]
    public class ChatViewModel
    {

    }

In view model I have this method:

With ShowScreen class help me Mr.Marco Amendola. It look likes this:

public class ShowScreen : IResult
    {
        readonly Type _screenType;
        readonly string _name;

        [Import]
        public IShellViewModel Shell { get; set; }

        Action<object> _initializationAction = screen => { };

        public ShowScreen InitializeWith<T>(T argument) 
        {
            _initializationAction = screen =>
                                        {
                                            var initializable = screen as IInitializable<T>;
                                            if (initializable != null)
                                                initializable.Initialize(argument);
                                        };
        return this;
        } 

        public ShowScreen(string name)
        {
            _name = name;
        }

        public ShowScreen(Type screenType)
        {
            _screenType = screenType;
        }

        public void Execute(ActionExecutionContext context)
        {
            var screen = !string.IsNullOrEmpty(_name)
                ? IoC.Get<object>(_name)
                : IoC.GetInstance(_screenType, null);

            _initializationAction(screen);

            Shell.ActivateItem(screen);
            Completed(this, new ResultCompletionEventArgs());
        }

        public event EventHandler<ResultCompletionEventArgs> Completed = delegate { };

        public static ShowScreen Of<T>()
        {
            return new ShowScreen(typeof(T));
        }
    }

My problem is if I try show new window it doesn’t works, it works only if I show new user control in shell(window).

I would like achieve behavior something like in skype. You have a main window with listbox, you double clicked on item and it show new chat window.

Main window can publish with EventAggregator on chat window and also chat window can publish on main window. This is my goal.

I know that I can not use class ShowScreen on showing new Window. I would like to know what is correct way to create new window from view model and inject event aggregator to this vie model.

Any advice? Thank for your help and time.

Was it helpful?

Solution

Have you looked at WindowManager.Show or WindowManager.ShowDialog? Rob has a sample at http://caliburnmicro.codeplex.com/wikipage?title=The%20Window%20Manager. You can inject this dependency into your view model as IWindowManager.

OTHER TIPS

I'm using this. Maybe could save a question about "where's the code ?".

DialogHelper:

public class DialogHelper
{
    public void ShowDialog<T>(params Object[] param) where T : class
    {
        var windowManager = new WindowManager();
        T viewModel = Activator.CreateInstance(typeof(T), param) as T;

        windowManager.ShowWindow(viewModel);
    } 
}

How to use:
Without constructor parameter:

Dialog.ShowDialog<TestTableViewModel>();

With constructor paramater:

Dialog.ShowDialog<TestTableViewModel>(dt);

Note that I'm not using MEF

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top