Skip to content

Commit 7ba696b

Browse files
committed
Added sample for community toolkit
1 parent a764335 commit 7ba696b

43 files changed

Lines changed: 1298 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.8.34112.27
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MauiMvvmDemo", "MauiMvvmDemo\MauiMvvmDemo.csproj", "{C50FA893-92DD-4E8B-90D4-8F791F376E29}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MauiMvvmDemo.ViewModels", "MauiMvvmDemo.ViewModels\MauiMvvmDemo.ViewModels.csproj", "{A79936A6-9775-41F7-9C4B-7E60CB74A12A}"
9+
EndProject
10+
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Any CPU = Debug|Any CPU
13+
Release|Any CPU = Release|Any CPU
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{C50FA893-92DD-4E8B-90D4-8F791F376E29}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17+
{C50FA893-92DD-4E8B-90D4-8F791F376E29}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
{C50FA893-92DD-4E8B-90D4-8F791F376E29}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
19+
{C50FA893-92DD-4E8B-90D4-8F791F376E29}.Release|Any CPU.ActiveCfg = Release|Any CPU
20+
{C50FA893-92DD-4E8B-90D4-8F791F376E29}.Release|Any CPU.Build.0 = Release|Any CPU
21+
{C50FA893-92DD-4E8B-90D4-8F791F376E29}.Release|Any CPU.Deploy.0 = Release|Any CPU
22+
{A79936A6-9775-41F7-9C4B-7E60CB74A12A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
23+
{A79936A6-9775-41F7-9C4B-7E60CB74A12A}.Debug|Any CPU.Build.0 = Debug|Any CPU
24+
{A79936A6-9775-41F7-9C4B-7E60CB74A12A}.Release|Any CPU.ActiveCfg = Release|Any CPU
25+
{A79936A6-9775-41F7-9C4B-7E60CB74A12A}.Release|Any CPU.Build.0 = Release|Any CPU
26+
EndGlobalSection
27+
GlobalSection(SolutionProperties) = preSolution
28+
HideSolutionNode = FALSE
29+
EndGlobalSection
30+
GlobalSection(ExtensibilityGlobals) = postSolution
31+
SolutionGuid = {A07C0295-93D2-4287-ABE8-69EEB451C2D8}
32+
EndGlobalSection
33+
EndGlobal
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System.Windows.Input;
2+
3+
namespace MauiMvvmDemo.ViewModels.Commands;
4+
5+
public class Command : ICommand
6+
{
7+
private readonly Action<object?> _action;
8+
9+
private readonly Func<object?, bool>? _canExecute;
10+
11+
public event EventHandler? CanExecuteChanged;
12+
13+
public Command(Action<object?> action)
14+
{
15+
_action = action;
16+
}
17+
18+
public Command(Action<object?> action, Func<object?, bool> canExecute)
19+
{
20+
_action = action;
21+
_canExecute = canExecute;
22+
23+
}
24+
25+
public bool CanExecute(object? parameter) =>
26+
_canExecute?.Invoke(parameter) ?? true;
27+
28+
public void Execute(object? parameter) =>
29+
_action?.Invoke(parameter);
30+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.1" />
11+
</ItemGroup>
12+
13+
</Project>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
namespace MauiMvvmDemo.ViewModels.Messages;
2+
public record class UsernameChangedMessage(string NewUsername);
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using CommunityToolkit.Mvvm.ComponentModel;
2+
3+
namespace MauiMvvmDemo.ViewModels.Models;
4+
5+
public partial class SelectedField : ObservableObject
6+
{
7+
[ObservableProperty]
8+
private string _color = string.Empty;
9+
10+
public SelectedField()
11+
{
12+
}
13+
14+
public SelectedField(string color)
15+
{
16+
Color = color;
17+
}
18+
19+
public static implicit operator SelectedField(string color) => new SelectedField(color);
20+
public static implicit operator string(SelectedField value) => value.Color;
21+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using CommunityToolkit.Mvvm.ComponentModel;
2+
using CommunityToolkit.Mvvm.Input;
3+
using CommunityToolkit.Mvvm.Messaging;
4+
using MauiMvvmDemo.ViewModels.Messages;
5+
using MauiMvvmDemo.ViewModels.Models;
6+
using System.Collections.ObjectModel;
7+
8+
namespace MauiMvvmDemo.ViewModels.Pages;
9+
10+
public partial class MainPageVM : ObservableObject
11+
{
12+
[ObservableProperty]
13+
private string _username = string.Empty;
14+
15+
public MainPageVM()
16+
{
17+
ResetSelectedFields(); // Initialize SelectedFields
18+
// Messenger can be used to communicate between pages or controls:
19+
// Sample for receiving message
20+
//WeakReferenceMessenger.Default.Register<UsernameChangedMessage>(this, (recipient, message) =>
21+
//{
22+
// //
23+
//});
24+
25+
//// Sample for sending message
26+
//WeakReferenceMessenger.Default.Send(new UsernameChangedMessage("My new Username"));
27+
}
28+
29+
public ICollection<string> AvailableColors { get; } = new string[]
30+
{
31+
"Red",
32+
"Blue",
33+
"Green",
34+
"Yellow",
35+
"Orange",
36+
"Purple"
37+
};
38+
39+
public ObservableCollection<SelectedField> SelectedFields { get; } = new();
40+
41+
public ObservableCollection<IEnumerable<SelectedField>> Moves { get; } = new();
42+
43+
[RelayCommand(CanExecute = nameof(CanExecuteMakeMoveCommand))]
44+
private async Task MakeMoveAsync()
45+
{
46+
Moves.Add(SelectedFields.ToArray()); // ToArray to copy the values
47+
ResetSelectedFields();
48+
await Task.Delay(100); // simulates some async logic
49+
}
50+
51+
private bool CanExecuteMakeMoveCommand() =>
52+
true; // e.g. validation logic here
53+
54+
private void ResetSelectedFields()
55+
{
56+
SelectedFields.Clear();
57+
58+
for (int i = 0; i < 4; i++)
59+
SelectedFields.Add(new SelectedField());
60+
}
61+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version = "1.0" encoding = "UTF-8" ?>
2+
<Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
4+
xmlns:local="clr-namespace:MauiMvvmDemo"
5+
x:Class="MauiMvvmDemo.App">
6+
<Application.Resources>
7+
<ResourceDictionary>
8+
<ResourceDictionary.MergedDictionaries>
9+
<ResourceDictionary Source="Resources/Styles/Colors.xaml" />
10+
<ResourceDictionary Source="Resources/Styles/Styles.xaml" />
11+
</ResourceDictionary.MergedDictionaries>
12+
</ResourceDictionary>
13+
</Application.Resources>
14+
</Application>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace MauiMvvmDemo;
2+
3+
public partial class App : Application
4+
{
5+
public App()
6+
{
7+
InitializeComponent();
8+
9+
MainPage = new AppShell();
10+
}
11+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<Shell
3+
x:Class="MauiMvvmDemo.AppShell"
4+
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
5+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
6+
xmlns:pages="clr-namespace:MauiMvvmDemo.Pages"
7+
Shell.FlyoutBehavior="Disabled"
8+
Title="MauiMvvmDemo">
9+
10+
<ShellContent
11+
Title="Home"
12+
ContentTemplate="{DataTemplate pages:MainPage}"
13+
Route="MainPage" />
14+
15+
</Shell>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace MauiMvvmDemo;
2+
3+
public partial class AppShell : Shell
4+
{
5+
public AppShell()
6+
{
7+
InitializeComponent();
8+
}
9+
}

0 commit comments

Comments
 (0)