C# 超宽带帧导航
我用xaml创建了我的主页,它有一个框架,我可以替换导航到其他页面的内容 这是我的主页中的框架:C# 超宽带帧导航,c#,xaml,uwp,C#,Xaml,Uwp,我用xaml创建了我的主页,它有一个框架,我可以替换导航到其他页面的内容 这是我的主页中的框架: <SplitView.Content> <Frame x:Name="contentFrame" x:FieldModifier="public"/> </SplitView.Content> 到目前为止,它工作得非常完美。框架内容将具有CreateResume.xaml页面: <Grid Background="Black">
<SplitView.Content>
<Frame x:Name="contentFrame" x:FieldModifier="public"/>
</SplitView.Content>
到目前为止,它工作得非常完美。框架内容将具有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>
此时我收到一个错误:“发生了类型为'System.AccessViolationException'的未处理异常”
我公开了框架,为什么会出现访问冲突异常?主页是新页面,但它不会显示在窗口中 如果要导航到其他页面,则应与要导航的主页对话,并使主页导航到其他页面 好的方法是使用MVVM 第一:可以将MainPage绑定到MainViewModel,MainViewModel必须作为CreateResumeViewModel和EducationInfoViewModel子viewModel 我认为您可以在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>
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;
}
您可以在CreateResumeViewModel中使用NavigateToPage事件来导航页面
见:
<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>
<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">
DataContext="{Binding Source={StaticResource ViewModel},Path=CreateResumeViewModel}"
class ViewModel
{
public ViewModel()
{
CreateResumeViewModel.NavigateToPage += (sender, type) =>
{
NavigateToPage?.Invoke(sender, type);
};
}
public event EventHandler<Type> NavigateToPage;
public CreateResumeViewModel CreateResumeViewModel{ get; set; }=new CreateResumeViewModel;
}
public MainPage()
{
this.InitializeComponent();
ViewModel = (ViewModel) DataContext;
ViewModel.NavigateToPage += ViewModel_NavigateToPage;
}
private void ViewModel_NavigateToPage(object sender, Type e)
{
Frame.Navigate(e);
}