如何在另一个 class 中更改 "Frame Content"? (C# WPF XAML)
How can I change a "Frame Content" in another class? (C# WPF XAML)
一次解释,我通过“frame.content”打开一个xaml页面,在这个页面我打开了,我想打开另一个但是在第二页的框架上运行。
但是我打不开页面,
没有任何反应。甚至没有期望。
这里是我写的:
这是打开的页面中的 class
private void bttn_start(object sender, RoutedEventArgs e)
{
MainWindow mw = new MainWindow();
mw.JoinNextPage();
}
这是框架所在的主窗口class。
public partial class MainWindow : Window
{
public void JoinNextPage() => pageMirror.Content = new page_finish();
}
试试这个:
private void bttn_start(object sender, RoutedEventArgs e)
{
MainWindow mw = (MainWindow)Application.Current.MainWindow;
mw.JoinNextPage();
}
您应该使用 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>
一次解释,我通过“frame.content”打开一个xaml页面,在这个页面我打开了,我想打开另一个但是在第二页的框架上运行。 但是我打不开页面,
没有任何反应。甚至没有期望。
这里是我写的:
这是打开的页面中的 class
private void bttn_start(object sender, RoutedEventArgs e) { MainWindow mw = new MainWindow(); mw.JoinNextPage(); }
这是框架所在的主窗口class。
public partial class MainWindow : Window { public void JoinNextPage() => pageMirror.Content = new page_finish(); }
试试这个:
private void bttn_start(object sender, RoutedEventArgs e)
{
MainWindow mw = (MainWindow)Application.Current.MainWindow;
mw.JoinNextPage();
}
您应该使用 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>