c# - UWP 框架导航

标签 c# xaml uwp

我在 xaml 中创建 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 对话,并使 mainPage 导航到其他页面。

最好的方法是使用 MVVM。

第一个:您可以将 MainPage 绑定(bind)到 MainViewModel,并且 MainViewModel 必须将 viewModel 子为 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 绑定(bind)到 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 绑定(bind)到 CreateResume。

对于 ViewModel 有两个属性,一个是 CreateResumeViewModel。

与 MainPage 一样,您可以将 CreateResumeViewModel 属性绑定(bind)到 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;
}

代码位于主页中。

    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

关于c# - UWP 框架导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43687209/

相关文章:

c# - .NET 中 "HttpRequest.RequestType"和 "WebRequest.Method"值的常量在哪里?

c# - 扩展treeview时需要指定控件模板还是使用默认的控件模板

c# - 在 map 上作为图钉的图像 - Windows Phone 8

c# - 数据绑定(bind)到 ListView 内部的命令在 UWP/MVVM-Light 中不起作用

c# - 将 DateTime 转换为字符串 "yyyy-mm-dd"

c# - 在 MVC 中,对于具有多个构造函数的 Controller ,我如何指示要调用哪个构造函数?

c# - 如何设置非事件窗口的样式?

c# - WPF KeyBinding 吞咽键,防止 TextBox 使用

c# - 将 map 控件的子级设置为最顶层

c# - 为什么这段代码在编译正常时会引发 'does not exist in namespace' 错误?