Windows phone 7 绑定到WP7的Silverlight中的全局变量

Windows phone 7 绑定到WP7的Silverlight中的全局变量,windows-phone-7,silverlight-4.0,Windows Phone 7,Silverlight 4.0,假设我在App.xaml.cs中定义了一个全局变量,如下所示: public static MyClass GlobalInstance = new MyClass() 然后在MainPage.xaml中,我想绑定到此类的一个属性,如下所示: <TextBlock Text="{Binding App.GlobalInstance.Property1}" VerticalAlignment="Top" Height="31" HorizontalAlignment="Left" Wid

假设我在App.xaml.cs中定义了一个全局变量,如下所示:

public static MyClass GlobalInstance = new MyClass() 
然后在MainPage.xaml中,我想绑定到此类的一个属性,如下所示:

<TextBlock Text="{Binding App.GlobalInstance.Property1}" VerticalAlignment="Top" Height="31" HorizontalAlignment="Left" Width="80"> 

这里有我遗漏的东西吗?由于某种原因,它似乎没有被正确地绑定

如果您有任何建议,我们将不胜感激


谢谢

您需要将应用程序分配给页面的DataContext

第一种方法是在页面构造函数中执行此操作:

 public MainPage()
 {
     InitializeComponent();

     DataContext = App.Current;
 }
你的装订会很好

 {Binding GlobalInstance.Property1}
第二种方法是在页面资源中引用App类

此外,请将字段实现编辑为以下内容:

 public static MyClass GlobalInstance {get; private set; }

 ...
 GlobalInstance = new MyClass(); 

感谢Ku6opr的帮助,我的问题完全出乎意料。我正在创建GlobalInstance的新实例,只需要在创建新实例后再次设置DataContext。。。