我的 Mainpage.xaml 檔案中有以下 XAML
<StackPanel Visibility="{Binding Path=IsSignedIn, Source={x:Bind Application.Current}}">
<Button Margin="10" Click="OnSignOut">
<StackPanel Orientation="Horizontal">
<SymbolIcon Symbol="Account"/>
<TextBlock Text="Sign Out" Margin="5 0 0 0"/>
</StackPanel>
</Button>
</StackPanel>
如果應用程式的“狀態”(在 IsSignedIn 變數中跟蹤)已登錄,我基本上只想顯示退出按鈕。
這是呼叫登錄邏輯值“IsSignedIn”的代碼
**主頁面.xaml.cs **
private async void OnSignIn(object sender, RoutedEventArgs e)
{
try
{
await(Application.Current as App).SignIn();
}
catch (Exception ex)
{
var messageDialog = new MessageDialog("Authentication Error", ex.Message);
await messageDialog.ShowAsync();
}
}
應用程式.xaml.cs
public async Task SignIn()
{
// First, attempt silent sign in
// If the user's information is already in the app's cache,
// they won't have to sign in again.
try
{
var accounts = await PCA.GetAccountsAsync();
var silentAuthResult = await PCA
.AcquireTokenSilent(Scopes, accounts.FirstOrDefault())
.ExecuteAsync();
Console.WriteLine("User already signed in.");
}
catch (MsalUiRequiredException msalEx)
{
// This exception is thrown when an interactive sign-in is required.
Console.WriteLine("Silent token request failed, user needs to sign-in: " msalEx.Message);
// Prompt the user to sign-in
var interactiveRequest = PCA.AcquireTokenInteractive(Scopes);
var interactiveAuthResult = await interactiveRequest.ExecuteAsync();
Console.WriteLine($"Successful interactive authentication for: {interactiveAuthResult.Account.Username}");
}
catch (Exception ex)
{
Console.WriteLine("Authentication failed. See exception messsage for more details: " ex.Message);
}
await GetUserInfo();
IsSignedIn = true;
await InitializeGraphClientAsync();
}
錯誤資訊
無效的系結路徑“Application.Current”:在“MainPage”型別上找不到屬性“Application”。MSGraphAPIDemo
對不起,如果這是一個菜鳥問題 - 這是我的第一次 XAML/UWP 嘗試。謝謝。
編輯 1
我創建了一個名為 MainPageViewModel.cs 的新類,如下所示:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MSGraphAPIDemo.DataProvider;
using Windows.UI.Xaml;
namespace MSGraphSPAPIDemo.Model
{
public class MainPageViewModel
{
public bool SignedIn = Application.Current.IsSignedIn;
}
}
但我得到的錯誤是:
CS1061“應用程式”不包含“IsSignedIn”的定義,并且找不到接受“應用程式”型別的第一個引數的可訪問擴展方法“IsSignedIn”(您是否缺少 using 指令或程式集參考?)
在 App.xaml.cs 中,我有以下代碼:
// Is a user signed in?
private bool isSignedIn;
public bool IsSignedIn
{
get { return isSignedIn; }
set
{
isSignedIn = value;
OnPropertyChanged("IsSignedIn");
OnPropertyChanged("IsSignedOut");
}
}
編輯 2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MSGraphAPIDemo.DataProvider;
using Windows.UI.Xaml;
namespace MSGraphAPIDemo.Model
{
public class MainPageViewModel
{
public bool SignedIn { get; set; }
public MainPageViewModel()
{
SignedIn = ((App)Application.Current).IsSignedIn;
}
}
}
uj5u.com熱心網友回復:
這里的問題是運行時正在尋找 Application.Current 作為 MainPage 的屬性或子項,它不是 - 它是一個靜態物件。
我建議您系結一個 ViewModel(在 MainWindow 中設定為您的資料背景關系),然后在其中包含一個讀取 Application.SignedIn 的屬性。像這樣:
public class MainPageViewModel
{
public bool SignedIn => Application.Current.IsSignedIn;
}
我還建議檢查 Microsoft.Extensions.Logging 而不是使用 Console.WriteLine。
uj5u.com熱心網友回復:
我同意@Tylor Pater 的觀點,如果您使用 ViewModel 會更好。
我制作了一個簡單的測驗演示,您可以檢查以下適用于我的代碼:Xaml:
<StackPanel Visibility="{Binding SignedIn}">
<Button Margin="10" Click="Button_Click">
<StackPanel Orientation="Horizontal">
<SymbolIcon Symbol="Account"/>
<TextBlock Text="Sign Out" Margin="5 0 0 0"/>
</StackPanel>
</Button>
</StackPanel>
主頁:
public sealed partial class MainPage : Page
{
public MainPageViewModel ViewModel { get; set; }
public MainPage()
{
this.InitializeComponent();
ViewModel = new MainPageViewModel();
this.DataContext = ViewModel;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
}
}
public class MainPageViewModel
{
public bool SignedIn { get; set; }
public MainPageViewModel()
{
SignedIn = ((App)Application.Current).IsSignedIn;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/330499.html
