C# 如何引用我的用户控件以在“设置”窗格中显示它们?
我正在尝试为我的Windows应用商店应用程序实施自定义设置。我有用户控件,例如UserControlAppBarColors.xaml:C# 如何引用我的用户控件以在“设置”窗格中显示它们?,c#,user-controls,windows-store-apps,winrt-xaml,settings,C#,User Controls,Windows Store Apps,Winrt Xaml,Settings,我正在尝试为我的Windows应用商店应用程序实施自定义设置。我有用户控件,例如UserControlAppBarColors.xaml: <UserControl x:Name="userControlAppBarColor" x:Class="Visits.UserControlAppBarColors" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http
<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>
. . .
…并将其发送到MainPage.xaml(在上下文中,在“网格”声明之后显示):
我在这里有什么误解或错误编码
更新
原来我的第一段代码放错地方了:它需要放在MainPage.xaml.cs中,而不是App.xaml.cs中——现在它可以工作了
顺便说一句,我的第一个想法(appBarColors.Show(command))是可行的。您正在使用
案例1:
作为开关
构造中的所有案例标签。
使用
案例2:
或适当的标签。案例标签必须是唯一的。Damir Arh给出了答案
原来我的第一段代码放错地方了:它需要放在MainPage.xaml.cs中,而不是App.xaml.cs中——现在它可以工作了
顺便说一句,我的第一个想法(appBarColors.Show(command))是可行的。不,这些只是我尝试过的所有事情的例子;我试了第一个,把它评论出来,试了第二个,等等。是的,它看起来已经有点奇怪了,但是现在每个人都有糟糕的一天。我进一步检查了一下,但唯一看到的是
Show()
不是用户控件的有效函数。它是表单的有效成员,但没有参数<代码>显示(命令)无效。您是否缺少自己的Show()
实现?我不知道这本书,也许它是在那里编码的,但不确定。
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;
. . .
}
}
. . .
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<!--next four lines from Nathan p. 506-->
<local:UserControlAppBarColors x:Name="appBarColors" />
. . .
// 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;