簡體   English   中英

從自定義WPF用戶控件獲取框架

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM