[英]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从一个
ViewModel
或Page
发送到另一个
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方法
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.