简体   繁体   English

在Xamarin.Forms中将字符串从一页传递到另一页

[英]Passing a string from one page to another in Xamarin.Forms

All i want is to pass my Entry Text from one page to another. 我想要的只是将我的输入文本从一页传递到另一页。 Not in Xaml but in .cs file. 不是在Xaml而是在.cs文件中。

First page code 第一页代码

<Label Text="Username" TextColor="Gold" FontSize="Small"/>
<Entry Placeholder="Username" TextColor="White" x:Name="Username"/>

Username.Text will be used for the Text. Username.Text将用于文本。 I want this text on my second page. 我希望在第二页上显示此文本。

Second page where i want this Username.Text 我想要此Username.Text第二页

sqlcmd.CommandText = queryString;
sqlcmd.Parameters.AddWithValue("@n", {I want Username.Text here });

Thank You. 谢谢。

There are many ways to do this depending on how you have structured your app. 有多种方法可以执行此操作,具体取决于您的应用程序结构。

If the page is already open you can use MessagingCenter to send from one ViewModel or Page To the other 如果页面已经打开,则可以使用MessagingCenter从一个ViewModelPage发送到另一个

Sender 寄件人

MessagingCenter.Send<MainPage> (this, "Hi");

Receiver 接收器

MessagingCenter.Subscribe<MainPage> (this, "Hi", (sender) => {
    // do something whenever the "Hi" message is sent
});

If you are creating the Page , you can just pass in the text through the Page Constructor , 如果要创建Page ,则可以通过Page Constructor传递文本,

New Page Constructor 新页面构造器

public MyPage(string someText)
{
}

Sender 寄件人

await Navigation.PushAsync(new NavigationPage(new MyPage(<YourTextHere>)));

or via a Property 或通过Property

await Navigation.PushAsync(new NavigationPage(new MyPage() { SomneProperty = "blach" ));

Additional Resources 其他资源

Xamarin.Forms.INavigation.PushAsync Method Xamarin.Forms.INavigation.PushAsync方法

How the MessagingCenter Works MessagingCenter的工作方式

So i had a Tabbed Page with 5 pages in it. 所以我有一个选项卡式页面,其中有5页。 What i did is this. 我所做的就是这个。

Passed my Username.Text like this 这样传递了我的Username.Text

 Navigation.PushModalAsync(new Overview(Username.Text));

Then retrieved it like this in the Overview Page. 然后像这样在“概述”页面中对其进行检索。

 public string str = null;
    public Overview (string g)
    {
        str = g;

        this.Children.Add(new Home()
        {
            Title = "Home",
            Icon = "",
        });
        this.Children.Add(new EarnPoints(str)
        {
            Title = "Earn More",
            Icon = "",
        });

And then finally in my EarnPoints page i did this. 最后,在我的EarnPoints页面中,我做到了。

 public string sts = null;
    public EarnPoints (string g)
    {
        InitializeComponent ();

        sts = g;

        GetAccountCountFromMySQL();  
    }

    public void GetAccountCountFromMySQL()
    {
        try
        {
            MySqlConnection sqlconn;
            string connsqlstring = "Server=lightningstorerewards.com;database=sukree_dzgpt;User Id=sukree_gptuser;Password=kgausi9nMNzX;charset=utf8";
            sqlconn = new MySqlConnection(connsqlstring);
            sqlconn.Open();
            string queryString = "SELECT points FROM users WHERE username = @n";
            MySqlCommand sqlcmd = new MySqlCommand(queryString, sqlconn);
            sqlcmd.CommandText = queryString;
            sqlcmd.Parameters.AddWithValue("@n", sts);

Thanks for all your help :) 感谢你的帮助 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Xamarin.Forms中将参数从一个页面传递到另一个页面? - How to pass a parameter from one Page to another Page in Xamarin.Forms? 将数据从平台特定页面传递到Xamarin.Forms中的PCL - Passing data from platform specific page to PCL in Xamarin.Forms 如何从Xamarin.forms中的按钮单击从一个页面移动到另一个页面 - how to move from one page to another from button click in Xamarin.forms Xamarin.Forms Prism-使用导航服务从一个详细信息页面导航到另一个 - Xamarin.Forms Prism - Using navigation service to navigate from one detail page to another hybridWebView 页面从 Xamarin.Forms 中的 Javascript 打开另一个 ContentPage - hybridWebView page open another ContentPage from Javascript in Xamarin.Forms 在使用 Xamarin.Essentials 抖动检测事件时,未在 Xamarin.Forms 中从一页导航到另一页 - Not navigating from one page to another page in Xamarin.Forms while using using Xamarin.Essentials shake detect event 使用xamarin.forms将数据传递回主页 - passing data back to main page using xamarin.forms 转到 xamarin.forms 中的另一个页面后,汉堡图标消失 - Hamburger icon disappears after going to another page in xamarin.forms Xamarin.Forms使用ViewModel导航到另一页 - Xamarin.Forms Navigate to another page using ViewModel Xamarin.Forms中的本地页面 - Native Page in Xamarin.Forms
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM