问题描述
我有一个 Windows 8 应用程序(Xaml 和 C#):
I have a Windows 8 application (Xaml and C#):
我有一个包含在 MainPage 中的 userControl.
I have a userControl that is included in a MainPage.
在此 MainPage 中,我包含了一个可在 RightClick 和 Windows+Z 上完全运行的BottomAppBar.
In this MainPage i have included a BottomAppBar that is fully functioning on RightClick and Windows+Z.
我需要做的是从 userControl 中存在的图像上的事件处理程序(在 usercontrol 后台代码中)打开 BottomAppBar.我需要访问BottomAppBar 才能使用IsOpen 属性,但我无法这样做.任何提示?我错过了什么吗?
What i need to do is to open the BottomAppBar from an event handler (in the usercontrol back code) on an image that exists in the userControl. i need to access the BottomAppBar in order to use the property IsOpen, but i am not being able to do so. Any hint? am i missing anything?
推荐答案
我给你举个最简单的例子,可以指导你如何去做.
I am giving you simplest example, which may guide you how to do it.
BlankPage4.xaml
BlankPage4.xaml
<Page.BottomAppBar>
<AppBar IsSticky="True" IsOpen="True">
<Button Style="{StaticResource BackButtonStyle}" />
</AppBar>
</Page.BottomAppBar>
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<local:MyUserControl1 />
</Grid>
MyUserControl1.xaml
MyUserControl1.xaml
<Grid>
<Button Click="btnClose_CLick" Content="Close AppBar" />
</Grid>
MyUserControl1.xaml.cs
MyUserControl1.xaml.cs
private void btnClose_CLick(object sender, RoutedEventArgs e)
{
var isOpen = ((AppBar)((BlankPage4)((Grid)this.Parent).Parent).BottomAppBar).IsOpen;
if (isOpen)
{
((AppBar)((BlankPage4)((Grid)this.Parent).Parent).BottomAppBar).IsOpen = false;
}
else
{
((AppBar)((BlankPage4)((Grid)this.Parent).Parent).BottomAppBar).IsOpen = true;
}
}
MVVM 方式
BlankPage4.xaml
MVVM Way
BlankPage4.xaml
<Page.BottomAppBar>
<AppBar IsSticky="True" IsOpen="{Binding IsOpenBottomBar}">
<Button Style="{StaticResource BackButtonStyle}" />
</AppBar>
</Page.BottomAppBar>
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<local:MyUserControl1 />
</Grid>
MyUserControl1.xaml.cs
MyUserControl1.xaml.cs
private void btnClose_CLick(object sender, RoutedEventArgs e)
{
var isOpen = (this.DataContext as ViewModel).IsOpenBottomBar;
if (isOpen)
{
(this.DataContext as ViewModel).IsOpenBottomBar = false;
}
else
{
(this.DataContext as ViewModel).IsOpenBottomBar = true;
}
}
ViewModel.cs
ViewModel.cs
public class ViewModel : INotifyPropertyChanged
{
private bool _IsOpenBottomBar;
public bool IsOpenBottomBar
{
get
{
return _IsOpenBottomBar;
}
set
{
_IsOpenBottomBar = value;
OnPropertyChanged("IsOpenBottomBar");
}
}
public ViewModel()
{
_IsOpenBottomBar = true;
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName = null)
{
var eventHandler = this.PropertyChanged;
if (eventHandler != null)
{
eventHandler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
这篇关于如何使用用户控件返回代码中存在的事件处理程序以编程方式打开和关闭bottomappbar?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!