C#/WinUI3
[WinUI3] Navigation
말하는 닭
2023. 1. 27. 00:53
1. 페이지 변경하기
네비게이션 하는 법은 간단하다
//BlankPage1.xaml.cs
private void NextPage(object sender, RoutedEventArgs e) {
this.Frame.Navigate(typeof(BlankPage2));
}
버튼을 누를 때, 위와 같이 작성하면 페이지가 잘 넘어간다.
//<Frame x:Name="frame" Loaded="frame_Loaded"/> - MainWindow.xaml
private void frame_Loaded(object sender, RoutedEventArgs e) {
frame.Navigate(typeof(BlankPage1));
}
//Window는 Page가 아니여서 그런지 Navigate가 안 되는 것 같다
//그래서 Frame 넣고 Loaded 때 Navigate로 BlankPage1을 불러왔다
![]() |
![]() |
2. 페이지를 넘길 때, 값 전달하기
//BlankPage1.xaml.cs
//Box는 TextBox의 이름
private void NextPage(object sender, RoutedEventArgs e) {
this.Frame.Navigate(typeof(BlankPage2), Box.Text);
}
//BlankPage2.xaml.cs
//Block은 TextBlock의 이름
protected override void OnNavigatedTo(NavigationEventArgs e) {
if(e.Parameter is string && !string.IsNullOrWhiteSpace((string)e.Parameter)) {
Block.Text = e.Parameter.ToString();
}
else {
Block.Text = "빈 내용";
}
base.OnNavigatedTo(e);
}
![]() |
![]() |
뭐.. 이렇게 하면 값도 잘 넘어간다. 실수로 겹치게 놨지만 잘 넘어간 걸 확인했으니..
근데 이 방식으로는 값을 매개변수 하나에 담아야 한다.
3. 페이지 트랜지션 바꾸기
Navigate를 하면 페이지가 아래에서 위로 올라오는 애니메이션이 적용된다.
그리고 이를 좌->우로 오게, 우->좌로 오게 바꿀 수도 있다. 두 번째 매개변수는 값 전달로, null을 집어넣어도 된다.
private void NextPage(object sender, RoutedEventArgs e) {
this.Frame.Navigate(typeof(BlankPage2), Box.Text, new SlideNavigationTransitionInfo() { Effect = SlideNavigationTransitionEffect.FromLeft});
//or
this.Frame.Navigate(typeof(BlankPage2), Box.Text, new SlideNavigationTransitionInfo() { Effect = SlideNavigationTransitionEffect.FromRight});
}
혹은 아예 애니메이션이 보기 싫을 때에는 애니메이션을 없앨 수도 있다.
private void NextPage(object sender, RoutedEventArgs e) {
this.Frame.Navigate(typeof(BlankPage2), Box.Text, new SuppressNavigationTransitionInfo());
}