diff --git a/AvaloniaExample.sln b/AvaloniaExample.sln index 871a8dd..5c39a07 100644 --- a/AvaloniaExample.sln +++ b/AvaloniaExample.sln @@ -1,9 +1,21 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 -VisualStudioVersion = 17.14.36310.24 d17.14 +VisualStudioVersion = 17.14.36310.24 MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MVVMExample", "MVVMExample\MVVMExample.csproj", "{6F5088D9-CDF9-4A48-B100-58B06E21E99D}" +EndProject Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {6F5088D9-CDF9-4A48-B100-58B06E21E99D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6F5088D9-CDF9-4A48-B100-58B06E21E99D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6F5088D9-CDF9-4A48-B100-58B06E21E99D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6F5088D9-CDF9-4A48-B100-58B06E21E99D}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection diff --git a/MVVMExample/App.axaml b/MVVMExample/App.axaml new file mode 100644 index 0000000..2e2b9ac --- /dev/null +++ b/MVVMExample/App.axaml @@ -0,0 +1,15 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/MVVMExample/App.axaml.cs b/MVVMExample/App.axaml.cs new file mode 100644 index 0000000..3aaae71 --- /dev/null +++ b/MVVMExample/App.axaml.cs @@ -0,0 +1,48 @@ +using System.Linq; +using Avalonia; +using Avalonia.Controls.ApplicationLifetimes; +using Avalonia.Data.Core; +using Avalonia.Data.Core.Plugins; +using Avalonia.Markup.Xaml; +using MVVMExample.ViewModels; +using MVVMExample.Views; + +namespace MVVMExample +{ + public partial class App : Application + { + public override void Initialize() + { + AvaloniaXamlLoader.Load(this); + } + + public override void OnFrameworkInitializationCompleted() + { + if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) + { + // Avoid duplicate validations from both Avalonia and the CommunityToolkit. + // More info: https://docs.avaloniaui.net/docs/guides/development-guides/data-validation#manage-validationplugins + DisableAvaloniaDataAnnotationValidation(); + desktop.MainWindow = new MainWindow + { + DataContext = new MainWindowViewModel(), + }; + } + + base.OnFrameworkInitializationCompleted(); + } + + private void DisableAvaloniaDataAnnotationValidation() + { + // Get an array of plugins to remove + var dataValidationPluginsToRemove = + BindingPlugins.DataValidators.OfType().ToArray(); + + // remove each entry found + foreach (var plugin in dataValidationPluginsToRemove) + { + BindingPlugins.DataValidators.Remove(plugin); + } + } + } +} \ No newline at end of file diff --git a/MVVMExample/Assets/avalonia-logo.ico b/MVVMExample/Assets/avalonia-logo.ico new file mode 100644 index 0000000..f7da8bb Binary files /dev/null and b/MVVMExample/Assets/avalonia-logo.ico differ diff --git a/MVVMExample/MVVMExample.csproj b/MVVMExample/MVVMExample.csproj new file mode 100644 index 0000000..75e49fb --- /dev/null +++ b/MVVMExample/MVVMExample.csproj @@ -0,0 +1,28 @@ + + + WinExe + net8.0 + enable + true + app.manifest + true + + + + + + + + + + + + + + + None + All + + + + diff --git a/MVVMExample/Program.cs b/MVVMExample/Program.cs new file mode 100644 index 0000000..1a8185f --- /dev/null +++ b/MVVMExample/Program.cs @@ -0,0 +1,22 @@ +using System; +using Avalonia; + +namespace MVVMExample +{ + internal sealed class Program + { + // Initialization code. Don't use any Avalonia, third-party APIs or any + // SynchronizationContext-reliant code before AppMain is called: things aren't initialized + // yet and stuff might break. + [STAThread] + public static void Main(string[] args) => BuildAvaloniaApp() + .StartWithClassicDesktopLifetime(args); + + // Avalonia configuration, don't remove; also used by visual designer. + public static AppBuilder BuildAvaloniaApp() + => AppBuilder.Configure() + .UsePlatformDetect() + .WithInterFont() + .LogToTrace(); + } +} diff --git a/MVVMExample/ViewLocator.cs b/MVVMExample/ViewLocator.cs new file mode 100644 index 0000000..c860b0b --- /dev/null +++ b/MVVMExample/ViewLocator.cs @@ -0,0 +1,32 @@ +using System; +using Avalonia.Controls; +using Avalonia.Controls.Templates; +using MVVMExample.ViewModels; + +namespace MVVMExample +{ + public class ViewLocator : IDataTemplate + { + + public Control? Build(object? param) + { + if (param is null) + return null; + + var name = param.GetType().FullName!.Replace("ViewModel", "View", StringComparison.Ordinal); + 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; + } + } +} diff --git a/MVVMExample/ViewModels/MainWindowViewModel.cs b/MVVMExample/ViewModels/MainWindowViewModel.cs new file mode 100644 index 0000000..e2338bd --- /dev/null +++ b/MVVMExample/ViewModels/MainWindowViewModel.cs @@ -0,0 +1,7 @@ +namespace MVVMExample.ViewModels +{ + public partial class MainWindowViewModel : ViewModelBase + { + public string Greeting { get; } = "Welcome to Avalonia!"; + } +} diff --git a/MVVMExample/ViewModels/ViewModelBase.cs b/MVVMExample/ViewModels/ViewModelBase.cs new file mode 100644 index 0000000..40be0d0 --- /dev/null +++ b/MVVMExample/ViewModels/ViewModelBase.cs @@ -0,0 +1,8 @@ +using CommunityToolkit.Mvvm.ComponentModel; + +namespace MVVMExample.ViewModels +{ + public class ViewModelBase : ObservableObject + { + } +} diff --git a/MVVMExample/Views/MainWindow.axaml b/MVVMExample/Views/MainWindow.axaml new file mode 100644 index 0000000..b955207 --- /dev/null +++ b/MVVMExample/Views/MainWindow.axaml @@ -0,0 +1,20 @@ + + + + + + + + + + diff --git a/MVVMExample/Views/MainWindow.axaml.cs b/MVVMExample/Views/MainWindow.axaml.cs new file mode 100644 index 0000000..31a9369 --- /dev/null +++ b/MVVMExample/Views/MainWindow.axaml.cs @@ -0,0 +1,12 @@ +using Avalonia.Controls; + +namespace MVVMExample.Views +{ + public partial class MainWindow : Window + { + public MainWindow() + { + InitializeComponent(); + } + } +} \ No newline at end of file diff --git a/MVVMExample/app.manifest b/MVVMExample/app.manifest new file mode 100644 index 0000000..da54bd8 --- /dev/null +++ b/MVVMExample/app.manifest @@ -0,0 +1,18 @@ + + + + + + + + + + + + + +