我找不到我在主页资源和样式中定义的按钮。我访问该按钮后,我想将其视觉状态更改为按下。这是我在codebehind(MainPage.xaml.cs)中尝试的:
private void OnShowPopupButtonClick(object sender, RoutedEventArgs e)
{
this.myPopup.IsOpen = !this.myPopup.IsOpen;
//attempt at accessing button resource to change state
if ((this.myPopup.IsOpen))
{
this.HomeButton.Controls.Style = this.Resources["PivotStyle1"] as Style;
VisualStateManager.GoToState(HomeButton, "Pressed", true);
}
}
HomeButton所在的层次结构(在MainPage.xaml中)如下:
<phone:PhoneApplicationPage.Resources>
...
<Style x:Key="PivotStyle1" TargetType="controls:Pivot">
....
<Setter Property="Template">
...
<Setter.Value>
<ControlTemplate TargetType="controls:Pivot">
...
<Grid>
<ContentPresenter>
<StackPanel>
<StackPanel>
<Button x:Name="HomeButton" Style="{StaticResource ButtonStyle1}" Click="OnShowPopupButtonClick" >
.....
.....
这就是我在布局中称呼我的风格的地方:
<Grid x:Name="LayoutRoot">
<controls:Pivot x:Name="MainPivotControl" Title="MyApp" Style="{StaticResource PivotStyle1}" FontFamily="myFont">
...
...
因此,当我尝试构建应用程序时,会弹出一条错误消息,指出HomeButton在当前上下文中不存在。另一条错误消息表明它没有定义或扩展方法。有谁知道我在背后的代码中做错了什么?这几天我一直在浏览网页并浏览一些教程,没有人帮忙。 感谢
private void OnShowPopupButtonClick(object sender, RoutedEventArgs e)
{
this.myPopup.IsOpen = !this.myPopup.IsOpen;
//attempt at accessing button resource to change state
if ((this.myPopup.IsOpen))
{
//This section will help locate and assign HomeButton then event function will launch
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(MainPivotControl); i++)
{
var child = VisualTreeHelper.GetChild(MainPivotControl, i);
if (child is Button)
{
var myHomeButton = child as Button;
//This is where the event procedure will be given
if (myHomeButton.Name == "HomeButton")
{
VisualStateManager.GoToState(myHomeButton, "Pressed", true);
break;
}
}
}
}
else
{
//This section will help locate and assign HomeButton then event function will launch
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(MainPivotControl); i++)
{
var child = VisualTreeHelper.GetChild(MainPivotControl, i);
if (child is Button)
{
var myHomeButton = child as Button;
//This is where the event procedure will be given
if (myHomeButton.Name == "HomeButton")
{
VisualStateManager.GoToState(myHomeButton, "Pressed", false);
break;
}
}
}
}
}
答案 0 :(得分:0)
您无法像这样访问HomeButton,因为它不是直接为您的窗口类定义的变量。要访问它,您必须在Pivot控件的可视树中搜索,如下所示:
public Button FindButton(DependencyObject parent)
{
// Confirm parent and childName are valid.
if (parent == null) return null;
Button homeButton = null;
int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < childrenCount; i++)
{
var child = VisualTreeHelper.GetChild(parent, i);
// If the child is not of the request child type child
Button button = child as Button;
if (button == null)
{
// recursively drill down the tree
homeButton = FindButton(child);
// If the child is found, break so we do not overwrite the found child.
if (homeButton != null) break;
}
else
{
// If the child's name is set for search
if (button.Name == "HomeButton")
{
// if the child's name is of the request name
homeButton = button;
break;
}
}
}
return homeButton;
}
答案 1 :(得分:0)
您可以在按钮上添加Loaded事件,并将sender参数保存在您的按钮字段中。
//button can be reused everywhere (including inside your OnShowPopupButtonClick function)
Button myPopupButton;
public myPopupButton_Loaded(object sender, RoutedEventArgs e) {
myPopupButton=sender as Button;
if ((this.myPopup.IsOpen))
{
VisualStateManager.GoToState(myPopupButton, "Pressed", true);
}
}