从MainPage类编辑XAML文本不 [英] editing XAML textbox not from MainPage class

查看:394
本文介绍了从MainPage类编辑XAML文本不的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有MainPage类,我可以使用此代码

I have the MainPage class which I can edit the contents of the XAML textbox from using this code

box1.Text = "";



不过试图从另一个类编辑文本框下面的代码将无法正常工作

however trying to edit the textbox from another class the following code will do not work

MainPage.box1.Text = "";



错误是是必需的非静态字段,方法或属性类的对象​​引用.MainPage.box1'
我已经试过各种喜欢做静态函数,并在其他类创建新对象的MainPage,但没有奏效。

The error is "An object reference is required for the non-static field, method or property 'class.MainPage.box1' I have tried everything like making static functions and creating new MainPage objects in the other class but nothing has worked

推荐答案

XAML文本框在MainPage.xaml中/的.cs

XAML Textbox is in MainPage.xaml/.cs

有关文本框您的价值二传手在一些X类。

Your value setter for the Textbox is in some class X.

原因下面的语句您的错误,则BOX1是不是静态的,你需要的MainPage的一个实例。

The reason for your error on below statement is, the box1 is not static and you need an instance of the MainPage.

MainPage.box1.Text = "";

但不是任何情况下你。需要当前的实例。所以在类X的方法,需要接收MainPage.xaml.cs中类的本实例,然后改变BOX1值。

But not just any instance. You need the current instance. So the method in the class X, needs to receive a "THIS" instance of MainPage.xaml.cs class and then change the box1 value.

函数调用:

X xobj=new X();
xobj.ChangeboxValue(this);

功能:

void ChangeboxValue(MainPage obj)
{
obj.box1.Text=""
}

这篇关于从MainPage类编辑XAML文本不的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆