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.
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;
}
}
...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:
What is the problem here?
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, henceElement not found
.Move you code to a page without changing anything and it will work: