Skip to content

Instantly share code, notes, and snippets.

@vmilev
Created February 29, 2016 14:51
Show Gist options
  • Save vmilev/c00b40965d3dc031bfad to your computer and use it in GitHub Desktop.
Save vmilev/c00b40965d3dc031bfad to your computer and use it in GitHub Desktop.
Share Code Blog 2
public partial class App : Application
{
private static readonly IUnityContainer Container = new UnityContainer();
public static INavigationService NavigationService { get; private set; }
// Other code omitted for conciseness
internal static void InitializeNavigationService(NavigationService service)
{
NavigationService = new SimpleNavigationService(service);
Container.RegisterInstance<INavigationService>(NavigationService);
}
}
protected override Type GetPageType(string pageToken)
{
var assemblyQualifiedAppType = this.GetType().AssemblyQualifiedName;
var pageNameWithParameter = assemblyQualifiedAppType.Replace(this.GetType().FullName, this.GetType().Namespace + ".Views.{0}");
var viewFullName = string.Format(CultureInfo.InvariantCulture, pageNameWithParameter, pageToken);
var viewType = Type.GetType(viewFullName);
if (viewType == null)
{
var resourceLoader = ResourceLoader.GetForCurrentView(PrismConstants.StoreAppsInfrastructureResourceMapId);
throw new ArgumentException(
string.Format(CultureInfo.InvariantCulture, resourceLoader.GetString("DefaultPageTypeLookupErrorMessage"), pageToken, this.GetType().Namespace + ".Views"),
"pageToken");
}
return viewType;
}
namespace Microsoft.Practices.Prism.Mvvm.Interfaces
{
public interface INavigationService
{
bool CanGoBack();
void ClearHistory();
void GoBack();
bool Navigate(string pageToken, object parameter);
void RestoreSavedNavigation();
void Suspending();
}
}
public class MainPageViewModel : ViewModelBase
{
private readonly ICommand navigateAway;
private INavigationService navService;
public ICommand NavigateAway
{
get { return this.navigateAway; }
}
public MainPageViewModel(INavigationService navService)
{
this.navService = navService;
this.navigateAway = new DelegateCommand<int?>(this.TestCommandCallback);
}
private void TestCommandCallback(int? obj)
{
this.navService.Navigate("SecondPage", null);
}
}
<Window x:Class="MyApp.Desktop.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Frame NavigationUIVisibility="Hidden" x:Name="navigationFrame">
</Frame>
</Grid>
</Window>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
App.InitializeNavigationService(navigationFrame.NavigationService);
App.NavigationService.Navigate("MainPage", null);
}
}
public class SimpleNavigationService : INavigationService
{
private NavigationService navigationService;
public SimpleNavigationService(NavigationService nativeService)
{
this.navigationService = nativeService;
}
public bool CanGoBack()
{
return this.navigationService.CanGoBack;
}
public void ClearHistory()
{
throw new NotImplementedException();
}
public void GoBack()
{
this.navigationService.GoBack();
}
public bool Navigate(string pageToken, object parameter)
{
return this.navigationService.Navigate(new Uri(string.Format("Views/{0}.xaml", pageToken), UriKind.Relative));
}
public void RestoreSavedNavigation()
{
throw new NotImplementedException();
}
public void Suspending()
{
throw new NotImplementedException();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment