Skip to content

Commit

Permalink
Added short-form binding
Browse files Browse the repository at this point in the history
  • Loading branch information
matt-goldman committed Jan 14, 2023
1 parent b4fda58 commit 125e483
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/DemoProject/AppShell.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
Route="MainPage" />
</Tab>

<Tab Title="Markup">
<!--<Tab Title="Markup">
<ShellContent
ContentTemplate="{DataTemplate pages:MarkupPage}"
Route="MarkupPage" />
</Tab>
</Tab>-->
</TabBar>

</Shell>
8 changes: 8 additions & 0 deletions src/DemoProject/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@
Command="{Binding GoToNameCommand}"
HorizontalOptions="Center" />

<Button
Text="Click to go to markup page"
FontAttributes="Bold"
Grid.Row="3"
SemanticProperties.Hint="Counts the number of times you click"
Command="{Binding GoToMarkupCommand}"
HorizontalOptions="Center" />

<Image Grid.Row="4"
Source="dotnet_bot.png"
SemanticProperties.Description="Cute dot net bot waving hi to you!"
Expand Down
5 changes: 3 additions & 2 deletions src/DemoProject/Pages/MarkupPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
x:Class="DemoProject.Pages.MarkupPage"
xmlns:resolver="clr-namespace:Maui.Plugins.PageResolver;assembly=Maui.Plugins.PageResolver"
xmlns:vm="clr-namespace:DemoProject.ViewModels"
BindingContext="{resolver:ResolveViewModel x:TypeArguments=vm:MarkupViewModel}"
Title="MarkupPage">
<ContentPage.BindingContext>
<!--<ContentPage.BindingContext>
<resolver:ResolveViewModel x:TypeArguments="vm:MarkupViewModel" />
</ContentPage.BindingContext>
</ContentPage.BindingContext>-->
<VerticalStackLayout Spacing="30">
<Label
Text="Welcome to .NET MAUI!"
Expand Down
7 changes: 7 additions & 0 deletions src/DemoProject/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public class MainViewModel : BaseViewModel

public ICommand GoToNameCommand => new Command(async () => await GoToName());

public ICommand GoToMarkupCommand => new Command(async () => await GoToMarkup());

public string Name { get; set; }

public MainViewModel(INameService nameService)
Expand All @@ -31,4 +33,9 @@ async Task GoToName()

await Navigation.PushAsync<ParamPage>(Name);
}

async Task GoToMarkup()
{
await Navigation.PushAsync(new MarkupPage());
}
}
17 changes: 17 additions & 0 deletions src/Maui.Plugins.PageResolver/MarkupExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Xaml;
using System;

namespace Maui.Plugins.PageResolver;

public class ResolveViewModel<T> : IMarkupExtension<T>
{
public T ViewModel { get; set; }

public T ProvideValue(IServiceProvider serviceProvider)
{
var sp = Resolver.GetServiceProvider();
Expand All @@ -19,3 +22,17 @@ object IMarkupExtension.ProvideValue(IServiceProvider serviceProvider)
return ProvideValue(serviceProvider);
}
}

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

public object ProvideValue(IServiceProvider serviceProvider)
{
var sp = Resolver.GetServiceProvider();
var result = sp.GetRequiredService(ViewModel);

return result;
}
}

0 comments on commit 125e483

Please sign in to comment.