Skip to main content

DevExpress v24.2 Update — Your Feedback Matters

Our What's New in v24.2 webpage includes product-specific surveys. Your response to our survey questions will help us measure product satisfaction for features released in this major update and help us refine our plans for our next major release.

Take the survey Not interested

ViewLocator

  • 2 minutes to read

The ViewLocator class implements the IViewLocator interface and provides a simple composition mechanism to locate Views by their type names.

You can use the functionality provided by the ViewLocator class to locate a View from another object if this object does not have a direct link to the View.

To retrieve a View by its type name, you typically call the IViewLocator.ResolveView method on the static Default property:

ViewLocator.Default.ResolveView(viewShortTypeName);

where viewShortTypeName is the short type name of the View to be returned.

By default, the Default property contains a ViewLocator object capable of locating Views within the entry assembly. If you want the ViewLocator to locate Views within two or more assemblies, create a new ViewLocator object via a constructor that takes a list of assemblies as a parameter. Then, assign the created object to the Default property.

public partial class App : Application {
    public App() {
        ViewLocator.Default = new ViewLocator(new Assembly[] { 
            typeof(App).Assembly, 
            typeof(ViewA).Assembly }
        );
    }
}

If the default ViewLocator implementation does not meet your requirements, you can implement a custom ViewLocator. For instance, the following ViewLocator retrieves a View from an assembly loaded on demand.

ViewLocator.Default = new MainViewLocator();

public class MainViewLocator : IViewLocator {
    public string GetViewTypeName(Type type) {
        return type?.FullName;
    }

    public object ResolveView(string name) {
        Type t = ResolveViewType(name);
        return Activator.CreateInstance(t);
    }
    public Type ResolveViewType(string name) {
        if(name == "ViewA") {
            Assembly assembly = Assembly.LoadFrom(@"ExampleViews.dll");
            return assembly.GetType("ExampleViews.ViewA");
        }
        return null;
    }
}

ViewLocator is used by Services. It provides the capability to create Views. Please review the following article for more details: View creation mechanisms.