[英]Getting frame from custom WPF user control
我正在开发WinRT 8.1应用程序,并且在自定义控件中有一个MenuFlyout。 本质上,当用户单击MenuFlyout中的某个项目时,该用户将导航到另一个页面。 我的困境是我无法访问用户控件内的Page元素。 有什么解决方法吗? 我看过许多类似的SO问题,但没有一个对我有用。
public sealed partial class BottomAppBar : UserControl {
public BottomAppBar() {
this.InitializeComponent();
//we are forced to manually add items as flyout does not support command
foreach (Vault v in User.Instance.Vaults) {
MenuFlyoutItem vault = new MenuFlyoutItem();
vault.Text = v.Name;
vault.Click += switchUser;
flyoutVault.Items.Add(vault);
}
}
private void switchUser(object sender, object e) {
//This line results in an error
this.Frame.Navigate(typeof(LoginPage));
/** Does not work as well
var parent = VisualTreeHelper.GetParent(this);
while (!(parent is Page)) {
parent = VisualTreeHelper.GetParent(parent);
}
(parent as Page).Frame.Navigate(typeof(LoginPage));
*/
}
设计模式的解决方案是创建一个将应用程序框架传递给它的导航服务,然后使用依赖注入之类的方法将导航服务传递给可能需要它的任何人。
一种简单的解决方案是将对Frame的引用存储在App类中,并通过app object / static属性访问它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.