2014-12-11 2 views
0

매우 쉽게 처리해야하는 문제가 있습니다. Windows phone 8에 대한 홍수 경고 응용 프로그램을 만들고 있습니다. 정수가 Severity인데, 이는 JSON 피드에서 가져온 1에서 4까지의 숫자가 될 수 있습니다. 단순히 int를 심각도에 해당하는 문자열로 변경 한 다음 문자열을 사용하여 바인딩을 통해 텍스트 블록을 채 웁니다. 여기에 다음 코드JSON에 의해 ​​생성 된 변수를 C로 변경하는 방법 #

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> 

이 다른 바인딩이 잘 작동하지만,의 I는 int에서 String으로 변경하려고하면 바인딩은 아무 것도 아닌 것처럼 보입니다.

+0

열거 형을 사용하지 않는 이유는 무엇입니까? –

+0

이 경우 Enum을 어떻게 사용할 수 있습니까? – Marushiru

답변

-1

먼저 RootObject는 INotifyPorpertyChanged 인터페이스를 구현해야 바인딩이 올바르게 작동합니다.

공용 필드를 제거하고 get 접근 자만 사용하여 severityTxt (BTW는 C# 명명 규칙에 따라 SeverityText라고해야 함)이라는 새 속성을 추가해야합니다.

코드는 다음과 같이 될 것입니다 : 이러한 구현으로

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)를 사용하여보다 견고한 코드를 고려해야합니다. (내가 가진 경우에 당신이 원하는 작업)

+0

코드를 시도했지만 NotifyPropertyChanged가 작동하지 않습니다. Windows Phone 8과 호환됩니까? – Marushiru

+0

코드를 사용하여 파일에'using System.ComponentModel;'을 덧붙여 야합니다. –

+0

내 말은 그게 바보 인 줄 알았어. (전체 코드를 읽지 않았습니다.) 감사합니다. – Marushiru

0

내가 거기에 생각이 열거 형에 문제 없습니다 :

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

쉬운 int를 열거 형으로 변환 할 수 있습니다.

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