C# 为什么找不到此元素?

C# 为什么找不到此元素?,c#,xaml,windows-store-apps,settings,C#,Xaml,Windows Store Apps,Settings,我将代码添加到我的Windows应用商店应用程序项目中,以利用设置窗格进行应用程序的自定义设置。我基于Adam Nathan的书“使用XAML和C#的Windows 8应用程序”中的内容编写了这段代码,从第页开始。503 我在App.xaml.cs中有以下代码: public App() { this.InitializeComponent(); this.Suspending += OnSuspending; SettingsPane.GetForCurrentView

我将代码添加到我的Windows应用商店应用程序项目中,以利用设置窗格进行应用程序的自定义设置。我基于Adam Nathan的书“使用XAML和C#的Windows 8应用程序”中的内容编写了这段代码,从第页开始。503

我在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;”行上失败,原因是:

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:
System.Exception未由用户代码处理
HResult=-2147023728
Message=未找到元素。(HRESULT的异常:0x80070490)
Source=Windows.UI
堆栈跟踪:
在Windows.UI.ApplicationSettings.SettingsPane.GetForCurrentView()中
在visions.App..ctor()上
在visions.Program.b__0(ApplicationInitializationCallbackParams p)
内部异常:
当我查看详细的异常信息时,我看到:


这里有什么问题?

在您的情况下,
设置span.GetForCurrentView()
正在引发异常,因为您调用它太早了。您正在配置特定视图的设置,但在应用程序构造函数中还没有视图,因此未找到
元素

将代码移动到页面而不做任何更改,它将正常工作:

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;
    }
}

在您的情况下,
设置span.GetForCurrentView()
正在引发异常,因为您调用它太早了。您正在配置特定视图的设置,但在应用程序构造函数中还没有视图,因此未找到
元素

将代码移动到页面而不做任何更改,它将正常工作:

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;
    }
}

在您的情况下,
设置span.GetForCurrentView()
正在引发异常,因为您调用它太早了。您正在配置特定视图的设置,但在应用程序构造函数中还没有视图,因此未找到
元素

将代码移动到页面而不做任何更改,它将正常工作:

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;
    }
}

在您的情况下,
设置span.GetForCurrentView()
正在引发异常,因为您调用它太早了。您正在配置特定视图的设置,但在应用程序构造函数中还没有视图,因此未找到
元素

将代码移动到页面而不做任何更改,它将正常工作:

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;
    }
}