Skip to content

Commit

Permalink
Fully support bindings for Xaml Navigation fixes #1754
Browse files Browse the repository at this point in the history
  • Loading branch information
dansiegel committed Jun 6, 2019
1 parent aee7c10 commit 8422312
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ public class MyMasterDetailViewModel : ViewModelBase
{
INavigationService _navigationService;

public string NavigatePath => "MyNavigationPage/ViewA";

public string Message => "Hello from MyMasterDetailViewModel";

public DelegateCommand<string> NavigateCommand { get; set; }
public MyMasterDetailViewModel(INavigationService navigationService)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<?xml version="1.0" encoding="utf-8" ?>
<MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
xmlns:nav="clr-namespace:Prism.Navigation.Xaml;assembly=Prism.Forms"
xmlns:prism="http://prismlibrary.com"
prism:ViewModelLocator.AutowireViewModel="True"
x:Class="HelloWorld.Views.MyMasterDetail">

Expand All @@ -13,9 +12,15 @@
<Button Text="ViewA" Command="{Binding NavigateCommand}" CommandParameter="MyTabbedPage?selectedTab=ViewA" />
<Button Text="ViewB" Command="{Binding NavigateCommand}" CommandParameter="MyTabbedPage?selectedTab=ViewB" />
<Button Text="ViewC" Command="{Binding NavigateCommand}" CommandParameter="MyTabbedPage?selectedTab=ViewC" />
<Button Text="XamlNav" Command="{nav:NavigateTo 'MyNavigationPage/ViewA'}" />
<Button Text="XamlNav wihout animation" Command="{nav:NavigateTo 'MyNavigationPage/ViewA', Animated=False}" />
<Button Text="XamlNav modal" Command="{nav:NavigateTo 'MyNavigationPage/ViewA', UseModalNavigation=True}" />
<Button Text="XamlNav" Command="{prism:NavigateTo Name={Binding NavigatePath}}">
<Button.CommandParameter>
<prism:NavigationParameters>
<prism:NavigationParameter Key="message" Value="{Binding Message}" />
</prism:NavigationParameters>
</Button.CommandParameter>
</Button>
<Button Text="XamlNav wihout animation" Command="{prism:NavigateTo 'MyNavigationPage/ViewA', Animated=False}" />
<Button Text="XamlNav modal" Command="{prism:NavigateTo 'MyNavigationPage/ViewA', UseModalNavigation=True}" />
</StackLayout>
</ContentPage>
</MasterDetailPage.Master>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace ModuleA.ViewModels
{
public class ViewAViewModel : BindableBase, INavigationAware, IActiveAware, IApplicationLifecycleAware, IPageLifecycleAware
public class ViewAViewModel : BindableBase, IAutoInitialize, INavigationAware, IActiveAware, IApplicationLifecycleAware, IPageLifecycleAware
{
private readonly INavigationService _navigationService;

Expand All @@ -22,6 +22,12 @@ public string Title

private bool _canNavigate = true;

string _message = "No Message Set.";
public string Message
{
get => _message;
set => SetProperty(ref _message, value);
}


public bool CanNavigate
Expand Down
28 changes: 18 additions & 10 deletions Sandbox/Xamarin/HelloWorld/ModuleA/Views/ViewA.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,25 @@
xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
prism:ViewModelLocator.AutowireViewModel="True"
x:Class="ModuleA.Views.ViewA"
Padding="{OnPlatform iOS='0,20,0,0'}"
Title="View A">
<ContentPage.Padding>
<OnPlatform x:TypeArguments="Thickness">
<On Platform="iOS" Value="0,20,0,0" />
</OnPlatform>
</ContentPage.Padding>
<ContentPage.Resources>
<ResourceDictionary>
<Style TargetType="Label">
<Setter Property="HorizontalTextAlignment" Value="Center" />
</Style>
<Style TargetType="Button">
<Setter Property="HorizontalOptions" Value="Center" />
</Style>
</ResourceDictionary>
</ContentPage.Resources>

<StackLayout>
<Label Text="{Binding Title}" VerticalOptions="Center" HorizontalOptions="Center" />
<Label Text="{Binding IsActive}" />
<Button Command="{Binding NavigateCommand}" Text="Navigate" />
</StackLayout>
<StackLayout VerticalOptions="Center"
HorizontalOptions="Center">
<Label Text="{Binding Title}" />
<Label Text="{Binding Message}" />
<Label Text="{Binding IsActive, StringFormat='IsActive: {0}'}" />
<Button Command="{Binding NavigateCommand}" Text="Navigate" />
</StackLayout>

</ContentPage>
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,14 @@ private void Initialize()
{
SourcePage = parentPage;

if(parentPage.Parent is MasterDetailPage mdp
if(parentPage.Parent is MasterDetailPage mdp
&& mdp.Master == parentPage)
{
SourcePage = mdp;
}
}

BindingContext = SourcePage.BindingContext;
}

private IEnumerable<Element> GetBindableStack()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public static INavigationParameters ToNavigationParameters(this object parameter
xamlParameter.BindingContext = xamlParameter.BindingContext ?? parent.BindingContext;
return new PrismNavParameters { { xamlParameter.Key, xamlParameter.Value } };
case XamlNavParams xamlParameters:
xamlParameters.BindingContext = parent.BindingContext;
return xamlParameters.ToNavigationParameters(parent);
default:
return new PrismNavParameters { { KnownNavigationParameters.XamlParam, parameter } };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,15 @@ private static void OnParentPropertyChanged(BindableObject bindable, object oldv
void BindingContextChanged(object parentObject, EventArgs args)
{
var parent = (BindableObject) parentObject;
for (var index = 0; index < self._list.Count; index++)
{
var parameter = self._list[index];
parameter.BindingContext = parent.BindingContext;
}
self.BindingContext = parent?.BindingContext;
}
}

protected override void OnBindingContextChanged()
{
foreach(var param in this)
{
param.BindingContext = BindingContext;
}
}
}
Expand Down

0 comments on commit 8422312

Please sign in to comment.