Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Support for NavigateTo within EventToCommandBehavior #2495

Merged
merged 2 commits into from
Jul 1, 2021
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
7 changes: 6 additions & 1 deletion src/Forms/Prism.Forms/Xaml/ParentPageAwareExtension.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Prism.Behaviors;
using Prism.Properties;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
Expand Down Expand Up @@ -85,8 +86,12 @@ private void Initialize()

_targetElement = valueTargetProvider.TargetObject as Element;

//this is handling the scenario of the extension being used within the EventToCommandBehavior
if (_targetElement is null && valueTargetProvider.TargetObject is BehaviorBase<BindableObject> behavior)
_targetElement = behavior.AssociatedObject as Element;

if (_targetElement is null)
throw new ArgumentNullException(nameof(TargetElement));
throw new Exception($"{valueTargetProvider.TargetObject} is not supported");

var parentPage = (Page)GetBindableStack().FirstOrDefault(p => p.GetType()
.IsSubclassOf(typeof(Page)));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Threading.Tasks;
using System.Windows.Input;
using Prism.Behaviors;
using Prism.DI.Forms.Tests.Fixtures;
using Prism.DI.Forms.Tests.Mocks.Internals;
using Prism.DI.Forms.Tests.Mocks.Views;
Expand Down Expand Up @@ -166,5 +167,50 @@ public void UsesMasterDetailPage_WhenParentIsMaster()
Assert.NotNull(navigateExtension.SourcePage);
Assert.IsType<MasterDetailPage>(navigateExtension.SourcePage);
}


[Fact]
public async Task ResolvesParentElementFrom_EventToCommandBehavior()
{
var app = CreateMockApplication();

Log.Listeners.Clear();
var logObserver = new FormsLogObserver();
Log.Listeners.Add(logObserver);

var behavior = new EventToCommandBehavior() { EventName = "ItemTapped" };
var layout = new ListView();
layout.Behaviors.Add(behavior);

var serviceProvider = new XamlServiceProvider();
serviceProvider.Add(typeof(IProvideValueTarget), new XamlValueTargetProvider(behavior, "Command"));

var navigateExtension = new NavigateToExtension()
{
Name = "/AutowireView"
};
navigateExtension.ProvideValue(serviceProvider);

app.MainPage = new NavigationPage(new ContentPage
{
Content = layout
});

Assert.NotNull(layout.Parent);
Assert.IsType<ContentPage>(layout.Parent);

Assert.NotNull(navigateExtension.SourcePage);
Assert.True(navigateExtension.CanExecute(null));
var ex = await Record.ExceptionAsync(async () =>
{
navigateExtension.Execute(null);
if (navigateExtension.IsNavigating)
await Task.Delay(100);
});
Assert.Null(ex);
Assert.Empty(logObserver.Logs);

Assert.IsType<AutowireView>(app.MainPage);
}
}
}