0

非常に簡単なはずの何かに問題があります。SeverityWindows Phone 8 用の洪水警告アプリを作成しています。JSON フィードから引き出された 1 から 4 までの整数があります。int を重大度に対応する文字列に変更し、その文字列を使用してバインディングを介してテキスト ブロックに入力したいだけです。これがコードです

namespace FloodAlertsApp
{


public class RootObject
{

    public int Severity { get; set; }
    public string AreaDescription { get; set; }
    public string Raised { get; set; }
    public string severityTxt;      

    public string getsevText()
    {

        switch (Severity)
        {
            case 1:
                severityTxt = "Severe";
                break;
            case 2:
                severityTxt = "Warning";
                break;
            case 3:
                severityTxt = "Alert";
                break;
            case 4:
                severityTxt = "";
                break;
        }
        return severityTxt;
    }
}

そして、バインディングを使用している xaml は次のとおりです。

<phone:PhoneApplicationPage
x:Class="FloodAlertsApp.ListView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d"
shell:SystemTray.IsVisible="True">

<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="White">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
        <!--<RowDefinition Height="Auto"/>-->
    </Grid.RowDefinitions>

    <!--TitlePanel contains the name of the application and page title-->
    <StackPanel x:Name="TitlePanel" Grid.Row="0" Width="Auto" HorizontalAlignment="Center"   Margin="10,10,10,10">
        <Image Width="Auto" Source="/nrw_logo_linear.png" Stretch="Fill"/>
    </StackPanel>

    <!--ContentPanel - place additional content here-->
    <StackPanel x:Name="ContentPanel" Grid.Row="1" Margin="0,0,0,0">

        <ListBox Name="listBoxBeaches" SelectionChanged="listBoxBeaches_SelectionChanged" Margin="10,10,10,0" Padding="0,0,0,100" Height="{Binding ElementName=ContentPanel, Path=ActualHeight}">
            <ListBox.ItemContainerStyle>
                <Style TargetType="ListBoxItem">
                    <Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
                </Style>
            </ListBox.ItemContainerStyle>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid Grid.Row="0" x:Name="TemplateLayout" Margin="0,5,0,5">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="60" />
                        </Grid.ColumnDefinitions>

                        <StackPanel Grid.Row="0" Grid.Column="0" x:Name="Stackpnl">
                            <TextBlock Foreground="Black" FontSize="28" Margin="5,0,5,0" Text="{Binding Path=AreaDescription}"></TextBlock>
Here be the binding---->    <TextBlock Foreground="Black" FontSize="28" Margin="5,0,5,0" Text="{Binding Path=SeverityTxt}"></TextBlock>
                            <TextBlock Foreground="Black" FontSize="22" Margin="5,0,5,0" Text="Raised at "></TextBlock>
                            <TextBlock Foreground="Black" FontSize="22" Margin="5,0,5,0" Text="{Binding Path=Raised}"></TextBlock>
                        </StackPanel>

                        <Ellipse Grid.Row="0" Grid.Column="1" Margin="5,0,5,0" x:Name="StatusEllipse" Height="50" Width="50" Fill="{Binding Path = Colour}" />

                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>

        </ListBox>


    </StackPanel>
</Grid>

</phone:PhoneApplicationPage>

他のバインディングは正常に機能しますが、int から文字列に変更しようとすると、バインディングが何も表示されません。

4

2 に答える 2

-1

INotifyPorpertyChangedまず、バインディングが適切に機能するように、RootObject はインターフェイスを実装する必要があります。

それが完了したら、パブリック フィールドを削除し、アクセサーseverityTxtのみを使用して (ところで、C# の命名規則に従って SeverityText と呼ばれる必要があります)という新しいプロパティを追加する必要があります。get

コードは次のようになります。

public class RootObject : INotifyPropertyChanged
{
    private int serverity;

    private string areaDescription;

    private string raised;

    public int Severity 
    {
        get
        {
            return serverity;
        }
        set
        {
            serverity = value;
            NotifyPropertyChanged("Severity");
            NotifyPropertyChanged("SeverityTxt");
        }
    }

    public string AreaDescription 
    {
        get
        {
            return areaDescription;
        }
        set
        {
            areaDescription = value;
            NotifyPropertyChanged("AreaDescription");
        }
    }

    public string Raised 
    {
        get
        {
            return raised;
        }
        set
        {
            raised = value;
            NotifyPropertyChanged("Raised");
        }
    }

    public string SeverityTxt
    {
        get 
        {
            switch (Severity)
            {
                case 1:
                    return "Severe";
                case 2:
                    return "Warning";
                case 3:
                    return "Alert";
                default:
                    return string.Empty;
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

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

severityTxtこのような実装では、 ではなくにバインドする必要がありますSeverityTxt。これは、MVVM Light や などの MVVM フレームワークの使用を検討する必要がある、より堅実なコードの基本的なソリューションですIValueConverter

于 2014-12-11T13:33:24.453 に答える