Как я могу ссылаться на свои UserControls, чтобы отображать их на панели настроек?
Я пытаюсь реализовать пользовательские настройки для своего приложения Магазина Windows. У меня есть пользовательские элементы управления, такие как User ControlAppBarColors.xaml:
<UserControl x:Name="userControlAppBarColor"
x:Class="Visits.UserControlAppBarColors"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Popup x:Name="popupAppBarColors" IsLightDismissEnabled="True"
Closed="PopupAppBarColors_OnClosed" HorizontalAlignment="Right" Width="345">
<Popup.Transitions>
<TransitionCollection>
<PaneThemeTransition/>
</TransitionCollection>
</Popup.Transitions>
<Grid Name="gridAppBarColors" Background="Brown" Width="345">
<StackPanel VerticalAlignment="Top" Margin="21,30,0,0" Orientation="Horizontal">
<StackPanel>
<Button Content="Go Back" Click="BackButton_Click" />
<TextBlock Text="App Bar Color" TextWrapping="Wrap" MaxWidth="144"
FontWeight="Normal" Style="{StaticResource SubheaderTextBlockStyle}" Margin="-2,-2,0,0" />
</StackPanel>
<StackPanel Margin="13,0,0,0">
<StackPanel Orientation="Horizontal">
<Canvas Background="Aqua" Width="20" Height="20"
VerticalAlignment="Center" Tapped="CanvasColor_Tapped"></Canvas>
<TextBlock Text="Aqua" VerticalAlignment="Center"></TextBlock>
</StackPanel>
<StackPanel Orientation="Horizontal">
<Canvas Background="Black" Width="20" Height="20"
VerticalAlignment="Center"></Canvas>
<TextBlock Text="Black" VerticalAlignment="Center"></TextBlock>
</StackPanel>
. . .
Основываясь на книге Адама Натана, на страницах 503-507, я добавил этот код в App.xaml.cs:
public App()
{
this.InitializeComponent();
this.Suspending += OnSuspending;
SettingsPane.GetForCurrentView().CommandsRequested += OnCommandsRequested;
}
private void OnCommandsRequested(SettingsPane sender, SettingsPaneCommandsRequestedEventArgs
spcreArgs)
{
spcreArgs.Request.ApplicationCommands.Add(new SettingsCommand(1, "App Bar Color",
OnSettingsCommand));
. . .
}
// Finish this; see Nathan p. 504-507
private void OnSettingsCommand(Windows.UI.Popups.IUICommand command)
{
int id = (int) command.Id;
switch (id)
{
case 1:
break;
. . .
}
}
... и это в MainPage.xaml (показывается в контексте после объявления "Grid"):
. . .
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<!--next four lines from Nathan p. 506-->
<local:UserControlAppBarColors x:Name="appBarColors" />
. . .
... но в операторе switch в App.xaml.cs ничего из следующего не работает - все говорят: "Не удается разрешить символ":
// Failure 1:
case 1:
this.appBarColors.Show(command); // name from MainPage.xaml
break;
// Failure 2:
case 1:
this.userControlAppBarColor.Show(command); // name of user control from
UserControlAppBarColors.xaml
break;
// Failure 3:
case 1:
this.popupAppBarColors.Show(command); // name of user control's popup from
UserControlAppBarColors.xaml
break;
Что я здесь недопонимаю?
ОБНОВИТЬ
Оказывается, у меня был первый раздел кода не в том месте: вместо App.xaml.cs он должен быть в MainPage.xaml.cs - теперь он работает!
Кстати, моя первая идея (appBarColors.Show(команда)) - это то, что работает.
2 ответа
Дамир Арх дал здесь ответ
Оказывается, у меня был первый раздел кода не в том месте: вместо App.xaml.cs он должен быть в MainPage.xaml.cs - теперь он работает!
Кстати, моя первая идея (appBarColors.Show(команда)) - это то, что работает.
Ты используешь case 1:
для всех ярлыков дела в switch
построить. использование case 2:
или соответствующий ярлык. Метки на корпусе должны быть уникальными.