Skip to content

Commit

Permalink
Merge pull request #1519 from PrismLibrary/xf-partialviews
Browse files Browse the repository at this point in the history
Support Partial Views in Xamarin Forms
  • Loading branch information
brianlagunas authored Sep 20, 2018
2 parents 9ee268e + dc15876 commit f50103f
Show file tree
Hide file tree
Showing 28 changed files with 529 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@
<Project>{152da85c-13f4-4427-850c-052cbbcdd4b0}</Project>
<Name>Prism.Forms</Name>
</ProjectReference>
<ProjectReference Include="..\..\ModuleA\ModuleA.csproj">
<Project>{f0d7441b-d48a-4f58-91fb-45fa441b0434}</Project>
<Name>ModuleA</Name>
</ProjectReference>
<ProjectReference Include="..\HelloWorld\HelloWorld.csproj">
<Project>{DCC0DE3D-297A-4CE1-A002-81AEE3CA0AE4}</Project>
<Name>HelloWorld</Name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@
<Project>{152da85c-13f4-4427-850c-052cbbcdd4b0}</Project>
<Name>Prism.Forms</Name>
</ProjectReference>
<ProjectReference Include="..\..\ModuleA\ModuleA.csproj">
<Project>{f0d7441b-d48a-4f58-91fb-45fa441b0434}</Project>
<Name>ModuleA</Name>
</ProjectReference>
<ProjectReference Include="..\HelloWorld\HelloWorld.csproj">
<Project>{DCC0DE3D-297A-4CE1-A002-81AEE3CA0AE4}</Project>
<Name>HelloWorld</Name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@
<Project>{152da85c-13f4-4427-850c-052cbbcdd4b0}</Project>
<Name>Prism.Forms</Name>
</ProjectReference>
<ProjectReference Include="..\..\ModuleA\ModuleA.csproj">
<Project>{f0d7441b-d48a-4f58-91fb-45fa441b0434}</Project>
<Name>ModuleA</Name>
</ProjectReference>
<ProjectReference Include="..\HelloWorld\HelloWorld.csproj">
<Project>{DCC0DE3D-297A-4CE1-A002-81AEE3CA0AE4}</Project>
<Name>HelloWorld</Name>
Expand Down
9 changes: 7 additions & 2 deletions Sandbox/Xamarin/HelloWorld/HelloWorld/HelloWorld/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using HelloWorld.Views;
using Prism;
using Prism.DryIoc;
using Prism.Ioc;
using Prism.Modularity;
using Xamarin.Forms;
Expand All @@ -10,7 +9,7 @@
[assembly: XamlCompilation (XamlCompilationOptions.Compile)]
namespace HelloWorld
{
public partial class App : PrismApplication
public sealed partial class App
{
public App()
: this(null)
Expand Down Expand Up @@ -87,11 +86,17 @@ protected override void OnStart ()

protected override void OnSleep ()
{
// Support IApplicationLifecycleAware
base.OnSleep();

// Handle when your app sleeps
}

protected override void OnResume ()
{
// Support IApplicationLifecycleAware
base.OnResume();

// Handle when your app resumes
}
}
Expand Down
105 changes: 105 additions & 0 deletions Sandbox/Xamarin/HelloWorld/ModuleA/ViewModels/PartialViewBViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
using System;
using System.Diagnostics;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Navigation;

namespace ModuleA.ViewModels
{
public class PartialViewBViewModel : BindableBase
{
private INavigationService _navigationService { get; }

public PartialViewBViewModel(INavigationService navigationService, IApplicationCommands applicationCommands)
{
_navigationService = navigationService;
NavigateCommand = new DelegateCommand(Navigate).ObservesCanExecute(() => CanNavigate);
SaveCommand = new DelegateCommand(Save);
ResetCommand = new DelegateCommand(Reset);

applicationCommands.SaveCommand.RegisterCommand(SaveCommand);
applicationCommands.ResetCommand.RegisterCommand(ResetCommand);
}

private string _title = "View B";
public string Title
{
get { return _title; }
set { SetProperty(ref _title, value); }
}

private bool _canNavigate = true;
public bool CanNavigate
{
get { return _canNavigate; }
set { SetProperty(ref _canNavigate, value); }
}

public event EventHandler IsActiveChanged;

private bool _isActive;
public bool IsActive
{
get { return _isActive; }
set
{
SetProperty(ref _isActive, value);
OnActiveChanged();
}
}

public DelegateCommand NavigateCommand { get; }

public DelegateCommand SaveCommand { get; }

public DelegateCommand ResetCommand { get; }

private void Reset()
{
Title = "View B";
}

async void Navigate()
{
CanNavigate = false;
await _navigationService.NavigateAsync("ViewA");
CanNavigate = true;
}

private void Save()
{
Title = "Saved";
}

void OnActiveChanged()
{
IsActiveChanged?.Invoke(this, EventArgs.Empty);
SaveCommand.IsActive = IsActive;
}

public void OnNavigatedFrom(INavigationParameters parameters)
{

}

public void OnNavigatedTo(INavigationParameters parameters)
{
Debug.WriteLine("Navigated to ViewB");
}

public void OnNavigatingTo(INavigationParameters parameters)
{

}

public void OnAppearing()
{
Debug.WriteLine("ViewB is appearing");
}

public void OnDisappearing()
{
Debug.WriteLine("ViewB is disappearing");
}
}
}
8 changes: 8 additions & 0 deletions Sandbox/Xamarin/HelloWorld/ModuleA/Views/PartialViewB.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<StackLayout xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="ModuleA.Views.PartialViewB">
<Label Text="{Binding Title}" VerticalOptions="Center" HorizontalOptions="Center" />
<Label Text="{Binding IsActive}" />
<Button Command="{Binding NavigateCommand}" Text="Navigate" />
</StackLayout>
13 changes: 13 additions & 0 deletions Sandbox/Xamarin/HelloWorld/ModuleA/Views/PartialViewB.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Xamarin.Forms.Xaml;

namespace ModuleA.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class PartialViewB
{
public PartialViewB ()
{
InitializeComponent ();
}
}
}
13 changes: 8 additions & 5 deletions Sandbox/Xamarin/HelloWorld/ModuleA/Views/ViewB.xaml
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:ModuleA.Views"
xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
prism:ViewModelLocator.AutowireViewModel="True"
x:Class="ModuleA.Views.ViewB"
x:Name="viewB"
Title="View B">
<ContentPage.Padding>
<OnPlatform x:TypeArguments="Thickness">
<On Platform="iOS" Value="0,20,0,0" />
</OnPlatform>
</ContentPage.Padding>

<StackLayout>
<Label Text="{Binding Title}" VerticalOptions="Center" HorizontalOptions="Center" />
<Label Text="{Binding IsActive}" />
<Button Command="{Binding NavigateCommand}" Text="Navigate" />
</StackLayout>
<local:PartialViewB prism:ViewModelLocator.AutowirePartialView="{x:Reference viewB}" />
<!--<StackLayout>
<Label Text="{Binding Title}" VerticalOptions="Center" HorizontalOptions="Center" />
<Label Text="{Binding IsActive}" />
<Button Command="{Binding NavigateCommand}" Text="Navigate" />
</StackLayout>-->

</ContentPage>
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0" />
<PackageReference Include="Xamarin.Forms" Version="3.1.0.697729" />
<PackageReference Include="Xamarin.Forms" Version="3.2.0.839982" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
</ItemGroup>
Expand Down
14 changes: 12 additions & 2 deletions Source/Xamarin/Prism.Autofac.Forms/AutofacContainerExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Autofac.Core;
using Autofac.Features.ResolveAnything;
using Prism.Ioc;
using Prism.Mvvm;
using Prism.Navigation;
using System;
using Xamarin.Forms;
Expand Down Expand Up @@ -61,9 +62,18 @@ public object Resolve(Type type, string name)
public object ResolveViewModelForView(object view, Type viewModelType)
{
Parameter parameter = null;
if (view is Page page)
switch(view)
{
parameter = new TypedParameter(typeof(INavigationService), this.CreateNavigationService(page));
case Page page:
parameter = new TypedParameter(typeof(INavigationService), this.CreateNavigationService(page));
break;
case BindableObject bindable:
var attachedPage = bindable.GetValue(ViewModelLocator.AutowirePartialViewProperty) as Page;
if(attachedPage != null)
{
parameter = new TypedParameter(typeof(INavigationService), this.CreateNavigationService(attachedPage));
}
break;
}

return Instance.Resolve(viewModelType, parameter);
Expand Down
24 changes: 24 additions & 0 deletions Source/Xamarin/Prism.DI.Forms.Tests/Fixtures/FixtureBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Prism.DI.Forms.Tests.Mocks;
using Xamarin.Forms;
using Xunit;
using Xunit.Abstractions;

namespace Prism.DI.Forms.Tests.Fixtures
{
public abstract class FixtureBase
{
protected ITestOutputHelper _testOutputHelper { get; }

public FixtureBase(ITestOutputHelper testOutputHelper)
{
_testOutputHelper = testOutputHelper;
Xamarin.Forms.Mocks.MockForms.Init();
}

protected PrismApplicationMock CreateMockApplication(Page view = null)
{
var initializer = new XunitPlatformInitializer(_testOutputHelper);
return view == null ? new PrismApplicationMock(initializer) : new PrismApplicationMock(initializer, view);
}
}
}
Loading

0 comments on commit f50103f

Please sign in to comment.