C# 如何将数据从一页传递到下一页?
我对Xamarin非常陌生,我一直在寻找一种方法,将编辑器中的文本存储在主页上,然后显示在第二页上。我知道不会那么难,但我找不到解决办法。我看到的其他一些选项是保存文本文件,我真的不想走这条路。但是,如果这是唯一的办法,我会的 这是我的密码: MainPage.xaml Page1.xamlC# 如何将数据从一页传递到下一页?,c#,xaml,xamarin,xamarin.forms,C#,Xaml,Xamarin,Xamarin.forms,我对Xamarin非常陌生,我一直在寻找一种方法,将编辑器中的文本存储在主页上,然后显示在第二页上。我知道不会那么难,但我找不到解决办法。我看到的其他一些选项是保存文本文件,我真的不想走这条路。但是,如果这是唯一的办法,我会的 这是我的密码: MainPage.xaml Page1.xaml 如果您只想将编辑器中主页中的文本传递到第1页,您可以: 在XAML中为您的编辑器指定一个名称 将编辑器的文本传递给Page1构造函数 private async void NavigateButton_
如果您只想将
编辑器中主页
中的文本传递到第1页
,您可以:
在XAML中为您的编辑器指定一个名称
将编辑器的文本传递给Page1
构造函数
private async void NavigateButton_OnClicked(对象发送方,事件参数e)
{
等待Navigation.PushAsync(新的Page1(editor.Text));
}
在Page1
构造函数中,您收到的输入为
公共部分类第1页:内容页
{
字符串MainPageEditorText;
公共页面1(字符串编辑器文本)
{
初始化组件();
MainPageEditorText=editorText;//现在您可以从Page1类中的任何位置访问MainPageEditorText!
...
在InitializeComponent()下;我如何声明它,以便在我的Page1.xaml中使用它。@Superbia,这取决于您到底想做什么,但一种典型的方法是将其值设置为本地字段或属性(我编辑了答案以包含它!)
<?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>
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());
}
}
}
<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>