我在Visual Studio 2017上使用Windows Template Studio创建了一个应用程序。
该应用主要是一个NavigationDrawer
,具有不同的页面。
一切正常,直到我想添加登录页面。
因此,我创建了登录页面的XAML
等。但是现在,我希望它在应用启动时显示在NavigationDrawer
页面的前面。
我寻求了有关App.xaml.cs
的一些文档,以了解要进行哪些更改,但是由于使用Windows Template Studio,因此代码不再是真正的香草。
我尝试了几件事,现在唯一能做的就是将NavigationDrawer
的外壳页面更改为我的Login
页面。
那并不是我想要的,因为我的首要目的是要在您登录之前使该应用程序不可用,并且因为NavigationDrawer
仍然可用,用户仍可以执行他想做的事情。
我的app.xaml.cs
看起来像这样:
using System;
using BasePosteMobilite.Services;
using Windows.ApplicationModel.Activation;
using Windows.UI.Xaml;
namespace BasePosteMobilite
{
public sealed partial class App : Application
{
private Lazy<ActivationService> _activationService;
private ActivationService ActivationService
{
get { return _activationService.Value; }
}
public App()
{
InitializeComponent();
// Deferred execution until used. Check https://msdn.microsoft.com/library/dd642331(v=vs.110).aspx for further info on Lazy<T> class.
_activationService = new Lazy<ActivationService>(CreateActivationService);
}
protected override async void OnLaunched(LaunchActivatedEventArgs args)
{
if (!args.PrelaunchActivated)
{
await ActivationService.ActivateAsync(args);
}
}
protected override async void OnActivated(IActivatedEventArgs args)
{
await ActivationService.ActivateAsync(args);
}
private ActivationService CreateActivationService()
{
return new ActivationService(this, typeof(ViewModels.LoginViewModel), new Lazy<UIElement>(CreateShell));
}
private UIElement CreateShell()
{
return new Views.ShellPage();
}
}
}
ShellPage.xaml.cs:
using System;
using BasePosteMobilite.ViewModels;
using Windows.UI.Xaml.Controls;
namespace BasePosteMobilite.Views
{
// TODO WTS: Change the icons and titles for all NavigationViewItems in ShellPage.xaml.
public sealed partial class ShellPage : Page
{
private ShellViewModel ViewModel
{
get { return ViewModelLocator.Current.ShellViewModel; }
}
public ShellPage()
{
InitializeComponent();
DataContext = ViewModel;
ViewModel.Initialize(shellFrame, navigationView, KeyboardAccelerators);
}
}
}
ViewModel.Initialize:
public void Initialize(Frame frame, WinUI.NavigationView navigationView, IList<KeyboardAccelerator> keyboardAccelerators)
{
_navigationView = navigationView;
_keyboardAccelerators = keyboardAccelerators;
NavigationService.Frame = frame;
NavigationService.NavigationFailed += Frame_NavigationFailed;
NavigationService.Navigated += Frame_Navigated;
_navigationView.BackRequested += OnBackRequested;
}
答案 0 :(得分:0)
您可以创建一个具有登录必需功能的项目,并且您会从ActivateAsync方法中看到以下代码:
var silentLoginSuccess = await IdentityService.AcquireTokenSilentAsync();
if (!silentLoginSuccess || !IdentityService.IsAuthorized())
{
await RedirectLoginPageAsync();
}
就是这样。如果要重定向到自己的页面,请在ActivationService.ActivateAsync(args)方法下编写检测代码。如果看到客户未登录,请调用重定向登录方法。这是template studio中有关redirectlogin的代码:
public async Task RedirectLoginPageAsync()
{
var frame = new Frame();
NavigationService.Frame = frame;
Window.Current.Content = frame;
await ThemeSelectorService.SetRequestedThemeAsync();
NavigationService.Navigate<Views.LogInPage>();
}