windows 8 metro app 中 选中某个 item 弹出 appbar 的方法
2013-06-15 11:36
429 查看
最近写的程序想模拟win8中的右键单击某个磁贴就会弹出一个appbar来当作菜单设置的功能,就是类似如下的效果:
查了好久没弄好,最终让我试出来了,只需要更改一下属性即可。
以下是我的appbar的初始xaml代码:
代码中包含五个按钮,其中有两个是暂时不可见的,我要实现的功能是当右键单击(触发selectionchanged事件)某个Grid的某个item的时候,马上弹出appbar,进行相应的操作。
当触发selectionchanged事件的时候,如果选中了一个那么
indexNum = IndexView.SelectedIndex;
就会得到一个索引值,如果取消选中就会得到-1,所以在得到非-1值的时候,让remove和removeAll两个按钮可见,并让整个appbar可见(isOpen=true);在-1的时候,让其不可见,综上,只要加入这么几行代码:
相关c#代码如下:
最终效果:
在未选中之前:
选中Grid中的item之后:
查了好久没弄好,最终让我试出来了,只需要更改一下属性即可。
以下是我的appbar的初始xaml代码:
<Page.BottomAppBar> <AppBar Background="#FF00B7EF" BorderBrush="#FF00B7EF" Name="myAppBar"> <AppBar.Content> <Grid> <StackPanel Orientation="Horizontal" HorizontalAlignment="Left"> <Button Visibility="Collapsed" Name="remove" AutomationProperties.Name="删除该项目" Style="{StaticResource RemoveAppBarButtonStyle}" Click="remove_Click"/> <Button Visibility="Collapsed" Name="removeAll" AutomationProperties.Name="删除所有项目" Style="{StaticResource RemoveAppBarButtonStyle}" Click="removeAll_Click"/> </StackPanel> <StackPanel Orientation="Horizontal" HorizontalAlignment="Right"> <Button Name="about" AutomationProperties.Name="关于" Style="{StaticResource HomeAppBarButtonStyle}" Click="about_Click"></Button> <Button Name="setting" AutomationProperties.Name="设置" Style="{StaticResource SettingsAppBarButtonStyle}" Click="setting_Click"></Button> <Button Name="export" AutomationProperties.Name="导出为文本文件" Style="{StaticResource DownloadAppBarButtonStyle}" Click="export_Click"></Button> </StackPanel> </Grid> </AppBar.Content> </AppBar> </Page.BottomAppBar>
代码中包含五个按钮,其中有两个是暂时不可见的,我要实现的功能是当右键单击(触发selectionchanged事件)某个Grid的某个item的时候,马上弹出appbar,进行相应的操作。
当触发selectionchanged事件的时候,如果选中了一个那么
indexNum = IndexView.SelectedIndex;
就会得到一个索引值,如果取消选中就会得到-1,所以在得到非-1值的时候,让remove和removeAll两个按钮可见,并让整个appbar可见(isOpen=true);在-1的时候,让其不可见,综上,只要加入这么几行代码:
if (indexNum != -1) { remove.Visibility = Windows.UI.Xaml.Visibility.Visible; removeAll.Visibility = Windows.UI.Xaml.Visibility.Visible; myAppBar.IsOpen = true; } else { remove.Visibility = Windows.UI.Xaml.Visibility.Collapsed; removeAll.Visibility = Windows.UI.Xaml.Visibility.Collapsed; }
相关c#代码如下:
int indexNum = 0;
private void remove_Click(object sender, RoutedEventArgs e)
{
myIndex.RemoveAt(indexNum);
remove.Visibility = Windows.UI.Xaml.Visibility.Visible;
removeAll.Visibility = Windows.UI.Xaml.Visibility.Visible;
}
private void select(object sender, SelectionChangedEventArgs e)
{
indexNum = IndexView.SelectedIndex;
if (indexNum != -1) { remove.Visibility = Windows.UI.Xaml.Visibility.Visible; removeAll.Visibility = Windows.UI.Xaml.Visibility.Visible; myAppBar.IsOpen = true; } else { remove.Visibility = Windows.UI.Xaml.Visibility.Collapsed; removeAll.Visibility = Windows.UI.Xaml.Visibility.Collapsed; }
}
private async void removeAll_Click(object sender, RoutedEventArgs e)
{
MessageDialog md = new MessageDialog("确定删除所有日记?");
md.Commands.Add(new UICommand("确定", command =>
{
myIndex.Clear();
}));
md.Commands.Add(new UICommand("取消", command =>
{
//do nothing
}));
await md.ShowAsync();
}
最终效果:
在未选中之前:
选中Grid中的item之后:
相关文章推荐
- Windows 8 Metro App开发[3]应用程序栏(AppBar)的使用
- windows 8 metro app 读取本地json的方法
- Windows 8 Metro App开发[5]导航栏(AppBar)的使用
- 引用 CTreeCtrl中用右键选中item并弹出菜单的方法
- 引用 CTreeCtrl中用右键选中item并弹出菜单的方法(转)
- windows 8 Metro 设置选中ListViewItem 的样式
- Windows 8 Metro App开发[4]弹出画面(Flayouts)
- windows自带截图工具snippingtool弹出未在计算机上运行错误提示的解决方法
- Android开发 activity里面的WebView加载js调用windows.location.reload()会弹出浏览器的解决方法
- Metro App/Windows 8 递归读取"我的音乐"目录下的所有音频文件
- windows metro app c#工程调用c++写的静态库
- GridView的getChildAt(postion)获取不到指定位置的item的解决方法(动态设置指定item为选中状态)
- AD认证模式下如何通过表单登录来代替WINDOWS弹出窗口的方法
- [Phonegap+Sencha Touch] 移动开发24 打包wp8.1的App,执行时输入框聚焦弹出软键盘之后,界面上移而不恢复原位的解决的方法
- ListView选中时Item的屎黄色背景的去除方法。
- windows metro app 学习中心
- jQuery实现选中弹出窗口选择框内容后赋值给文本框的方法
- Windows 8 Metro开发疑难杂症——APP的挂起状态 恢复
- Windows Store App 加载设计视图提示Package failed updates, dependency or conflict validation. (Exception from HRESULT: 0x80073CF3)解决方法