UWP框架导航

时间:2017-04-28 19:01:20

标签: c# xaml uwp

我在xaml中创建了我的MainPage,它有一个Frame,我将其内容替换为导航到另一个页面。

这是我的主页中的框架:

<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'类型的未处理异常”

我公开了框架,为什么我会收到访问冲突异常?

1 个答案:

答案 0 :(得分:1)

mainPage是新页面,但它没有在Window中显示。

如果您要导航到您应该与要导航的主页对话的其他页面,并使主页导航到其他页面。

好方法是使用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有一个事件,可以调用Navigate到其他页面。

ViewModel还有一个事件,可以调用Navigate到其他页面。

代码在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

相关问题