如何在c#中更改由JSON生成的变量

时间:2014-12-11 12:06:40

标签: c# json xaml xaml-binding

我遇到的问题应该很容易。我正在为Windows Phone 8创建一个洪水警告应用程序。我有一个整数Severity,可以是1到4之间的任何东西,从JSON提要中提取。我只想将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更改为字符串时,绑定显示为无。

2 个答案:

答案 0 :(得分:0)

我想Enums没有问题(如果我得到了你想要的东西):

XAML:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
   <ListView Name="TestListView">

   </ListView>
</Window>

代码:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        TestListView.ItemsSource = new[] {Severity.Severe, Severity.Alert, Severity.Warning};
    }

    enum Severity
    {
        Severe = 1,
        Warning = 2,
        Alert = 3,
        None = 4
    }
}

结果:

enter image description here

您可以将简单的整数转换为枚举,例如

TestListView.ItemsSource = new[] {1, 2, 3}.Select(i => (Severity) i);

答案 1 :(得分:-1)

首先,RootObject应该实现INotifyPorpertyChanged接口,这样绑定才能正常工作。

完成此操作后,您应该删除公共字段,并添加一个名为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框架,例如MVVM Light和\或IValueConverter