如何将数据从一页传递到下一页?

时间:2020-01-15 21:24:22

标签: c# xaml xamarin xamarin.forms

我对 Xamarin 来说还很陌生,我一直在寻找一种方法来存储文本,该文本来自主页的编辑器,然后在第二页上显示。我知道这并不难,但我找不到解决方案。我看到的其他一些选项是用于保存文本文件的,我真的不想走这条路。但是,如果这是我唯一的方法。

这是我的代码:

MainPage.xaml

this.dataComponent.dataTable.dataSource = this.personService.getPersonsList();

MainPage.xaml.cs

<?xml version="1.0" encoding="utf-8" ?>
    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:Controls="http://xamarin.com/schemas/2014/forms"
             mc:Ignorable="d"
             x:Class="Counter.MainPage">

    <StackLayout BackgroundColor="White" Padding="60" VerticalOptions="Start">
        <Label Text="Editor"
               x:Name="CounterLabel"
               FontSize="25"
               FontFamily="ComicSans"
               HorizontalOptions="Center"
            />
        <Editor Placeholder="Enter text here" AutoSize="TextChanges"/>
        <Button Text="Telepromt" Clicked="NavigateButton_OnClicked">
            <VisualStateManager.VisualStateGroups>
                <VisualStateGroup x:Name="CommonStates">
                    <VisualState x:Name="Normal">
                        <VisualState.Setters>
                            <Setter Property="Scale"
                            Value="1" />
                        </VisualState.Setters>
                    </VisualState>

                    <VisualState x:Name="Pressed">
                        <VisualState.Setters>
                            <Setter Property="Scale"
                            Value="0.99" />
                        </VisualState.Setters>
                    </VisualState>

                </VisualStateGroup>
            </VisualStateManager.VisualStateGroups>
            </Button>
        </StackLayout>

    </ContentPage>

Page1.xaml

using GalaSoft.MvvmLight.Views;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace Counter 
{
    [DesignTimeVisible(false)]

    public partial class MainPage : ContentPage
    {
        public MainPage() => InitializeComponent();

        public class RoutedEventArgs: EventArgs
        {

        }

        private async void NavigateButton_OnClicked(object sender, EventArgs e)
        {
            await Navigation.PushAsync(new Page1());
        }
    }
}

1 个答案:

答案 0 :(得分:1)

如果仅要将EditorMainPage中的文本传递到Page1,则可以:

XAML

中为您的Editor命名
<Editor x:Name = "editor" Placeholder="Enter text here" AutoSize="TextChanges"/>

Editor的文本传递到Page1构造函数

private async void NavigateButton_OnClicked(object sender, EventArgs e)
{
    await Navigation.PushAsync(new Page1(editor.Text));
}

在您的Page1构造函数中,您收到的输入为

public partial class Page1 : ContentPage
{
    String MainPageEditorText;

    public Page1(string editorText)
    {
        InitializeComponent();

        MainPageEditorText = editorText; // Now you can access MainPageEditorText from anywhere in Page1 class!
        ...