Open New Screen From Actual Screen

Dec 19, 2010 at 4:05 PM

Hi, I need open new view-model  from actual view-model and pass some object as argument to construtor of new view model. But I don’t know how do it. I use coroutines on creation new view-model.

 

Shell-view-model:

    public interface IShellViewModel:IConductor
    {
    }

    [Export(typeof(IShellViewModel))]
    public class ShellViewModel : Conductor<object>, IShellViewModel
    {
        private readonly LogOnViewModel _initScreen;

        [ImportingConstructor]
        public ShellViewModel(LogOnViewModel initScreen)
        {
            _initScreen = initScreen;
        }

        protected override void OnInitialize()
        {
            ActivateItem(_initScreen);
            base.OnInitialize();
        }
    }

LogOn screen, fro this screen I need create new screen and pass some object as arguments of construtor of new screen.

 

    [Export(typeof(LogOnViewModel))]
public class LogOnViewModel : Screen,ILogOnViewModel
{
[Import]
private IPokecConnection _pokecConn;

private PokecAccount _account;

public IEnumerable<IResult> LogOn(string nick, string password)
{
yield return new ShowScreen("LoggingScreen");// here I need pass som arguments to constructor of LoggingViewModel
_account = _pokecConn.LogOn(nick, password);

if (_account != null)
{
yield return new ShowScreen("MessengerScreen");// here I need pass som arguments to constructor of MessengerViewModel

            }
else { //yield return new ShowScreen("ErrorScress") } }

Messenger screen:

    [Export("MessengerScreen",typeof(MessengerViewModel))]
    public class MessengerViewModel : Screen,IMessengerViewModel
    {}

 

Show class:

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

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

        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);

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

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

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

Any advance?

Dec 19, 2010 at 7:49 PM
Edited Dec 19, 2010 at 7:50 PM

Something along this line usually works well for me: 

 

public interface IInitializable<T> {
	void Initialize(T argument);
}

public class MessengerViewModel : Screen,IMessengerViewModel, IInitializable<MyInitArgument> 
{
	public void Initialize(MyInitArgument argument) {
		//initialize MessengerViewModel
		...
	}
}

public class ShowScreen : IResult
{
	...
	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 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());
	}
	...
}


//calling code:
public IEnumerable<IResult> LogOn(string nick, string password)
{
	...
	yield return new ShowScreen("MessengerScreen")
		.InitializeWith(new MyInitArgument{...});
	...
}

 

 

Obviously an Initialize function is weaker than a constructor parameter from a modeling point of view; you can enforce the call of Initialize with a runtime check, however, to avoid forgetting about it.

Dec 20, 2010 at 12:09 PM

Thank for help, I ame begginer in caliburn, mvvm and this help me a lot.