2013-10-01 32 views
0

我在WP 7上有一个奇怪的绑定问题。代码在WP8上工作没有问题,但是当我在WP7绑定上运行相同(以下)代码不起作用并且TextBlock.Text是“” 。下面是代码(绑定被设置在所述第二TextBlock的文本属性):在Windows Phone 7上绑定不起作用

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,35"> 
     <ListBox x:Name="MainListBox" Margin="0,0,-12,0" SelectionChanged="MainListBox_SelectionChanged"> 

      <StackPanel x:Name="MeasurementUnitPropertyPanel" toolkit:TiltEffect.IsTiltEnabled="True" Margin="12,0,0,0" Orientation="Horizontal" MinHeight="100"> 
       <TextBlock x:Name="MeasurementUnitPropertyLabel" Width="235" Margin="0,30,0,0" HorizontalAlignment="Left" Text="{Binding Path=AppResources.MeasurementUnitPropertyLabel, Source={StaticResource LocalizedStrings}}" Style="{StaticResource PhoneTextLargeStyle}" FontSize="28"> 
        <TextBlock.Foreground> 
         <SolidColorBrush Color="Black"/> 
        </TextBlock.Foreground> 
       </TextBlock> 
       <TextBlock x:Name="MeasurementUnitPropertyValue" Width="185" Margin="0,30,0,0" TextAlignment="Right" Text="{Binding MeasurementUnit}" Style="{StaticResource PhoneTextLargeStyle}" FontSize="28"> 
        <TextBlock.Foreground> 
         <SolidColorBrush Color="{StaticResource DarkGrayThemeColor}"/> 
        </TextBlock.Foreground> 
       </TextBlock> 
      </StackPanel> 

...

然后我设置的DataContext在的OnNavigatedTo方法(或在构造函数,问题是一样的)...

// When page is navigated to set data context to selected item in list 
    protected override void OnNavigatedTo(NavigationEventArgs e) 
    { 
     viewModel = new ClimateSettingsViewModel(); 
     DataContext = viewModel; 
     //MeasurementUnitPropertyValue.DataContext = viewModel.MeasurementUnit; //This does not work too... 

     //Other stuff... 
    } 

(的一部分)ClimateSettingsViewModel类:

class ClimateSettingsViewModel : INotifyPropertyChanged 
{ 
    /// <summary> 
    /// Sample ViewModel property; this property is used in the view to display its value using a Binding. 
    /// </summary> 
    /// <returns></returns> 
    public String MeasurementUnit 
    { 
     get 
     { 
      return ClimateSettings.MeasurementUnitValues[App.UserData.SelectedConfiguration.ClimateSettings.MeasurementUnit]; 
     } 
     /* 
     set 
     { 
      if (value != ClimateSettings.MeasurementUnitValues[App.UserData.SelectedConfiguration.ClimateSettings.MeasurementUnit]) 
      { 
       App.UserData.SelectedConfiguration.ClimateSettings.MeasurementUnit = value; 
       NotifyPropertyChanged("MeasurementUnit"); 
      } 
     }*/ 
    } 


    public event PropertyChangedEventHandler PropertyChanged; 
    private void NotifyPropertyChanged(String propertyName) 
    { 
     PropertyChangedEventHandler handler = PropertyChanged; 
     if (null != handler) 
     { 
      handler(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 
} 

应用平台是WP OS 7.1。提前致谢!

+0

在ClimateSettings.MeasurementUnitValues [App.UserData.SelectedConfiguration.ClimateSettings.MeasurementUnit];“行上放置断点,并检查它是否在此行上,并在执行此行后检查MeasurementUnit的值(使用f10)并更新你得到的信息 – loop

+1

我不确定它是复制/粘贴错字,但我确定你的意思是'* public * class ClimateSettingsViewModel'?它使世界上的所有不同:) –

+1

正如Toni Petrina所说,问题在于你的viewmodel类没有被标记为公共的,虽然wp8绑定系统可以绑定到未声明为public的视图模型,但wp7需要它是公共的 –

回答

0

经过进一步调查,Windows Phone 7和Windows Phone 8有不同的反思。

在Windows Phone 7上,如果您试图访问私人或内部功能,您将获得MethodAccessException,但在Windows Phone 8上它将正常工作。

只要在调试时打开所有异常,并且此错误将跳起来。

相关问题