简体   繁体   中英

Binding on Windows Phone 7 does not work

I have a strange binding problem on WP 7. Code works on WP8 without problems but when I run the same (following) code on WP7 binding does not work and TextBlock.Text is "". Here is the code (binding is set on the Text property of the second TextBlock):

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

...

Then I set the DataContext in the OnNavigatedTo method (or in the constructor, problem is the same)...

// 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...
    }

(part of) ClimateSettingsViewModel class:

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));
        }
    }
}

App platform is WP OS 7.1. Thanks in advance!

After further investigation, Windows Phone 7 and Windows Phone 8 have differently implemented reflection.

On Windows Phone 7, if you try to access private or internal functions, you will get a MethodAccessException but on the Windows Phone 8 it will just work.

Just turn on all exceptions when debugging and this error will jump up.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM