Windows phone 7 如何使用App.xaml资源中定义的弹出窗口?
我在App.xaml资源中定义了一个弹出窗口:Windows phone 7 如何使用App.xaml资源中定义的弹出窗口?,windows-phone-7,Windows Phone 7,我在App.xaml资源中定义了一个弹出窗口: <Application.Resources> <Popup x:Key="popup"> //some content here </Popup> </Application.Resources> 出于某种原因,弹出窗口不显示? 非常感谢您的帮助。您的问题是,当您在App.xaml资源中定义弹出窗口及其内容时,您正在将其分配到与页面上显
<Application.Resources>
<Popup x:Key="popup">
//some content here
</Popup>
</Application.Resources>
出于某种原因,弹出窗口不显示?
非常感谢您的帮助。您的问题是,当您在App.xaml
资源
中定义弹出窗口及其内容时,您正在将其分配到与页面上显示的树不同的视觉树。将IsOpen
属性设置为true works不足以实际使其可见,您必须将弹出窗口添加到当前视觉树中。
第二个问题来了,因为弹出窗口
已经有一个父项
,您不能直接将其添加到页面中,因为您将得到一个无效操作异常
下面是一个可能的解决方案:
popup = App.Current.Resources["popup"] as Popup;
App.Current.Resources.Remove("popup"); // remove the PopUp from the Resource and thus clear his Parent property
ContentPanel.Children.Add(popup); // add the PopUp to a container inside your page visual tree
popup.IsOpen = true;
请注意,这样您的应用程序的资源字典中就不再有它的引用,如果您尝试后续调用此方法,它将由于NullReferenceException而失败。同样,只需一点代码,您就可以修复此问题,并在关闭弹出窗口时将其添加回资源:
popup.IsOpen=false;//之前存储的弹出窗口的本地引用
ContentPanel.Children.Remove(弹出);//从当前视觉树中删除
App.Current.Resources.Add(“弹出”,弹出);//将其添加回资源
虽然这段代码可以正常工作,并且您可以正确显示弹出窗口,但我认为仅对于PopUp
,您实际上可以在页面内部进行定义,并通过更改IsOpen
属性使其可见,这有点过分了
popup = App.Current.Resources["popup"] as Popup;
App.Current.Resources.Remove("popup"); // remove the PopUp from the Resource and thus clear his Parent property
ContentPanel.Children.Add(popup); // add the PopUp to a container inside your page visual tree
popup.IsOpen = true;