Back to Devexpress

ViewLocator

wpf-17470-mvvm-framework-viewlocator.md

latest3.5 KB
Original Source

ViewLocator

  • Feb 08, 2021
  • 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:

csharp
ViewLocator.Default.ResolveView(viewShortTypeName);
vb
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.

csharp
public partial class App : Application {
    public App() {
        ViewLocator.Default = new ViewLocator(new Assembly[] { 
            typeof(App).Assembly, 
            typeof(ViewA).Assembly }
        );
    }
}
vb
Public Partial Class App
    Inherits Application
    Public Sub New()
        ViewLocator.[Default] = New ViewLocator(New Assembly() {
            GetType(App).Assembly, 
            GetType(ViewA).Assembly})
    End Sub
End Class

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.

csharp
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;
    }
}
vb
ViewLocator.[Default] = New MainViewLocator()

Public Class MainViewLocator
    Implements IViewLocator
    Public Function GetViewTypeName(ByVal type As Type) As String
        Return type?.FullName
    End Function

    Public Function ResolveView(name As String) As Object Implements IViewLocator.ResolveView
        Dim t As Type = ResolveViewType(name)
        Return Activator.CreateInstance(t)
    End Function
    Public Function ResolveViewType(name As String) As Type Implements IViewLocator.ResolveViewType
        If name = "ViewA" Then
            Dim _assembly As Assembly = Assembly.LoadFrom("ExampleViews.dll")
            Return _assembly.[GetType]("ExampleViews.ViewA")
        End If
        Return Nothing
    End Function
End Class

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