ViewLocator.Generator.Common 2025.8.4.737-manual

This is a prerelease version of ViewLocator.Generator.Common.
dotnet add package ViewLocator.Generator.Common --version 2025.8.4.737-manual
                    
NuGet\Install-Package ViewLocator.Generator.Common -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.Common" Version="2025.8.4.737-manual" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ViewLocator.Generator.Common" Version="2025.8.4.737-manual" />
                    
Directory.Packages.props
<PackageReference Include="ViewLocator.Generator.Common" />
                    
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.Common --version 2025.8.4.737-manual
                    
#r "nuget: ViewLocator.Generator.Common, 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.Common@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.Common&version=2025.8.4.737-manual&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=ViewLocator.Generator.Common&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.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on ViewLocator.Generator.Common:

Package Downloads
ViewLocator.Generator

A C# source generator that automatically implements static view locator for Avalonia without using reflection. Originally from wieslawsoltes/StaticViewLocator.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2025.8.4.737-manual 139 8/4/2025
2025.8.4.531-manual 120 8/4/2025
2025.8.4.334-manual 123 8/4/2025
2025.8.4.314-pr8 118 8/4/2025
2025.8.4.234-pr7 125 8/4/2025
2025.8.4.208-pr6 108 8/4/2025