2014-11-02 152 views
1

在点击事件中,我想在后面的代码中显示一个弹出窗口,但弹出窗口不显示?弹出窗口不显示

void PopupDisplay_Tap(object sender, System.Windows.Input.GestureEventArgs e) 
    { 
     if (sender != null) 
     { 
      p = new Popup 
      { 
       Width = 480, 
       Height = 580, 
       HorizontalAlignment = System.Windows.HorizontalAlignment.Center, 
       VerticalAlignment = System.Windows.VerticalAlignment.Center      
      }; 

      Border b = new Border(); 
      b.BorderBrush = new SolidColorBrush(Colors.Gray); 
      b.BorderThickness = new Thickness(2); 
      b.Margin = new Thickness(10, 10, 10, 10); 

      p.Child = b; 
      p.IsOpen = true; 
     } 
    } 
+0

不知也许'Popup'是走出去范围和被处置?尝试在此方法之外定义一次'Popup',然后在需要显示时调用'IsOpen = true'。只是一个猜测 - 我不是一个Windows手机开发。 – 2014-11-02 15:59:55

+0

从你的代码中我注意到了Popup没有添加到可视化树中。 – 2014-11-02 21:19:36

回答

1

想想你想弹出过像Pivot这是非常错误的顶层控制。

Popup with Pivots

如果这是一个Grid,它会弹出没有问题。为了解决这个问题,你必须把它添加到相同的视觉水平,像这样的支点:

<Grid x:Name="ContentPanel" Margin="0,0,0,0"> 
    <phone:Pivot x:Name="MainDisplay"> 
    <!-- more code --> 
    </phone:Pivot>  
</Grid> 

然后在你的代码隐藏

// I made with a thickness of 100, so we can see the border better 
Popup p; 

p = new Popup 
{ 
    Width = 480, 
    Height = 580, 
    VerticalOffset = 0 
}; 

Border b = new Border(); 
b.BorderBrush = new SolidColorBrush(Colors.Red); 
b.BorderThickness = new Thickness(100); 
b.Margin = new Thickness(10, 10, 10, 10); 
b.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch; 
b.VerticalAlignment = System.Windows.VerticalAlignment.Stretch; 

p.Child = b; 

// add it to the same level as the pivot to over ride pivot 
this.ContentPanel.Children.Add(p); 

p.IsOpen = true; 
+1

谢谢我最终做的是在我的透视控制下定义XAML中的弹出窗口,但在主网格内。然后,根据我的需要切换“IsOpen”属性,它工作正常。我感谢你的回应。 – Matthew 2014-11-03 04:26:25