这是为什么找不到元素? [英] Why is this Element not Found?

查看:1173
本文介绍了这是为什么找不到元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我添加代码,以我的Windows Store应用项目,利用我的应用程序的自定义设置的设置窗格。我基于什么是亚当森的著作Windows 8的应用程序使用XAML和C#,开始页上的验证码。 503

I added code to my Windows Store App project to utilize the Settings pane for my app's custom settings. I based this code on what is in Adam Nathan's book "Windows 8 Apps with XAML and C#", beginning on p. 503.

我在App.xaml.cs这段代码:

I have this code in App.xaml.cs:

public App()
{
    this.InitializeComponent();
    this.Suspending += OnSuspending;
    SettingsPane.GetForCurrentView().CommandsRequested += OnCommandsRequested;
    UnhandledException += Application_UnhandledException;
}

private void OnCommandsRequested(SettingsPane sender, SettingsPaneCommandsRequestedEventArgs spcreArgs)
{
    spcreArgs.Request.ApplicationCommands.Add(new SettingsCommand(1, "App Bar Color", OnSettingsCommand));
    spcreArgs.Request.ApplicationCommands.Add(new SettingsCommand(2, "Visit Types to Display", OnSettingsCommand));
    spcreArgs.Request.ApplicationCommands.Add(new SettingsCommand(3, "Display Current Location", OnSettingsCommand));
    spcreArgs.Request.ApplicationCommands.Add(new SettingsCommand(4, "Set Home Base", OnSettingsCommand));
}

private void OnSettingsCommand(Windows.UI.Popups.IUICommand command)
{
    int id = (int) command.Id;
    switch (id)
    {
        case 1:
            //Bla => I think this is where I need to add "this.[custom settings pane (user control) name].Show(command)
            break;
        case 2:
            //Bla
            break;
        case 3:
            //Bla
            break;
        case 4:
            //Bla
            break;
    }
}

...但它失败 SettingsPane.GetForCurrentView()。 CommandsRequested + = OnCommandsRequested; 的符合:

...but it fails on the "SettingsPane.GetForCurrentView().CommandsRequested += OnCommandsRequested;" line with:

System.Exception was unhandled by user code
  HResult=-2147023728
  Message=Element not found. (Exception from HRESULT: 0x80070490)
  Source=Windows.UI
  StackTrace:
       at Windows.UI.ApplicationSettings.SettingsPane.GetForCurrentView()
       at Visits.App..ctor()
       at Visits.Program.<Main>b__0(ApplicationInitializationCallbackParams p)
  InnerException:

当我查看详细的异常信息,我看到:

When I view the detailed exception info, I see:

有什么问题吗?

推荐答案

在你的情况 SettingsPane.GetForCurrentView()因为你调用它抛出一个异常太早了。你是一个特定视图配置设置,但在应用程序构造没有看法呢,因此找不到元素

In your case SettingsPane.GetForCurrentView() is throwing an exception because you're calling it too early. You are configuring the settings for a specific view, but in the application constructor there is no view yet, hence Element not found.

动你的代码的网页不改变任何东西,它会工作:

Move you code to a page without changing anything and it will work:

public MainPage()
{
    this.InitializeComponent();
    SettingsPane.GetForCurrentView().CommandsRequested += OnCommandsRequested;
}

private void OnCommandsRequested(SettingsPane sender, SettingsPaneCommandsRequestedEventArgs spcreArgs)
{
    spcreArgs.Request.ApplicationCommands.Add(new SettingsCommand(1, "App Bar Color", OnSettingsCommand));
    spcreArgs.Request.ApplicationCommands.Add(new SettingsCommand(2, "Visit Types to Display", OnSettingsCommand));
    spcreArgs.Request.ApplicationCommands.Add(new SettingsCommand(3, "Display Current Location", OnSettingsCommand));
    spcreArgs.Request.ApplicationCommands.Add(new SettingsCommand(4, "Set Home Base", OnSettingsCommand));
}

private void OnSettingsCommand(Windows.UI.Popups.IUICommand command)
{
    int id = (int)command.Id;
    switch (id)
    {
        case 1:
            //Bla => I think this is where I need to add "this.[custom settings pane (user control) name].Show(command)
            break;
        case 2:
            //Bla
            break;
        case 3:
            //Bla
            break;
        case 4:
            //Bla
            break;
    }
}

这篇关于这是为什么找不到元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆