Skip to content

Simplifying IViewFor Interface #5

@chris84948

Description

@chris84948

First of all, thanks for this, it definitely helped me get my head around where I needed to go. Normally I'd make the changes and issue a PR, but in this case I was just grabbing some of the things you've done and rolling it my own way as well.

I had a quick suggestion on the IViewFor interface. It seems like you don't really need to have the ViewModel property in that interface. I simplified it to just -

public interface IViewFor<T> : IViewFor where T : IViewModel
{ }

Then you can change NavigationService.IntantiateView to this -

IViewFor<T> InstantiateView<T>(T viewModel) where T : class, IViewModel
{
	// Figure out what type the view model is
	var viewModelType = viewModel.GetType();

	// look up what type of view it corresponds to
	var viewType = _viewModelViewDictionary[viewModelType];

	// instantiate it
	var view = (IViewFor<T>)Activator.CreateInstance(viewType);

	view.BindingContext = viewModel;

	return view;
}

You're still getting the binding context in there, but now you don't have to go and add that property to every view.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions