ViewBindings.SourceGenerator
0.1.0
See the version list below for details.
dotnet add package ViewBindings.SourceGenerator --version 0.1.0
NuGet\Install-Package ViewBindings.SourceGenerator -Version 0.1.0
<PackageReference Include="ViewBindings.SourceGenerator" Version="0.1.0"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
paket add ViewBindings.SourceGenerator --version 0.1.0
#r "nuget: ViewBindings.SourceGenerator, 0.1.0"
// Install ViewBindings.SourceGenerator as a Cake Addin #addin nuget:?package=ViewBindings.SourceGenerator&version=0.1.0 // Install ViewBindings.SourceGenerator as a Cake Tool #tool nuget:?package=ViewBindings.SourceGenerator&version=0.1.0
ViewBinding.SourceGenerator
The ViewBinding.SourceGenerator allows you to bind view models to views within a generated ResourceDictionary.
Installation
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageReference Include="ViewBindings.SourceGenerator" Version="0.1.0" PrivateAssets="all" ExcludeAssets="runtime" />
Usage
To bind a view model to a view, add the [ViewBinding]
attribute to the view model, specifying the view type as a named argument. If no view type is provided, the attribute will search for the first view with a matching name based on a predetermined naming convention.
using ViewBindings.SourceGenerator.Contracts.Attributes;
namespace ViewBindings.SourceGenerator.Demo.ViewModels;
[ViewBinding]
public class ViewModel : ViewModelBase
{
}
Generates a class GeneratedViewBindings.g.cs
// <auto-generated/>
#pragma warning disable
#nullable enable
using System;
using System.Windows;
namespace ViewBindings.SourceGenerator.Demo.Resources;
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public class GeneratedViewBindings : ResourceDictionary
{
public GeneratedViewBindings()
{
AddDataTemplate(typeof(global::ViewBindings.SourceGenerator.Demo.ViewModels.ViewModel), typeof(global::ViewBindings.SourceGenerator.Demo.Views.View));
}
void AddDataTemplate(Type viewModel, Type view)
{
var dataTemplate = new DataTemplate(viewModel)
{VisualTree = new FrameworkElementFactory(view)};
Add(new DataTemplateKey(viewModel), dataTemplate);
}
}
The view model is mapped to the following view based on the predefined naming convention, where the view model's name is used to identify the corresponding view. Specifically, "ViewModel" is replaced with "View" and the first matching view found in the solution is selected.
View.xaml
<UserControl x:Class="ViewBindings.SourceGenerator.Demo.Views.View"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
</Grid>
</UserControl>
View.xaml.cs
using System.Windows.Controls;
namespace ViewBindings.SourceGenerator.Demo.Views;
/// <summary>
/// Interaction logic for View2.xaml
/// </summary>
public partial class View : UserControl
{
public View()
{
InitializeComponent();
}
}
To use the GeneratedViewBindings
calss, simply put it in App.xaml
<Application x:Class="ViewBindings.SourceGenerator.Demo.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:resources="clr-namespace:ViewBindings.SourceGenerator.Demo.Resources"
StartupUri="MainWindow.xaml">
<Application.Resources>
<resources:GeneratedViewBindings />
</Application.Resources>
</Application>
Specify view type
If you choose not to use the predefined naming convention, you can specify the view to use by setting ViewType
to the type of the desired view (e.g. typeof(View)
). This will result in the same GeneratedViewBindings
as in the example above.
using ViewBindings.SourceGenerator.Contracts.Attributes;
using ViewBindings.SourceGenerator.Demo.Views;
namespace ViewBindings.SourceGenerator.Demo.ViewModels;
[ViewBinding(ViewType = typeof(View))]
public class ViewModel : ViewModelBase
{
}
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. |
.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. |
This package has no dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.