【问题标题】:UWP Application Error: "Invalid binding path 'Application.Current' : Property 'Application' not found on type 'MainPage' "UWP 应用程序错误:“无效的绑定路径‘Application.Current’:在类型‘MainPage’上找不到属性‘Application’”
【发布时间】:2021-12-07 13:41:33
【问题描述】:

我的 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”的代码

**MainPage.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();

        }
    }

App.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“Application”不包含“IsSignedIn”的定义,并且找不到接受“Application”类型的第一个参数的可访问扩展方法“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;
        }
    }
}

【问题讨论】:

    标签: c# xaml uwp


    【解决方案1】:

    这里的问题是运行时正在寻找 Application.Current 作为 MainPage 的属性或子项,但事实并非如此 - 它是一个静态对象。

    我建议您绑定一个 ViewModel(在 MainWindow 中设置为您的数据上下文),然后在其中有一个读取 Application.SignedIn 的属性。像这样:

    public class MainPageViewModel
    {
        public bool SignedIn => Application.Current.IsSignedIn;
    }
    

    我还建议检查 Microsoft.Extensions.Logging 而不是使用 Console.WriteLine。

    【讨论】:

    • 抱歉,我不清楚如何将这个新类/模型与 Application 对象“链接”?对不起..
    • 请参阅编辑 1
    【解决方案2】:

    我同意@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;
        }
    }
    

    【讨论】:

    • 感谢您的详细信息!我收到名称 Application 在当前上下文中不存在的错误 - 在新的 ViewModel 类中。请参阅我在整个 ViewModel 类中粘贴的编辑 2。 \
    • @dot 您的 UWP 应用是本机 UWP 应用吗?还是 Xamarin App 等其他应用?
    • 李,在这种情况下,它是一个 WPF UWP 应用程序。
    • @dot 什么是 WPF UWP 应用程序。您能否分享更多关于这个特殊项目的细节?
    • @dot 关于这个有什么更新吗?
    猜你喜欢
    • 2020-11-08
    • 2020-04-27
    • 2018-05-15
    • 2017-03-16
    • 1970-01-01
    • 1970-01-01
    • 2011-07-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多