Как передать данные с одной страницы на следующую? - PullRequest
1 голос
/ 16 января 2020

Я очень плохо знаком с Xamarin , и я искал способ сохранить текст из редактора на главной странице, а затем отобразить его на второй странице. Я знаю, что это не может быть так сложно, но я не могу найти решение. Некоторые из других вариантов, которые я видел, были для сохранения текстового файла, и я действительно не хочу go этот маршрут. Однако, если это единственный способ, чем я буду.

Вот мой код:

MainPage.xaml

<?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>

MainPage.xaml.cs

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());
        }
    }
}

Page1.xaml

    <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"
             mc:Ignorable="d"
             x:Class="Counter.Page1">
    <ContentPage.Content>
        <StackLayout BackgroundColor="White" Padding="60" VerticalOptions="Center">
            <Button Text="Main Page" 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.Content>
</ContentPage>

1 Ответ

1 голос
/ 16 января 2020

Если вы хотите передать только текст из Editor in MainPage в Page1, вы можете:

дать имя вашему Editor в XAML

<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!
        ...
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...