Skip to content

Commit

Permalink
Merge pull request #203 from AvaloniaUI/return-viewLocator
Browse files Browse the repository at this point in the history
Return view locator
  • Loading branch information
Takoooooo authored Jun 14, 2023
2 parents b91ac29 + 1ae49f5 commit 92e0749
Show file tree
Hide file tree
Showing 32 changed files with 408 additions and 21 deletions.
16 changes: 16 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,14 @@ Available parameters:

*By default*: ReactiveUI

``-rvl, --remove-view-locator``

*Description*: Defines if your app will use default ViewLocator made by Avalonia Team or you are planning to use a custom one. Removing ViewLocator may be useful in code trimming scenarios. Default ViewLocator is not trimming-friendly.

*Options*: **true**, **false**

*By default*: false

``--no-restore``

*Description*: If specified, skips the automatic restore of the project on create.
Expand All @@ -143,6 +151,14 @@ Available parameters:

*By default*: true

``-rvl, --remove-view-locator``

*Description*: Defines if your app will use default ViewLocator made by Avalonia Team or you are planning to use a custom one. Removing ViewLocator may be useful in code trimming scenarios. Default ViewLocator is not trimming-friendly.

*Options*: **true**, **false**

*By default*: false

# Creating a new Window

To create a new `Window` called `MyNewWindow`, in the namespace `MyApp` run:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
"AvaloniaVersion": {
"longName": "avalonia-version"
},
"RemoveViewLocator": {
"longName": "remove-view-locator"
},
"MVVMToolkit": {
"longName": "mvvm"
},
Expand Down
7 changes: 7 additions & 0 deletions templates/csharp/app-mvvm/.template.config/ide.host.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@
"text": "Use Compiled Bindings"
},
"isVisible": true
},
{
"id": "RemoveViewLocator",
"name": {
"text": "Remove View Locator"
},
"isVisible": true
}
]
}
53 changes: 44 additions & 9 deletions templates/csharp/app-mvvm/.template.config/template.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@
"defaultValue": "true",
"isEnabled": "(AvaloniaVersion != \"0.10.21\")"
},
"RemoveViewLocator": {
"type": "parameter",
"description": "Defines if your app will use default ViewLocator made by Avalonia Team or you are planning to use a custom one. Removing ViewLocator may be useful in code trimming scenarios. Default ViewLocator is not trimming-friendly.",
"datatype": "bool",
"displayName": "Remove Avalonia ViewLocator",
"defaultValue": "false"
},
"HostIdentifier": {
"type": "bind",
"binding": "HostIdentifier"
Expand All @@ -100,21 +107,49 @@
{
"modifiers": [
{
"condition": "(AvaloniaStableChosen)",
"condition": "(RemoveViewLocator && AvaloniaStableChosen)",
"exclude": [
"App.v11.axaml"
]
"App.axaml",
"App.v11.axaml",
"App.v11.RemoveViewLocator.axaml",
"ViewLocator.cs"
],
"rename": {
"App.RemoveViewLocator.axaml": "App.axaml"
}
},
{
"condition": "(!AvaloniaStableChosen)",
"condition": "(!RemoveViewLocator && AvaloniaStableChosen)",
"exclude": [
"App.axaml"
"App.v11.axaml",
"App.RemoveViewLocator.axaml",
"App.v11.RemoveViewLocator.axaml"
]
},
{
"condition": "(RemoveViewLocator && !AvaloniaStableChosen)",
"exclude": [
"App.axaml",
"App.v11.axaml",
"App.RemoveViewLocator.axaml",
"ViewLocator.cs"
],
"rename": {
"App.v11.RemoveViewLocator.axaml": "App.axaml"
}
},
{
"condition": "(!RemoveViewLocator && !AvaloniaStableChosen)",
"exclude": [
"App.axaml",
"App.RemoveViewLocator.axaml",
"App.v11.RemoveViewLocator.axaml"
],
"rename": {
"App.v11.axaml": "App.axaml"
}
}
],
"rename": {
"App.v11.axaml": "App.axaml"
}
]
}
],
"primaryOutputs": [
Expand Down
8 changes: 8 additions & 0 deletions templates/csharp/app-mvvm/App.RemoveViewLocator.axaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Application xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="AvaloniaAppTemplate.App">

<Application.Styles>
<FluentTheme Mode="Light"/>
</Application.Styles>
</Application>
5 changes: 5 additions & 0 deletions templates/csharp/app-mvvm/App.axaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
<Application xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:AvaloniaAppTemplate"
x:Class="AvaloniaAppTemplate.App">

<Application.DataTemplates>
<local:ViewLocator/>
</Application.DataTemplates>

<Application.Styles>
<FluentTheme Mode="Light"/>
</Application.Styles>
Expand Down
10 changes: 10 additions & 0 deletions templates/csharp/app-mvvm/App.v11.RemoveViewLocator.axaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Application xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="AvaloniaAppTemplate.App"
RequestedThemeVariant="Default">
<!-- "Default" ThemeVariant follows system theme variant. "Dark" or "Light" are other available options. -->

<Application.Styles>
<FluentTheme />
</Application.Styles>
</Application>
5 changes: 5 additions & 0 deletions templates/csharp/app-mvvm/App.v11.axaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
<Application xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="AvaloniaAppTemplate.App"
xmlns:local="using:AvaloniaAppTemplate"
RequestedThemeVariant="Default">
<!-- "Default" ThemeVariant follows system theme variant. "Dark" or "Light" are other available options. -->

<Application.DataTemplates>
<local:ViewLocator/>
</Application.DataTemplates>

<Application.Styles>
<FluentTheme />
</Application.Styles>
Expand Down
31 changes: 31 additions & 0 deletions templates/csharp/app-mvvm/ViewLocator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using Avalonia.Controls;
using Avalonia.Controls.Templates;
using AvaloniaAppTemplate.ViewModels;

namespace AvaloniaAppTemplate;

public class ViewLocator : IDataTemplate
{
#if (AvaloniaStableChosen)
public IControl Build(object data)
#else
public Control Build(object data)
#endif
{
var name = data.GetType().FullName!.Replace("ViewModel", "View");
var type = Type.GetType(name);

if (type != null)
{
return (Control)Activator.CreateInstance(type)!;
}

return new TextBlock { Text = "Not Found: " + name };
}

public bool Match(object data)
{
return data is ViewModelBase;
}
}
3 changes: 3 additions & 0 deletions templates/csharp/xplat/.template.config/dotnetcli.host.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
},
"UseCompiledBindings": {
"longName": "compiled-bindings"
},
"RemoveViewLocator": {
"longName": "remove-view-locator"
}
},
"usageExamples": [
Expand Down
7 changes: 7 additions & 0 deletions templates/csharp/xplat/.template.config/ide.host.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
"text": "Use Compiled Bindings"
},
"isVisible": true
},
{
"id": "RemoveViewLocator",
"name": {
"text": "Remove View Locator"
},
"isVisible": true
}
]
}
31 changes: 30 additions & 1 deletion templates/csharp/xplat/.template.config/template.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,40 @@
"displayName": "Use compiled Bindings",
"defaultValue": "true"
},
"RemoveViewLocator": {
"type": "parameter",
"description": "Defines if your app will use default ViewLocator made by Avalonia Team or you are planning to use a custom one. Removing ViewLocator may be useful in code trimming scenarios. Default ViewLocator is not trimming-friendly.",
"datatype": "bool",
"displayName": "Remove Avalonia ViewLocator",
"defaultValue": "false"
},
"HostIdentifier": {
"type": "bind",
"binding": "HostIdentifier"
}
},
"sources": [
{
"modifiers": [
{
"condition": "(RemoveViewLocator)",
"exclude": [
"AvaloniaTest/App.axaml",
"AvaloniaTest/ViewLocator.cs"
],
"rename": {
"App.RemoveViewLocator.axaml": "App.axaml"
}
},
{
"condition": "(!RemoveViewLocator)",
"exclude": [
"AvaloniaTest/App.RemoveViewLocator.axaml"
]
}
]
}
],
"primaryOutputs": [
{ "path": "AvaloniaTest/AvaloniaTest.csproj" },
{ "path": "AvaloniaTest.Android/AvaloniaTest.Android.csproj" },
Expand All @@ -60,7 +89,7 @@
"id": "editor",
"condition": "(HostIdentifier != \"dotnetcli\" && HostIdentifier != \"dotnetcli-preview\")",
"description": "Opens MainView and MainViewModel in the editor",
"manualInstructions": [ ],
"manualInstructions": [],
"actionId": "84C0DA21-51C8-4541-9940-6CA19AF04EE6",
"args": {
"files": "5;6"
Expand Down
10 changes: 10 additions & 0 deletions templates/csharp/xplat/AvaloniaTest/App.RemoveViewLocator.axaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Application xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="AvaloniaTest.App"
RequestedThemeVariant="Default">
<!-- "Default" ThemeVariant follows system theme variant. "Dark" or "Light" are other available options. -->

<Application.Styles>
<FluentTheme />
</Application.Styles>
</Application>
5 changes: 5 additions & 0 deletions templates/csharp/xplat/AvaloniaTest/App.axaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
<Application xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:AvaloniaTest"
x:Class="AvaloniaTest.App"
RequestedThemeVariant="Default">
<!-- "Default" ThemeVariant follows system theme variant. "Dark" or "Light" are other available options. -->

<Application.DataTemplates>
<local:ViewLocator/>
</Application.DataTemplates>

<Application.Styles>
<FluentTheme />
</Application.Styles>
Expand Down
34 changes: 34 additions & 0 deletions templates/csharp/xplat/AvaloniaTest/ViewLocator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using Avalonia.Controls;
using Avalonia.Controls.Templates;
using AvaloniaTest.ViewModels;

namespace AvaloniaTest;

public class ViewLocator : IDataTemplate
{
#if (AvaloniaStableChosen)
public IControl Build(object data)
#else
public Control Build(object data)
#endif
{
if (data is null)
return null;

var name = data.GetType().FullName!.Replace("ViewModel", "View");
var type = Type.GetType(name);

if (type != null)
{
return (Control)Activator.CreateInstance(type)!;
}

return new TextBlock { Text = name };
}

public bool Match(object? data)
{
return data is ViewModelBase;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
"AvaloniaVersion": {
"longName": "avalonia-version"
},
"RemoveViewLocator": {
"longName": "remove-view-locator"
},
"MVVMToolkit": {
"longName": "mvvm"
},
Expand Down
7 changes: 7 additions & 0 deletions templates/fsharp/app-mvvm/.template.config/ide.host.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@
"text": "Use Compiled Bindings"
},
"isVisible": true
},
{
"id": "RemoveViewLocator",
"name": {
"text": "Remove View Locator"
},
"isVisible": true
}
]
}
Loading

0 comments on commit 92e0749

Please sign in to comment.