ViewLocator.Generator
2025.8.4.737-manual
dotnet add package ViewLocator.Generator --version 2025.8.4.737-manual
NuGet\Install-Package ViewLocator.Generator -Version 2025.8.4.737-manual
<PackageReference Include="ViewLocator.Generator" Version="2025.8.4.737-manual"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
<PackageVersion Include="ViewLocator.Generator" Version="2025.8.4.737-manual" />
<PackageReference Include="ViewLocator.Generator"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
paket add ViewLocator.Generator --version 2025.8.4.737-manual
#r "nuget: ViewLocator.Generator, 2025.8.4.737-manual"
#:package ViewLocator.Generator@2025.8.4.737-manual
#addin nuget:?package=ViewLocator.Generator&version=2025.8.4.737-manual&prerelease
#tool nuget:?package=ViewLocator.Generator&version=2025.8.4.737-manual&prerelease
ViewLocator.Generator
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.UserView
→MyApp.ViewModels.UserViewModel
MyApp.Views.Products.ProductView
→MyApp.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:
UserPage
→UserPageViewModel
ProductPage
→ProductPageViewModel
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:
ViewModels
→Views
- Suffix transformation:
ViewModel
→View
(with special handling forWindowViewModel
→Window
) - 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.
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- ViewLocator.Generator.Common (>= 2025.8.4.737-manual)
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 |