Skip to content
This repository has been archived by the owner on Nov 4, 2022. It is now read-only.

Commit

Permalink
Added Navigation and made ShellContent set its own route.
Browse files Browse the repository at this point in the history
  • Loading branch information
vniehues committed Nov 4, 2021
1 parent 252e91f commit e0e0c5d
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 29 deletions.
18 changes: 12 additions & 6 deletions samples/mavvmApp/AppShell.xaml
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<Shell
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:viewmodels="clr-namespace:mavvmApp.ViewModels" xmlns:mavvm="clr-namespace:mavvm;assembly=mavvm"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:viewmodels="clr-namespace:mavvmApp.ViewModels" xmlns:mavvm="clr-namespace:mavvm;assembly=mavvm"
Shell.FlyoutBehavior="Disabled"
x:Class="mavvmApp.AppShell">
<TabBar>
<Tab Title="Main">
<ShellItem>
<mavvm:MavvmShellContent ViewModel="{x:Type viewmodels:MainPageViewModel}">
</mavvm:MavvmShellContent>
</Tab>
</ShellItem>
<TabBar>
<Tab Title="Second">
<mavvm:MavvmShellContent Title="Second 2" ViewModel="{x:Type viewmodels:SecondTabPageViewModel}">
<mavvm:MavvmShellContent ViewModel="{x:Type viewmodels:SecondPageViewModel}">
</mavvm:MavvmShellContent>
</Tab>
<Tab Title="Second 2">
<mavvm:MavvmShellContent ViewModel="{x:Type viewmodels:SecondTabPageViewModel}">
</mavvm:MavvmShellContent>
</Tab>
</TabBar>

<!--<Tab Title="Library" Icon="library.png">
<ShellContent ContentTemplate="{DataTemplate pages:LibraryPage}"/>
</Tab>
<Tab Title="Contact" Icon="contact.png">
<ShellContent ContentTemplate="{DataTemplate pages:ContactPage}"/>
</Tab>-->
</TabBar>
</Shell>
5 changes: 3 additions & 2 deletions samples/mavvmApp/ViewModels/MainPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.ComponentModel;
using mavvm;
using mavvm.Navigation;
using Microsoft.Maui.Controls;

namespace mavvmApp.ViewModels
Expand Down Expand Up @@ -43,9 +44,9 @@ void CountUp()
Count++;
}

void Navigate()
async void Navigate()
{
Shell.Current.GoToViewModel<SecondPageViewModel>(new Dictionary<string, object>{ { "countParam", Count } });
await Navigation.GoToViewModel<SecondPageViewModel>(true, new Dictionary<string, object>{ { "countParam", Count } });
}
}
}
Expand Down
10 changes: 8 additions & 2 deletions src/mavvm/Customs/MavvmShellContent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,22 @@ namespace mavvm
{
public class MavvmShellContent : ShellContent
{

public static readonly BindableProperty ViewModelProperty = BindableProperty.Create(nameof(ViewModel), typeof(Type),
typeof(MavvmShellContent));
typeof(MavvmShellContent), propertyChanged: ViewModelChanged);

public Type ViewModel
{
get => (Type)GetValue(ViewModelProperty);
set => SetValue(ViewModelProperty, value);
}

static void ViewModelChanged(BindableObject bindable, object oldValue, object newValue)
{
var shellcontent = bindable as MavvmShellContent;

shellcontent.Route = (newValue as Type).Name;
}

public MavvmShellContent()
{
ContentTemplate = new DataTemplate(() => Routing.GetOrCreateContent(ViewModel.Name));
Expand Down
19 changes: 0 additions & 19 deletions src/mavvm/Extensions/MavvmShellExtensions.cs

This file was deleted.

26 changes: 26 additions & 0 deletions src/mavvm/Navigation/Navigation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Maui.Controls;

namespace mavvm
{
public static class Navigation
{
public static Task GoBack(bool replaceStack = false)
{
return Shell.Current.GoToAsync("..");
}

public static Task GoToViewModel<TViewModel>(bool replaceStack = false, Dictionary<string, object> parameters = null)
{
var path = (replaceStack ? "//" : "") + typeof(TViewModel).Name;

if (parameters is null)
return Shell.Current.GoToAsync(path);
else
return Shell.Current.GoToAsync(path, parameters);
}
}
}

13 changes: 13 additions & 0 deletions src/mavvm/mavvm.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,27 @@
<SupportedOSPlatformVersion Condition="'$(TargetFramework)' == 'net6.0-maccatalyst'">14.0</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="'$(TargetFramework)' == 'net6.0-android'">21.0</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$(TargetFramework.Contains('-windows'))">10.0.18362.0</SupportedOSPlatformVersion>
<ReleaseVersion>0.9</ReleaseVersion>
<SynchReleaseVersion>false</SynchReleaseVersion>
<PackageId>mavvm</PackageId>
<PackageVersion>0.9</PackageVersion>
<Authors>Vincent Niehues</Authors>
<Copyright>Vincent Niehues</Copyright>
<Owners>Vincent Niehues</Owners>
<Title>mavvm</Title>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<None Remove="ViewModels\" />
<None Remove="Containers\" />
<None Remove="Navigation\" />
</ItemGroup>
<ItemGroup>
<Folder Include="Implementations\" />
<Folder Include="Containers\" />
<Folder Include="Navigation\" />
</ItemGroup>
</Project>

0 comments on commit e0e0c5d

Please sign in to comment.