簡體   English   中英

Windows Phone 7上的綁定不起作用

[英]Binding on Windows Phone 7 does not work

我在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。 提前致謝!

經過進一步調查后,Windows Phone 7和Windows Phone 8實施了不同的反射。

在Windows Phone 7上,如果您嘗試訪問私有或內部功能,則將獲得MethodAccessException但在Windows Phone 8上它將正常工作。

調試時只需打開所有異常,此錯誤就會跳起。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM