0

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

    <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>

...

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

// 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。提前致谢!

4

1 回答 1

0

经过进一步调查,Windows Phone 7 和 Windows Phone 8 实现反射的方式不同。

在 Windows Phone 7 上,如果您尝试访问私有或内部功能,您会得到一个MethodAccessException,但在 Windows Phone 8 上它可以正常工作。

调试的时候只要把所有异常都打开,这个错误就会跳起来。

于 2013-10-01T07:52:36.313 回答