有一次解釋一下,我通過“frame.content”打開了一個 xaml 頁面,在我打開的這個頁面中,我想打開另一個頁面,但在第二個頁面運行的框架上。但是打不開頁面
沒發生什么事。甚至沒有期待。
所以這里是我寫的:
這是來自打開的頁面的類
private void bttn_start(object sender, RoutedEventArgs e) { MainWindow mw = new MainWindow(); mw.JoinNextPage(); }
這是框架所在的 MainWindow 類。
public partial class MainWindow : Window { public void JoinNextPage() => pageMirror.Content = new page_finish(); }
我將不勝感激任何有用的提示,因為昨天我花了幾個小時在互聯網上查找,但一無所獲。
uj5u.com熱心網友回復:
嘗試這個:
private void bttn_start(object sender, RoutedEventArgs e)
{
MainWindow mw = (MainWindow)Application.Current.MainWindow;
mw.JoinNextPage();
}
uj5u.com熱心網友回復:
您應該使用RoutedCommand來觸發Frame導航而不是靜態 MainWindow 參考。
這將從您的頁面中洗掉完整的導航邏輯(按鈕事件處理程式)。
主視窗.xaml.cs
public partial class MainWindow : Window
{
public static RoutedCommand NextPageCommand { get; } = new RoutedCommand("NextPageCommand", typeof(MainWindow));
public MainWindow()
{
InitializeComponent();
CommandBindings.Add(
new CommandBinding(NextPageCommand, ExecuteNextPageCommand, CanExecuteNextPageCommand));
}
private void CanExecuteNextPageCommand(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}
private void ExecuteNextPageCommand(object sender, ExecutedRoutedEventArgs e)
{
// Logic to select the next Frame content
JoinNextPage();
}
}
主視窗.xaml
<Window>
<Frame>
<Frame.Content>
<Page>
<Button Command="{x:Static local:MainWindow.NextPageCommand}"
Content="Next Page" />
</Page>
</Frame.Content>
</Frame>
</Window>
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/317074.html
