[英]UWP Frame Navigation
我在xaml中创建了MainPage,并且它具有一个Frame,可以替换导航到另一个页面的内容。
这是我的MainPage中的框架:
<SplitView.Content>
<Frame x:Name="contentFrame" x:FieldModifier="public"/>
</SplitView.Content>
我按一下按钮,然后更改内容:
private void createResume_Click(object sender, RoutedEventArgs e)
{
contentFrame.Navigate(typeof(CreateResume));
}
到现在为止,它运作良好。 框架内容将具有CreateResume.xaml页面:
<Grid Background="Black">
<Grid.RowDefinitions>
<RowDefinition Height="150"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Text="Full Name:" Foreground="White" FontSize="20" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="5" />
<TextBox Grid.Column="1" x:Name="fullName" PlaceholderText="Type your full name here" Width="250" HorizontalAlignment="Left" VerticalAlignment="Bottom" Margin="5" />
<TextBlock Grid.Row="1" Text="Email:" Foreground="White" FontSize="20" HorizontalAlignment="Right" Margin="5" />
<TextBox Grid.Row="1" Grid.Column="1" x:Name="email" PlaceholderText="Type your email here" HorizontalAlignment="Left" Width="250" Margin="5" />
<TextBlock Grid.Row="2" Text="City:" Foreground="White" FontSize="20" HorizontalAlignment="Right" Margin="5" />
<TextBox Grid.Row="2" Grid.Column="1" x:Name="city" PlaceholderText="Type your city here" HorizontalAlignment="Left" Width="250" Margin="5" />
<TextBlock Grid.Row="3" Text="State:" Foreground="White" FontSize="20" HorizontalAlignment="Right" Margin="5" />
<TextBox Grid.Row="3" Grid.Column="1" x:Name="state" PlaceholderText="Type your state name here" HorizontalAlignment="Left" Width="250" Margin="5" />
<Button Grid.Row="4" Content="Save and Continue" Background="White" Foreground="DarkBlue" FontSize="20" HorizontalAlignment="Right" Click="Button_Click" />
</Grid>
在CreateResume.xaml中,单击我的按钮,然后尝试调用另一个页面来替换框架内容:
MainPage mainPage = new MainPage();
mainPage.contentFrame.Navigate(typeof(EducationInfo));
此时,我收到一个错误:“发生了'System.AccessViolationException'类型的未处理异常”
我已将框架公开,那么为什么会出现访问冲突异常?
mainPage是新页面,但不显示在Window中。
如果要导航到其他页面,则应与要导航的mainPage对话,并使mainPage导航到其他页面。
好的方法是使用MVVM。
第一个:您可以将MainPage绑定到MainViewModel,并且MainViewModel必须将子视图模型绑定为CreateResumeViewModel和EducationInfoViewModel
我认为您可以在App.xaml中编写ViewModel
该代码在App.xaml.Resource中
<Application
x:Class="MehdiDiagram.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MehdiDiagram"
RequestedTheme="Light">
<Application.Resources>
<local:ViewModel x:Key="ViewModel"></local:ViewModel>
</Application.Resources>
</Application>
您可以将ViewModel绑定到MainPage。
<Page
x:Class="MehdiDiagram.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MehdiDiagram"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
DataContext="{StaticResource ViewModel}"
mc:Ignorable="d">
并将CreateResumeViewModel绑定到CreateResume。
对于ViewModel,有两个属性,一个是CreateResumeViewModel。
与MainPage一样,您可以将CreateResumeViewModel属性绑定到CreateResume。
DataContext="{Binding Source={StaticResource ViewModel},Path=CreateResumeViewModel}"
CreateResumeViewModel有一个事件,该事件调用导航到其他页面。
ViewModel还具有一个事件,该事件调用导航到其他页面。
该代码在ViewModel中:
class ViewModel
{
public ViewModel()
{
CreateResumeViewModel.NavigateToPage += (sender, type) =>
{
NavigateToPage?.Invoke(sender, type);
};
}
public event EventHandler<Type> NavigateToPage;
public CreateResumeViewModel CreateResumeViewModel{ get; set; }=new CreateResumeViewModel;
}
该代码在MainPage中。
public MainPage()
{
this.InitializeComponent();
ViewModel = (ViewModel) DataContext;
ViewModel.NavigateToPage += ViewModel_NavigateToPage;
}
private void ViewModel_NavigateToPage(object sender, Type e)
{
Frame.Navigate(e);
}
您可以在CreateResumeViewModel中导航页面时使用NavigateToPage事件。
参见: https : //github.com/lindexi/UWP/tree/master/uwp/src/Framework
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.