C# 如何使用全景项目选择已更改
我试图检测用户当前打开的当前全景项目,然后相应地切换应用程序栏图标按钮isenabled属性。在如何实现这样的功能或正确检测当前全景项目方面,我没有任何运气。具体来说,我希望使用selectedItem属性并检测全景项目的名称,而不是selectedIndex属性,因为全景项目可能会更改其顺序。有没有办法做到这一点?到目前为止,我掌握的情况如下:C# 如何使用全景项目选择已更改,c#,windows-phone-7,xaml,C#,Windows Phone 7,Xaml,我试图检测用户当前打开的当前全景项目,然后相应地切换应用程序栏图标按钮isenabled属性。在如何实现这样的功能或正确检测当前全景项目方面,我没有任何运气。具体来说,我希望使用selectedItem属性并检测全景项目的名称,而不是selectedIndex属性,因为全景项目可能会更改其顺序。有没有办法做到这一点?到目前为止,我掌握的情况如下: string currentPanoramaItem = ((PanoramaItem)(((Panorama)sender).SelectedIte
string currentPanoramaItem = ((PanoramaItem)(((Panorama)sender).SelectedItem)).Header.ToString();
MainPage.xaml
<controls:Panorama SelectionChanged="PanoramaItemSelectionChanged">
<!--Panorama item one-->
<controls:PanoramaItem Header="statuses" >
...
</controls:PanoramaItem>
<!--Panorama item two-->
<controls:PanoramaItem Header="mentions" >
...
</controls:PanoramaItem>
<!--Panorama item three-->
<controls:PanoramaItem Header="messages" >
...
</controls:PanoramaItem>
<!--Panorama item four-->
<controls:PanoramaItem Header="favorites" >
...
</controls:PanoramaItem>
</controls:Panorama>
由于某些原因,我的SelectionChanged实现无法工作。有什么想法吗(请举例)?我的方法是给全景命名,然后您可以参考其selectedIndex属性来确定选择了哪个全景项目。我相信这比处理标题中的字符串更有效
<Controls:Panorama name="x">...</Controls:Panorama>
private void PanoramaItemSelectionChanged(object sender, SelectionChangedEventArgs e)
{ switch (x.SelectedIndex) ... }
。。。
私有无效全景项目SelectionChanged(对象发送者,SelectionChangedEventArgs e)
{开关(x.SelectedIndex)…}
不过,我还要补充一点,在全景中使用应用程序栏不是最佳做法,事实上,建议您不要这样做,但是否这样做实际上取决于您。switch语句中使用的panoramaItemSelectionChanged字符串值应如下所示:
string currentPanoramaItem = ((PanoramaItem)(((Panorama)sender).SelectedItem)).Header.ToString();
使用标题名进行测试是个坏主意,因为如果重命名或本地化全景项目标题,代码将中断 您可以计算出所选索引,但如果您重新订购项目,则该索引将中断:
PanoramaItem selectedItem = (PanoramaItem)e.AddedItems[0];
int selectedIndex = mainPanorama.Items.IndexOf(selectedItem);
最可靠的方法是在全景项目上设置一个标记并测试:
XAML:
代码隐藏:
private void Panorama_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems.Count < 1) return;
if (!(e.AddedItems[0] is PanoramaItem)) return;
PanoramaItem selectedItem = (PanoramaItem)e.AddedItems[0];
string strTag = (string)selectedItem.Tag;
if (strTag.Equals("places"))
// Do places stuff
else if (strTag.Equals("routes"))
// Do routes stuff
}
private void Panorama\u SelectionChanged\u 1(对象发送者,selectionchangedventargs e)
{
如果(e.AddedItems.Count<1)返回;
如果(!(e.AddedItems[0]为全景项目))返回;
全景项目selectedItem=(全景项目)e.AddedItems[0];
string strTag=(string)selectedItem.Tag;
如果(标准符号等于(“位置”))
//做些什么
else if(标准标记等于(“路由”))
//做些什么
}
您也可以简单地使用索引号来触发不同的操作。因为全景页面不是为与大量全景项目一起使用而设计的。这可能不是问题
Xaml
<phone:Panorama Title="my application" SelectionChanged="Panorama_SelectionChanged">
<phone:PanoramaItem Header="first item">
</phone:PanoramaItem>
<phone:PanoramaItem Header="second item">
</phone:PanoramaItem>
<phone:PanoramaItem Header="third item">
</phone:PanoramaItem>
</phone:Panorama>
有其他的选择,以开关的情况。。。。你可以用
private void PanoramaItemSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (panoramacontrol.selecteditem == xyzpanorama)
{
//....................//
}
}
在哪里
- 全景控件是您的全景的名称
- xyzpanorama是全景图中项目的名称。例如,您的案例中的“状态”
不要与标题混淆,因为标题和名称可能不同。我的解决方案适用于WP8,但必须与WP7.x相同 首先在每个全景项目上添加一个名称,并将该名称用作代码中的引用。在我的例子中,我有x:Name=“piRegister”和x:Name=“piLogin” 在选择更改事件时,必须重新创建应用程序栏:
private void Login_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
string piName = (e.AddedItems[0] as PanoramaItem).Name;
switch(piName)
{
case "piLogin":
SetupAppBar_Signin();
break;
case "piRegister":
SetupAppBar_Register();
break;
}
}
//use this code only if you need to setup the app bar from code
void SetupAppBar()
{
ApplicationBar = new ApplicationBar();
ApplicationBar.Mode = ApplicationBarMode.Default;
ApplicationBar.Opacity = 1.0;
ApplicationBar.IsVisible = true;
ApplicationBar.IsMenuEnabled = true;
}
void SetupAppBar_Signin()
{
ApplicationBar.Buttons.Clear();
ApplicationBarIconButton button1 = new ApplicationBarIconButton();
button1.IconUri = new Uri("/icon.png", UriKind.Relative);
button1.Text = "button 1";
ApplicationBar.Buttons.Add(button1);
button1.Click += new EventHandler(button1_Click);
}
void SetupAppBar_Register()
{
ApplicationBar.Buttons.Clear();
ApplicationBarIconButton button1 = new ApplicationBarIconButton();
button1.IconUri = new Uri("/icon.png", UriKind.Relative);
button1.Text = "button 1";
ApplicationBar.Buttons.Add(button1);
button1.Click += new EventHandler(button1_Click);
ApplicationBarIconButton button2 = new ApplicationBarIconButton();
button2.IconUri = new Uri("/icon.png", UriKind.Relative);
button2.Text = "button 1";
ApplicationBar.Buttons.Add(button2);
button2.Click += new EventHandler(button2_Click);
}
尝试字符串currentPanormaItem=((全景)发送方).sellecteditem.Header@不幸的是,这也不起作用。Header不是SelectedItem的选项。在此之前,您必须将SelectedItem强制转换为PanoramaItem:
((PanoramaItem)(((PanoramaItem)sender.SelectedItem))。Header
是的,我现在看到SelectedItem是一个对象,请尝试将其强制转换为PanoramaItem,然后它就可以工作了
private void PanoramaItemSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (panoramacontrol.selecteditem == xyzpanorama)
{
//....................//
}
}
private void Login_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
string piName = (e.AddedItems[0] as PanoramaItem).Name;
switch(piName)
{
case "piLogin":
SetupAppBar_Signin();
break;
case "piRegister":
SetupAppBar_Register();
break;
}
}
//use this code only if you need to setup the app bar from code
void SetupAppBar()
{
ApplicationBar = new ApplicationBar();
ApplicationBar.Mode = ApplicationBarMode.Default;
ApplicationBar.Opacity = 1.0;
ApplicationBar.IsVisible = true;
ApplicationBar.IsMenuEnabled = true;
}
void SetupAppBar_Signin()
{
ApplicationBar.Buttons.Clear();
ApplicationBarIconButton button1 = new ApplicationBarIconButton();
button1.IconUri = new Uri("/icon.png", UriKind.Relative);
button1.Text = "button 1";
ApplicationBar.Buttons.Add(button1);
button1.Click += new EventHandler(button1_Click);
}
void SetupAppBar_Register()
{
ApplicationBar.Buttons.Clear();
ApplicationBarIconButton button1 = new ApplicationBarIconButton();
button1.IconUri = new Uri("/icon.png", UriKind.Relative);
button1.Text = "button 1";
ApplicationBar.Buttons.Add(button1);
button1.Click += new EventHandler(button1_Click);
ApplicationBarIconButton button2 = new ApplicationBarIconButton();
button2.IconUri = new Uri("/icon.png", UriKind.Relative);
button2.Text = "button 1";
ApplicationBar.Buttons.Add(button2);
button2.Click += new EventHandler(button2_Click);
}