-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds NavigationController to Windows Phone tools
NavigationController is no longer static, so multiple navigation controllers can be used with multiple frames within one application. The existing ExtendedSplashScreen was modified to support the new NavigationController and an ExtendedSplashScreen for Windows Phone was created.
- Loading branch information
Showing
12 changed files
with
953 additions
and
417 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
//----------------------------------------------------------------------- | ||
// <copyright file="ExtendedSplashScreen.cs" company="Wygwam"> | ||
// Copyright (c) 2013 Wygwam. | ||
// Licensed under the Microsoft Public License (Ms-PL) (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://opensource.org/licenses/Ms-PL.html | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// </copyright> | ||
//----------------------------------------------------------------------- | ||
|
||
namespace Wygwam.Windows.Controls | ||
{ | ||
using Microsoft.Phone.Controls; | ||
using Microsoft.Phone.Shell; | ||
using System; | ||
using System.Windows; | ||
using System.Windows.Media; | ||
using Wygwam.Windows.ViewModels; | ||
using System.Reflection; | ||
|
||
public partial class ExtendedSplashScreen : BaseExtendedSplashScreen | ||
{ | ||
private NavigationController _navigationController; | ||
|
||
private Type _nextPage; | ||
|
||
public ExtendedSplashScreen() | ||
{ | ||
this.FontFamily = (FontFamily)Application.Current.Resources["PhoneFontFamilyNormal"]; | ||
this.FontSize=(double)Application.Current.Resources["PhoneFontSizeNormal"]; | ||
this.Foreground=(Brush)Application.Current.Resources["PhoneForegroundBrush"]; | ||
|
||
this.Orientation = PageOrientation.Portrait; | ||
this.SupportedOrientations = SupportedPageOrientation.Portrait; | ||
|
||
this.SetValue(SystemTray.IsVisibleProperty, false); | ||
|
||
this.Loaded += this.OnLoaded; | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="ExtendedSplashScreen"/> class. | ||
/// </summary> | ||
/// <param name="navigationController">The navigation controller used to navigate away from the splash screen.</param> | ||
/// <param name="args">The <see cref="IActivatedEventArgs"/> instance provided by the | ||
/// application when it was launched.</param> | ||
/// <param name="nextPage">The type of the page to which the application will navigate when loading is done.</param> | ||
/// <param name="parameter">An optional navigation parameter.</param> | ||
public ExtendedSplashScreen(NavigationController navigationController, Type nextPageViewModel, object parameter = null) | ||
: this() | ||
{ | ||
_navigationController = navigationController; | ||
|
||
_nextPage = nextPageViewModel; | ||
|
||
this.NavigationParameter = parameter; | ||
} | ||
|
||
private async void OnLoaded(object sender, RoutedEventArgs e) | ||
{ | ||
await this.PerformLoadingActionAsync().ContinueWith( | ||
_ => | ||
{ | ||
if (this.AdvancesAutomatically) | ||
{ | ||
this.IsDone = true; | ||
} | ||
}, | ||
this.UIScheduler); | ||
} | ||
|
||
protected override void MoveToNextPage() | ||
{ | ||
_navigationController.GoToType(_nextPage); | ||
|
||
var nextPage = _navigationController.CurrentView; | ||
|
||
if (nextPage != null) | ||
{ | ||
nextPage.NavigationService.RemoveBackEntry(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
//----------------------------------------------------------------------- | ||
// <copyright file="DefaultWindowManager.cs" company="Wygwam"> | ||
// Copyright (c) 2013 Wygwam. | ||
// Licensed under the Microsoft Public License (Ms-PL) (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://opensource.org/licenses/Ms-PL.html | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// </copyright> | ||
//----------------------------------------------------------------------- | ||
|
||
namespace Wygwam.Windows | ||
{ | ||
using Microsoft.Phone.Controls; | ||
using System.Windows; | ||
using System.Windows.Controls; | ||
|
||
/// <summary> | ||
/// An implementation of <see cref="T:IWindowManager"/> that provides the content | ||
/// of the app window as a <see cref="global::Windows.UI.Xaml.Controls.Frame"/> or sets | ||
/// a new <see cref="global::Windows.UI.Xaml.Controls.Frame"/> if necessary. | ||
/// </summary> | ||
public class DefaultWindowManager : IWindowManager | ||
{ | ||
/// <summary> | ||
/// Gets the <see cref="global::Windows.UI.Xaml.Controls.Frame" /> used for navigation if it exists. | ||
/// </summary> | ||
/// <remarks> | ||
/// Use <see cref="M:EnsureNaviationFrameExists" /> if you need to be sure that a frame for | ||
/// navigation exits (e.g. when the application navigates to its first page.) | ||
/// </remarks> | ||
public Frame NavigationFrame | ||
{ | ||
get | ||
{ | ||
return Application.Current.RootVisual as Frame; | ||
} | ||
|
||
private set | ||
{ | ||
Application.Current.RootVisual = value; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Ensures the navigation frame exists. | ||
/// </summary> | ||
/// <returns></returns> | ||
/// <exception cref="System.NotImplementedException"></exception> | ||
/// <remarks> | ||
/// Implementation of this method should check that a frame is available that can be used for | ||
/// navigation. If it isn't, it should take the necessary steps to create one and set the contents of | ||
/// the current <see cref="global::Windows.UI.Xaml.Window" /> appropriately. | ||
/// </remarks> | ||
public Frame EnsureNavigationFrameExists() | ||
{ | ||
if (this.NavigationFrame == null) | ||
{ | ||
// Create a Frame to act as the navigation context and set it as window content | ||
this.NavigationFrame = new PhoneApplicationFrame(); | ||
} | ||
|
||
return this.NavigationFrame; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
//----------------------------------------------------------------------- | ||
// <copyright file="NavigationController.cs" company="Wygwam"> | ||
// Copyright (c) 2013 Wygwam. | ||
// Licensed under the Microsoft Public License (Ms-PL) (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://opensource.org/licenses/Ms-PL.html | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// </copyright> | ||
//----------------------------------------------------------------------- | ||
|
||
namespace Wygwam.Windows | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Windows; | ||
using System.Windows.Controls; | ||
using Wygwam.Windows.ViewModels; | ||
|
||
/// <summary> | ||
/// Provides MVVM-compatible navigation for Windows Store Apps. | ||
/// </summary> | ||
public class NavigationController : BaseNavigationController | ||
{ | ||
private readonly Dictionary<Type, Uri> _viewModelMap = new Dictionary<Type, Uri>(); | ||
|
||
private Frame _currentNavigationFrame; | ||
|
||
private object _pendingNavigationParameter; | ||
|
||
/// <summary> | ||
/// Associates a view type with a view model type. | ||
/// </summary> | ||
/// <typeparam name="TViewModel">The type of the view model. This must extend <see cref="BaseViewModel" />.</typeparam> | ||
/// <typeparam name="TView">The type of the view. This must be a <see cref="global::Windows.UI.Xaml.Controls.Page" />.</typeparam> | ||
/// <seealso cref="M:GoTo{TViewModel}" /> | ||
/// <remarks> | ||
/// A new instance of the view model will be created at runtime by reflection when you | ||
/// navigate. The constructor of the view model will receive any parameters that you pass during navigation. | ||
/// </remarks> | ||
public void Register<TViewModel>(Uri pageUri) | ||
where TViewModel : BaseViewModel | ||
{ | ||
this.Register<TViewModel>(pageUri, args => (TViewModel)Activator.CreateInstance(typeof(TViewModel), args)); | ||
} | ||
|
||
/// <summary> | ||
/// Associates a view type with a view model type. | ||
/// </summary> | ||
/// <typeparam name="TViewModel">The type of the view model. This must extend <see cref="BaseViewModel" />.</typeparam> | ||
/// <typeparam name="TView">The type of the view. This must be a <see cref="global::Windows.UI.Xaml.Controls.Page" />.</typeparam> | ||
/// <param name="instance">An instance of the view model that will be used as a singleton during navigation operations.</param> | ||
/// <seealso cref="M:GoTo{TViewModel}" /> | ||
/// <remarks> | ||
/// The same instance of the view model will used for every navigation operation. | ||
/// </remarks> | ||
public void Register<TViewModel>(Uri pageUri, TViewModel instance) | ||
where TViewModel : BaseViewModel | ||
{ | ||
this.Register<TViewModel>(pageUri, _ => instance); | ||
} | ||
|
||
/// <summary> | ||
/// Associates a view type with a view model type. | ||
/// </summary> | ||
/// <typeparam name="TViewModel">The type of the view model. This must extend <see cref="BaseViewModel" />.</typeparam> | ||
/// <typeparam name="TView">The type of the view. This must be a <see cref="global::Windows.UI.Xaml.Controls.Page" />.</typeparam> | ||
/// <param name="delegate">A delegate that will create an instance of the view model during navigation.</param> | ||
/// <seealso cref="M:GoTo{TViewModel}" /> | ||
/// <remarks> | ||
/// The provided delegate must return the desired instance of the view model. It will be provided an array of arguments | ||
/// that can be used to create or initialize the view model. | ||
/// </remarks> | ||
public void Register<TViewModel>(Uri pageUri, Func<object[], TViewModel> @delegate) | ||
where TViewModel : BaseViewModel | ||
{ | ||
var type = typeof(TViewModel); | ||
|
||
_viewModelMap.Add(type, pageUri); | ||
|
||
this.RegisterViewModelGenerator<TViewModel>(type, @delegate); | ||
} | ||
|
||
protected override bool NavigateToType(Type viewModelType, object viewModelInstance) | ||
{ | ||
var frame = this.WindowManager.EnsureNavigationFrameExists(); | ||
|
||
if (frame != _currentNavigationFrame) | ||
{ | ||
if (_currentNavigationFrame != null) | ||
{ | ||
_currentNavigationFrame.Navigated -= this.OnFrameNavigated; | ||
} | ||
|
||
_currentNavigationFrame = frame; | ||
_currentNavigationFrame.Navigated += this.OnFrameNavigated; | ||
} | ||
|
||
_pendingNavigationParameter = viewModelInstance; | ||
|
||
return frame.Navigate(_viewModelMap[viewModelType]); | ||
} | ||
|
||
protected override bool IsViewModelRegistered(Type viewModelType) | ||
{ | ||
return _viewModelMap.ContainsKey(viewModelType); | ||
} | ||
|
||
private void OnFrameNavigated(object sender, System.Windows.Navigation.NavigationEventArgs e) | ||
{ | ||
var navigationTarget = e.Content as FrameworkElement; | ||
|
||
if (navigationTarget != null) | ||
{ | ||
navigationTarget.DataContext = _pendingNavigationParameter; | ||
} | ||
|
||
_pendingNavigationParameter = null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.