Tom Ceuppens
Tom Ceuppens

Reputation: 396

Unable to find view for viewmodel?

I'm trying to make a composition UI for a small website.

My building tree looks like this:

This last one is of sort IPodConductor, so a combination of a Screen ( the pagepod ) containing IPage (like MainPage, ContactPage .. )

My whole construction can find all my viewmodels and views following Caliburns convention, but not my MainPage.

The error is as following: "Cannot find view for Gymsport.Client.Pages.Main.MainPageViewModel"

My structure to the view is as following: Gymsport.Client.Pages.Main.MainPageView

Following the convention, caliburn should be able to locate my view... but it doens't.

Anybody any tips on to figure out or pointers for debugging this error.

Thanks in advance.

Upvotes: 3

Views: 1774

Answers (1)

gius
gius

Reputation: 9437

In C.M, there is additional logic for finding Views concerning words like Page, etc. (see here).

So you can either change your views to match the rules in C.M, remove word Page from your view models, or you can enforce custom simple view location with something like that:

ViewLocator.LocateTypeForModelType = (modelType, displayLocation, context) =>
{
    var viewTypeName = modelType.FullName.Substring(
        0,
        modelType.FullName.IndexOf("`") < 0
            ? modelType.FullName.Length
            : modelType.FullName.IndexOf("`")
        );

    viewTypeName = viewTypeName.Replace("Model", string.Empty);

    if (context != null)
    {
        viewTypeName = Regex.Replace(viewTypeName, "View$", string.Empty);
        viewTypeName += "." + context;
    }

    var viewType = (from assembly in AssemblySource.Instance
                    from type in assembly.GetExportedTypes()
                    where type.FullName == viewTypeName
                    select type).FirstOrDefault();

    return viewType;
};

Upvotes: 4

Related Questions