如何更改其他班级中的"Frame Content"?(C# WPF XAML)



解释一下,我通过"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();
}

您应该使用RoutedCommand来触发Frame导航,而不是使用静态MainWindow引用。

这将从页面中删除完整的导航逻辑(按钮事件处理程序)。

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();
}
}

MainWindow.xaml

<Window>
<Frame>
<Frame.Content>
<Page>
<Button Command="{x:Static local:MainWindow.NextPageCommand}" 
Content="Next Page" />
</Page>
</Frame.Content>
</Frame>
</Window>

试试这个:

private void bttn_start(object sender, RoutedEventArgs e)
{
MainWindow mw = (MainWindow)Application.Current.MainWindow;
mw.JoinNextPage();
}

最新更新