我正在尝试在这样的welcomePage.cs文件中使用string user = textbox1.Text;
,string user = MainPage.textbox1.Text;
我一直收到错误:
此处更新了照片:http://imgur.com/a/ZCfdm
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
namespace Gregoua_Technologies
{
public sealed partial class welcomePage : Page
{
public MainPage mpInstance;
public welcomePage(MainPage mp)
{
this.InitializeComponent();
//Will put the MainPage that this was called from into a variable
this.mpInstance = mp;
}
public void test_TextChanged(object sender, TextChangedEventArgs e)
{
//Get the instance of main page, and get the textbox1 text.
string user = this.mpInstance.textbox1.Text;
box.Text = "User: " + user;
}
public void priv_Clicked(object sender, PointerRoutedEventArgs e)
{
this.Frame.Navigate(typeof(TermsAndConditions), null);
}
public void function_Clicked(object sender, PointerRoutedEventArgs e)
{
this.Frame.Navigate(typeof(FunctionPage), null);
}
public void terms_Clicked(object sender, PointerRoutedEventArgs e)
{
this.Frame.Navigate(typeof(TermsAndConditions), null);
}
}
}
我的帖子主要是代码我猜这里你去staack溢出
答案 0 :(得分:0)
静态变量和实例变量之间存在差异。 textbox1
是一个实例变量(在MainPage类中),因此您需要使用创建的MainPage实例(通常在加载应用程序时)访问它。
您需要将上一页(MainPage)中的信息传递给welcomePage。
将welcomePage()
更改为:
public string username;
public welcomePage()
{
this.InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
this.username = e.Parameter as string;
}
public void test_TextChanged(object sender, TextChangedEventArgs e)
{
//string username = test.Text;
box.Text = "User: " + this.username;
}
请务必将您的用户名从您的MainPage类传递给welcomePage:
this.Frame.Navigate(typeof(welcomePage), this.textbox1.Text);
答案 1 :(得分:0)
您需要创建MainPage
的实例(并确保textbox1
成员不是私有的),或者您需要将textbox1
成员设置为静态。这是直接从另一个类访问textbox1.Text
的两种方法。
实际上在您的情况下,您应该在MainPage
类中创建welcomePage
的实例,因为另一个选项可能会导致线程安全问题。
答案 2 :(得分:0)
使用公共静态字段
public static string UserName { get; set; }
public void test_TextChanged(object sender, TextChangedEventArgs e)
{
string user = MainPage.textbox1.Text;
welcomePage.UserName = test.Text;
box.Text = "User: " + welcomePage.UserName;
}