ViewLocator.Generator 2025.8.4.737-manual

This is a prerelease version of ViewLocator.Generator.
dotnet add package ViewLocator.Generator --version 2025.8.4.737-manual
                    
NuGet\Install-Package ViewLocator.Generator -Version 2025.8.4.737-manual
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ViewLocator.Generator" Version="2025.8.4.737-manual">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ViewLocator.Generator" Version="2025.8.4.737-manual" />
                    
Directory.Packages.props
<PackageReference Include="ViewLocator.Generator">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ViewLocator.Generator --version 2025.8.4.737-manual
                    
#r "nuget: ViewLocator.Generator, 2025.8.4.737-manual"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package ViewLocator.Generator@2025.8.4.737-manual
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=ViewLocator.Generator&version=2025.8.4.737-manual&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=ViewLocator.Generator&version=2025.8.4.737-manual&prerelease
                    
Install as a Cake Tool

ViewLocator.Generator

CI NuGet NuGet

A C# source generator that automatically implements static view locator for Avalonia without using reflection.

Originally from wieslawsoltes/StaticViewLocator

Usage

Add NuGet package references to your project:

<PackageReference Include="ViewLocator.Generator" Version="0.0.1">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="ViewLocator.Generator.Common" Version="0.0.1" />

Annotate view locator class with [GenerateViewLocator] attribute, make class partial and implement Build using s_views dictionary to retrieve views for data objects.

using ViewLocator.Generator.Common;

[GenerateViewLocator]
public partial class ViewLocator : IDataTemplate
{
    public Control? Build(object? data)
    {
        if (data is null)
        {
            return null;
        }

        var type = data.GetType();

        if (s_views.TryGetValue(type, out var func))
        {
            return func.Invoke();
        }

        throw new Exception($"Unable to create view for type: {type}");
    }

    public bool Match(object? data)
    {
        return data is ViewModelBase;
    }
}

Source generator will generate the s_views dictionary similar to below code using convention based on ViewModel suffix for view models substituted to View suffix.

public partial class ViewLocator
{
	private static Dictionary<Type, Func<Control>> s_views = new()
	{
		[typeof(GenerateViewLocatorDemo.ViewModels.MainWindowViewModel)] = () => new GenerateViewLocatorDemo.Views.MainWindow(),
		[typeof(GenerateViewLocatorDemo.ViewModels.TestViewModel)] = () => new GenerateViewLocatorDemo.Views.TestView(),
	};
}

Configuration Options

The GenerateViewLocator attribute supports several properties to customize the view location behavior:

ViewToViewModelNamespaceRule

Transforms view namespaces to view model namespaces using a mapping rule.

Format: "ViewNamespace -> ViewModelNamespace"

[GenerateViewLocator(ViewToViewModelNamespaceRule = "Views -> ViewModels")]
public partial class ViewLocator : IDataTemplate
{
    // Implementation...
}

Example transformations:

  • MyApp.Views.UserViewMyApp.ViewModels.UserViewModel
  • MyApp.Views.Products.ProductViewMyApp.ViewModels.Products.ProductViewModel

ViewToViewModelSuffixRule

Transforms view suffixes to view model suffixes using a mapping rule.

Format: "ViewSuffix -> ViewModelSuffix"

[GenerateViewLocator(ViewToViewModelSuffixRule = "Page -> PageViewModel")]
public partial class ViewLocator : IDataTemplate
{
    // Implementation...
}

Example transformations:

  • UserPageUserPageViewModel
  • ProductPageProductPageViewModel

IncludeNamespaces

Only includes ViewModels from the specified namespaces. This is useful when you want to limit view location to specific parts of your application.

[GenerateViewLocator(IncludeNamespaces = new[] { "MyApp.ViewModels", "SharedLib.ViewModels" })]
public partial class ViewLocator : IDataTemplate
{
    // Implementation...
}

ExcludeNamespaces

Excludes ViewModels from the specified namespaces. This is particularly useful for excluding framework or third-party ViewModels.

[GenerateViewLocator(ExcludeNamespaces = new[] { "Avalonia", "System" })]
public partial class ViewLocator : IDataTemplate
{
    // Implementation...
}

Combined Configuration

You can combine multiple properties for complex scenarios:

[GenerateViewLocator(
    ViewToViewModelNamespaceRule = "Views -> ViewModels",
    ViewToViewModelSuffixRule = "View -> ViewModel",
    ExcludeNamespaces = new[] { "Avalonia", "System" },
    IncludeNamespaces = new[] { "MyApp" }
)]
public partial class ViewLocator : IDataTemplate
{
    // Implementation...
}

Default Behavior

When no configuration is specified, the default behavior is:

  • Namespace transformation: ViewModelsViews
  • Suffix transformation: ViewModelView (with special handling for WindowViewModelWindow)
  • Include all: All accessible ViewModels are included (except when explicitly excluded)

Examples

See the Examples directory for comprehensive examples demonstrating each configuration option.

Common Use Cases

1. Standard MVVM with Framework Exclusion

[GenerateViewLocator(ExcludeNamespaces = new[] { "Avalonia" })]

2. Custom Naming Convention

[GenerateViewLocator(
    ViewToViewModelNamespaceRule = "UI -> Business.ViewModels",
    ViewToViewModelSuffixRule = "Control -> ControlViewModel"
)]

3. Multi-Project Architecture

[GenerateViewLocator(
    IncludeNamespaces = new[] { "MyApp.Core.ViewModels", "MyApp.Modules.ViewModels" },
    ExcludeNamespaces = new[] { "ThirdParty" }
)]

License

ViewLocator.Generator is licensed under the MIT license. See LICENSE file for details.

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2025.8.4.737-manual 129 8/4/2025
2025.8.4.531-manual 119 8/4/2025
2025.8.4.334-manual 115 8/4/2025
2025.8.4.314-pr8 116 8/4/2025
2025.8.4.234-pr7 118 8/4/2025
2025.8.4.208-pr6 105 8/4/2025
2025.8.1.1028-pr4 58 8/1/2025
2025.8.1.831-pr3 61 8/1/2025