ViewLocator.Generator.Common
2025.8.4.737-manual
dotnet add package ViewLocator.Generator.Common --version 2025.8.4.737-manual
NuGet\Install-Package ViewLocator.Generator.Common -Version 2025.8.4.737-manual
<PackageReference Include="ViewLocator.Generator.Common" Version="2025.8.4.737-manual" />
<PackageVersion Include="ViewLocator.Generator.Common" Version="2025.8.4.737-manual" />
<PackageReference Include="ViewLocator.Generator.Common" />
paket add ViewLocator.Generator.Common --version 2025.8.4.737-manual
#r "nuget: ViewLocator.Generator.Common, 2025.8.4.737-manual"
#:package ViewLocator.Generator.Common@2025.8.4.737-manual
#addin nuget:?package=ViewLocator.Generator.Common&version=2025.8.4.737-manual&prerelease
#tool nuget:?package=ViewLocator.Generator.Common&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.
Product | Versions 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. |
-
.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 |