【问题标题】:UWP Frame NavigationUWP 框架导航
【发布时间】:2017-04-29 10:16:40
【问题描述】:

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

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

【问题讨论】:

    标签: c# xaml uwp


    【解决方案1】:

    mainPage 是新页面,但它不显示在窗口中。

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

    最好的方法是使用 MVVM。

    第一个:你可以将 MainPage 绑定到 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 绑定到 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

    【讨论】:

      猜你喜欢
      • 2020-08-06
      • 2018-01-31
      • 2023-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多