Skip to content
This repository has been archived by the owner on Dec 18, 2017. It is now read-only.

loading views and view models from different assemblies #61

Open
raV720 opened this issue Nov 18, 2016 · 9 comments
Open

loading views and view models from different assemblies #61

raV720 opened this issue Nov 18, 2016 · 9 comments

Comments

@raV720
Copy link

raV720 commented Nov 18, 2016

The problem in details is described here:

http://stackoverflow.com/questions/39044800/mvvmcross-loading-views-and-view-models-from-different-assemblies

Git with source: https://bitbucket.org/equo/mvvmcross_test.git

Expected behavior
MVVMCROSS should be able to link View and ViewModel by naming convention.

Actual behavior
MVVMCROSS does not link View and ViewModel by naming convention when are in separate assemblies.

Configuration
Version: 4.4
Platform: Windows Store (other patforms not tested)

@Cheesebaron
Copy link
Member

I believe this is similar to #45

The presenter need to use the registered View Assemblies to look up Views, just like for ViewModels.

@raV720
Copy link
Author

raV720 commented Nov 19, 2016

Could you be more specific? As I understand Views and ViewModels are registred via overriding GetViewAssemblies() and GetViewModelAssemblies(). And it is done correctly in provided source https://bitbucket.org/equo/mvvmcross_test.git. Then what is wrong?

@Cheesebaron
Copy link
Member

The method in the presenter which locates the View.

@raV720
Copy link
Author

raV720 commented Nov 21, 2016

Could you provide some example?

@Cheesebaron
Copy link
Member

@raV720
Copy link
Author

raV720 commented Nov 21, 2016

protected virtual Type GetPageType(MvxViewModelRequest request)
{
	if (_viewFinder == null)
		_viewFinder = Mvx.Resolve<IMvxViewsContainer> ();

	try
	{
		return _viewFinder.GetViewType (request.ViewModelType);
	}
	catch(KeyNotFoundException) 
	{
		var pageName = GetPageName(request);
		return request.ViewModelType.GetTypeInfo().Assembly.CreatableTypes()
			.FirstOrDefault(t => t.Name == pageName);
	}
}

The above method (from MvxFormsPageLoader class) assumes that View is in ViewModel assembly. Will it be somehow fixed?

@Cheesebaron
Copy link
Member

Yes, when someone rewrites it to get rid of that assumption.

@tofutim
Copy link

tofutim commented Dec 7, 2016

Would we have to create a way for the user to specify View location?

@bruzkovsky
Copy link

Doing it the Mvx-Way, I guess, is to override the CreateViewsContainer method in your Setup.cs:

protected override IMvxAndroidViewsContainer CreateViewsContainer(Context applicationContext)
{
    var viewsContainer = (IMvxViewsContainer) base.CreateViewsContainer(applicationContext);
    var viewModelTypes =
        typeof(MainViewModel).GetTypeInfo().Assembly.CreatableTypes().Where(t => t.Name.EndsWith("ViewModel")).ToDictionary(t => t.Name.Remove(t.Name.LastIndexOf("ViewModel", StringComparison.Ordinal)));
    var viewTypes =
        typeof(MainPage).GetTypeInfo().Assembly.CreatableTypes().Where(t => t.Name.EndsWith("Page")).ToDictionary(t => t.Name.Remove(t.Name.LastIndexOf("Page", StringComparison.Ordinal)));
    foreach (var viewModelTypeAndName in viewModelTypes)
    {
        Type viewType;
        if (viewTypes.TryGetValue(viewModelTypeAndName.Key, out viewType))
            viewsContainer.Add(viewModelTypeAndName.Value, viewType);
    }
    return (IMvxAndroidViewsContainer) viewsContainer;
}

You can also specify your own naming convention here.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Development

No branches or pull requests

4 participants