2014-04-20 39 views
1

我是WP8的初学者。
我有一个问题...
例如:
我有2个页面在我的Windows Phone应用程序,并在第一页上我有3个按钮,并在第二页上我有一个TextBlock控件。
如何通过单击第一页上的按钮来更改第二页上的文本属性?如何更改WP7和WP8中TextBlock的Text属性?

我只知道如何更改文本时控件都在同一页上:

private void button1_Click(object sender, RoutedEventArgs e) 
{ 
    textBlock1.Text= "Hello!"; 
} 

回答

0

这似乎对我来说,坏的做法,但它可以通过与获取和存储实例的页来完成文本块中的一个全局变量,并从instance.eg

public Page myFirstPage = null; 

public partial class FirstPage : Page 
    { 
    FirstPage() 
     { 
      initializeComponent(); 
     } 

    void onLoadedEvent(Object Sender, Eventargs e) 
    { 
     myFirstPage = this; 
    } 
    } 

然后你就可以这样做

myFirstPage.TextBlock1 = "FooBar"; 
访问任何其他网页的每一件事访问文本块
0

你可以用MVVM的方式解决这个问题。有一个适当的视图模型来存储你的数据:

public class MyViewModel 
{ 
    public String WelcomeText { get; set; } 
} 

声明类型上述视图模型的财产App.xaml.cs,这样就可以在应用程序页面共享相同的数据:

public MyViewModel MyViewModel = new MyViewModel(); 

同时设置页面到的DataContext同视图模型:

:在第2页

public Page1() 
{ 
    InitializeComponents(); 
    this.DataContext = App.MyViewModel; 
} 

public Page2() 
{ 
    InitializeComponents(); 
    this.DataContext = App.MyViewModel; 
} 

数据绑定正文块

<TextBlock Text="{Binding WelcomeText}"/> 

然后在第1页的按钮单击事件处理程序,只需更新视图模型属性:

private void button1_Click(object sender, RoutedEventArgs e) 
{ 
    App.MyViewModel.WelcomeText= "Hello!"; 
} 
+0

我在WP8初学者...有没有解决这样的问题的另一个简单的办法?我在网上搜索它,但对我来说结果很困难。有人有什么主意吗? – user3553509

相关问题