Windows phone 7 如何更改Windows Phone 7应用程序中其他类的UI属性?

Windows phone 7 如何更改Windows Phone 7应用程序中其他类的UI属性?,windows-phone-7,Windows Phone 7,请让我知道如何更改Windows Phone 7应用程序中其他类的UI属性(例如textblock、textbox等) 我的代码图像如下所示: MainPage.xaml.cs public partial class MainPage : PhoneApplicationPage { // Constructor public MainPage() { InitializeComponent(); //call other class

请让我知道如何更改Windows Phone 7应用程序中其他类的UI属性(例如textblock、textbox等)

我的代码图像如下所示:

MainPage.xaml.cs

public partial class MainPage : PhoneApplicationPage
{
    // Constructor
    public MainPage()
    {
        InitializeComponent();

        //call other class method
        OtherClass a = new OtherClass();
        //Update method is executed different thread.
        a.Update();
    }
}
OtherClass.cs

public OtherClass
{
    //execute the method in async.
    Thread a = new Thread(new TreadStart(Update));
    a.Start();

    public static void Update()
    {
        //Can I do that?? (textBlock1 is MainPage's property.)
        textBlock1.Text = "abc";
    }
}

从其他类很好地访问GUI不是好的应用程序设计。您应该考虑使用MVVM设计模式。然后,“其他类”将成为视图模型,视图将使用数据绑定来检索数据


如果您仍然想使用您的方法,那么请尝试在主页上使用Singleton模式。

首先-我希望我从一开始就采用MVVM和数据绑定,这将是一个PITA,因此如果您还没有完成太多代码,最好改成官方模式

如果没有,我现在就这样做,并将其转化为您的示例:

MainPage.xaml.cs上的UI线程:

..
a.Update(this); //this = reference to MainPage
其他类别:

我觉得这很难看,我不知道传递MainPages对象是否会有很大的开销,但它应该是线程保存(多亏了..BeginInvoke)

考虑使用MVVM;D


弗兰克

谢谢。我将使用MVVM paten。
public static void Update(PhoneApplicationPage mainRef)
    {
     Deployment.Current.Dispatcher.BeginInvoke(() =>      //this will async. execute in UI
        {((MainPage)mainRef).textBlock1.Text = "abc";} ); //casting mainRef to MainPage
    }                                                     //and access textBlock1