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

fixed navigation #12

Merged
merged 1 commit into from
May 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions samples/mavvmApp/AppShell.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@ xmlns="http://schemas.microsoft.com/dotnet/2021/maui"

x:Class="mavvmApp.AppShell">
<ShellItem Route="start">
<mavvm:MavvmShellContent ViewModel="{x:Type viewmodels:MainPageViewModel}">
</mavvm:MavvmShellContent>
<mavvm:MavvmShellContent ViewModel="{x:Type viewmodels:MainPageViewModel}">
</mavvm:MavvmShellContent>
</ShellItem>
<TabBar Route="main">
<Tab Title="Second" Route="firstTab">
<mavvm:MavvmShellContent Title="MainPageTitle" ViewModel="{x:Type viewmodels:SecondPageViewModel}">
<TabBar x:Name="MainTabbar" Route="main">
<Tab>
<mavvm:MavvmShellContent ViewModel="{x:Type viewmodels:SecondPageViewModel}">
</mavvm:MavvmShellContent>
</Tab>
<Tab Title="Second 2" Route="secondTab">
<Tab>
<mavvm:MavvmShellContent ViewModel="{x:Type viewmodels:SecondTabPageViewModel}">
</mavvm:MavvmShellContent>
</Tab>
</Tab>
</TabBar>

<!--<Tab Title="Library" Icon="library.png">
<!--<Tab Title="Library" Icon="library.png">
<ShellContent ContentTemplate="{DataTemplate pages:LibraryPage}"/>
</Tab>
<Tab Title="Contact" Icon="contact.png">
Expand Down
13 changes: 9 additions & 4 deletions samples/mavvmApp/AppShell.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
using Microsoft.Extensions.DependencyInjection;
using mavvm.Interfaces;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Maui;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.PlatformConfiguration.WindowsSpecific;

namespace mavvmApp
{
public partial class AppShell : Shell
public partial class AppShell : IShellWithTabBar
{
public AppShell()
{
InitializeComponent();
}
InitializeComponent();

TabBar = MainTabbar;
}

public TabBar TabBar { get; }
}
}
1 change: 1 addition & 0 deletions samples/mavvmApp/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public static MauiApp CreateMauiApp()
.AddRoute<MainPage, MainPageViewModel>()
.AddRoute<SecondPage, SecondPageViewModel>()
.AddRoute<ThirdPage, ThirdPageViewModel>()
.AddRoute<LastPage, LastPageViewModel>()
.AddRoute<SecondTabPage, SecondTabPageViewModel>();

builder.Services.AddSingleton<IConsoleService, ConsoleService>();
Expand Down
32 changes: 32 additions & 0 deletions samples/mavvmApp/ViewModels/LastPageViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using CommunityToolkit.Mvvm.Input;
using mavvm;

namespace mavvmApp.ViewModels
{
public partial class LastPageViewModel : BindableBase, INavigateToAware
{

[ICommand]
async void GoBack()
{
await BaseMethods.GoBack(parameters: new NavigationParameters {{"testKey3","testValue3" }});
}

[ICommand]
async void GoToSecondTabPage()
{
await BaseMethods.GoToViewModel<SecondTabPageViewModel>(parameters: new NavigationParameters { { "testKey4", "testValue4" } });
}

public void NavigatedTo(NavigationParameters parameters)
{
Console.WriteLine(parameters);
}

public LastPageViewModel()
{
}
}
}

2 changes: 1 addition & 1 deletion samples/mavvmApp/ViewModels/MainPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void CountUp()

async void Navigate()
{
await BaseMethods.GoToSection("main", new NavigationParameters{ { "countParam", Count } });
await BaseMethods.GoToSection<SecondTabPageViewModel>("main",parameters: new NavigationParameters{ { "countParam", Count } });
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions samples/mavvmApp/ViewModels/SecondTabPageViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using mavvm;
using mavvm.Attibutes;

namespace mavvmApp.ViewModels
{
[TabRoute("secondTabPage")]
public class SecondTabPageViewModel : BindableBase
{
string _title;
Expand Down
16 changes: 11 additions & 5 deletions samples/mavvmApp/ViewModels/ThirdPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@ namespace mavvmApp.ViewModels
public partial class ThirdPageViewModel : BindableBase, INavigateToAware
{

[ICommand]
async void GoBack()
{
await BaseMethods.GoBack(parameters: new NavigationParameters {{"testKey1","testValue1" }});
}
[ICommand]
async void GoBack()
{
await BaseMethods.GoBack();
}

[ICommand]
async void GoToLastPage()
{
await BaseMethods.GoToViewModel<LastPageViewModel>(parameters: new NavigationParameters { { "testKey2", "testValue2" } });
}

public void NavigatedTo(NavigationParameters parameters)
{
Expand Down
43 changes: 43 additions & 0 deletions samples/mavvmApp/Views/LastPage.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:viewmodels="clr-namespace:mavvmApp.ViewModels"
x:Class="mavvmApp.Views.LastPage"
Title="{Binding Title}"
BackgroundColor="{DynamicResource SecondaryColor}"
Shell.PresentationMode="Animated">


<ScrollView>
<Grid RowSpacing="25" RowDefinitions="Auto,Auto,Auto,Auto,*"
Padding="{OnPlatform iOS='30,60,30,30', Default='30'}">

<Label
Text="Hello, World!"
Grid.Row="0"
SemanticProperties.HeadingLevel="Level1"
FontSize="32"
HorizontalOptions="Center" />

<Label
Text="Welcome to .NET Multi-platform App UI"
Grid.Row="1"
SemanticProperties.HeadingLevel="Level1"
SemanticProperties.Description="Welcome to dot net Multi platform App U I"
FontSize="18"
HorizontalOptions="Center" />

<Label
Text="{Binding Count, StringFormat='Current count: {0}'}"
Grid.Row="2"
FontSize="18"
FontAttributes="Bold"
HorizontalOptions="Center" />

<Button Text="GoBack"
Grid.Row="3" Command="{Binding GoBackCommand}"/>
<Button Text="Go to SecondTab"
Grid.Row="4" Command="{Binding GoToSecondTabPageCommand}"/>

</Grid>
</ScrollView>
</ContentPage>
14 changes: 14 additions & 0 deletions samples/mavvmApp/Views/LastPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using mavvmApp.ViewModels;
using Microsoft.Maui.Controls;

namespace mavvmApp.Views
{
public partial class LastPage : ContentPage
{
public LastPage()
{
InitializeComponent();
}
}
}
13 changes: 4 additions & 9 deletions samples/mavvmApp/Views/ThirdPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,11 @@
FontSize="18"
FontAttributes="Bold"
HorizontalOptions="Center" />

<Button Text="GoBack"
Grid.Row="2" Command="{Binding GoBackCommand}"/>

<Image Grid.Row="4"
Source="dotnet_bot.png"
SemanticProperties.Description="Cute dot net bot waving hi to you!"
WidthRequest="250"
HeightRequest="310"
HorizontalOptions="Center" />
Grid.Row="3" Command="{Binding GoBackCommand}"/>
<Button Text="Go to ThirdPage"
Grid.Row="4" Command="{Binding GoToLastPageCommand}"/>

</Grid>
</ScrollView>
Expand Down
6 changes: 6 additions & 0 deletions samples/mavvmApp/mavvmApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@
<MauiXaml Condition=" '$(EnableDefaultXamlItems)' == 'true' " Update="Views\ThirdPage.xaml">
<SubType>Designer</SubType>
</MauiXaml>
<MauiXaml Condition=" '$(EnableDefaultXamlItems)' == 'true' " Update="Views\ThirdPage %28Kopie%29.xaml">
<SubType>Designer</SubType>
</MauiXaml>
<MauiXaml Condition=" '$(EnableDefaultXamlItems)' == 'true' " Update="Views\LastPage.xaml">
<SubType>Designer</SubType>
</MauiXaml>
</ItemGroup>
<PropertyGroup Condition="$(TargetFramework.Contains('-windows'))">
<OutputType>WinExe</OutputType>
Expand Down
15 changes: 15 additions & 0 deletions src/mavvm/Attibutes/TabRouteAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
namespace mavvm.Attibutes
{
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
public class TabRouteAttribute : Attribute
{
public string TabRoute { get; }

public TabRouteAttribute(string tabRoute)
{
TabRoute = tabRoute;
}
}
}

16 changes: 16 additions & 0 deletions src/mavvm/Customs/MavvmShellContent.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using mavvm.Attibutes;
using Microsoft.Maui.Controls;

namespace mavvm
Expand All @@ -21,6 +24,19 @@ static void ViewModelChanged(BindableObject bindable, object oldValue, object ne
shellcontent.SetContentTemplate();
}

protected override void OnParentSet()
{
base.OnParentSet();

var tab = this.Parent as Tab;
if (tab is null) return;

var attrib = ViewModel.GetCustomAttribute<TabRouteAttribute>();
if (attrib is null) return;

tab.Route = attrib.TabRoute;
}

public MavvmShellContent()
{
SetContentTemplate();
Expand Down
25 changes: 25 additions & 0 deletions src/mavvm/Customs/MavvmShellTab.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Reflection;
using mavvm.Attibutes;
using Microsoft.Maui.Controls;

namespace mavvm.Customs
{
public class MavvmShellTab : Tab
{
protected override void OnChildAdded(Element child)
{
base.OnChildAdded(child);

var vmContent = (child as MavvmShellContent);
var vm = vmContent?.ViewModel;
if (vm is null) return;

var attrib = vm.GetCustomAttribute<TabRouteAttribute>();
if (attrib is null) return;

(child as MavvmShellContent).Route = attrib.TabRoute;
}
}
}

16 changes: 16 additions & 0 deletions src/mavvm/Extensions/ShellContentExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Xaml;

namespace mavvm
{
[ContentProperty(nameof(ViewModel))]
public class GetOrCreateContent : IMarkupExtension
{
public Type ViewModel { get; set; }

public object ProvideValue(IServiceProvider serviceProvider) =>
new DataTemplate(() => Routing.GetOrCreateContent(ViewModel.Name));
}
}

11 changes: 11 additions & 0 deletions src/mavvm/Interfaces/IShellWithTabbar.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using Microsoft.Maui.Controls;

namespace mavvm.Interfaces
{
public interface IShellWithTabBar
{
TabBar TabBar { get; }
}
}

Loading