【问题标题】:Getting frame from custom WPF user control从自定义 WPF 用户控件获取框架
【发布时间】:2014-04-08 20:14:00
【问题描述】:

我正在开发一个 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));
     */
  }

【问题讨论】:

  • WPF 是指 WinRT/XAML,对吧?

标签: c# user-controls windows-runtime winrt-xaml mvvm-light


【解决方案1】:

设计模式的解决方案是创建一个导航服务,将应用框架传递给它,然后使用依赖注入之类的东西将导航服务传递给可能需要它的任何人。

简单的解决方案是将 Frame 的引用存储在您的 App 类中,并通过应用程序对象/静态属性访问它。

【讨论】:

    猜你喜欢
    • 2011-09-10
    • 2013-04-20
    • 2011-09-10
    • 1970-01-01
    • 1970-01-01
    • 2021-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多