我对 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());
}
}
}
答案 0 :(得分:1)
如果仅要将Editor
中MainPage
中的文本传递到Page1
,则可以:
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!
...