Windows Phone 8.1 Binding 绑定值转换 Converter

xmlns:local="using:格式化数据类的引用路径"
<Page.Resources>
    <local:Clock x:Key="clock" />
    <local:HoursToDayStringConverter x:Key="booleanToDayString" />
</Page.Resources>

<Grid x:Name="LayoutRoot" Background="Transparent">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,40,0,28">
        <TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" FontSize="20"/>
        <TextBlock x:Name="PageTitle" Text="绑定值转换" FontSize="60"/>
    </StackPanel>

    <StackPanel x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"  DataContext="{StaticResource clock}">
        <TextBlock FontSize="30" Text="{Binding Hour,  Converter={StaticResource booleanToDayString}}"/>
        <TextBlock FontSize="30" Text="现在的时间是:"/>
        <TextBlock FontSize="20" Text="{Binding Hour}"/>
        <TextBlock FontSize="20" Text="小时" />
        <TextBlock FontSize="20" Text="{Binding Minute}"/>
        <TextBlock FontSize="20" Text="分钟"/>
        <TextBlock FontSize="20" Text="{Binding Second}" />
        <TextBlock FontSize="20" Text="秒"/>
    </StackPanel>
</Grid>
public class Clock: INotifyPropertyChanged {
    int hour,
    min,
    sec;

    public event PropertyChangedEventHandler PropertyChanged;

    public Clock() {
        OnTimerTick(null, null);

        DispatcherTimer tmr = new DispatcherTimer();
        tmr.Interval = TimeSpan.FromSeconds(0.1);
        tmr.Tick += OnTimerTick;
        tmr.Start();
    }

    public int Hour {
        protected set {
            if (value != hour) {
                hour = value;
                OnPropertyChanged(new PropertyChangedEventArgs("Hour"));
            }
        }
        get {
            return hour;
        }
    }

    public int Minute {
        protected set {
            if (value != min) {
                min = value;
                OnPropertyChanged(new PropertyChangedEventArgs("Minute"));
            }
        }
        get {
            return min;
        }
    }

    public int Second {
        protected set {
            if (value != sec) {
                sec = value;
                OnPropertyChanged(new PropertyChangedEventArgs("Second"));
            }
        }
        get {
            return sec;
        }
    }

    protected virtual void OnPropertyChanged(PropertyChangedEventArgs args) {
        if (PropertyChanged != null) PropertyChanged(this, args);
    }

    void OnTimerTick(object sender, object args) {
        DateTime dt = DateTime.Now;
        Hour = dt.Hour;
        Minute = dt.Minute;
        Second = dt.Second;
    }
}
public class HoursToDayStringConverter: IValueConverter {
    public object Convert(object value, Type targetType, object parameter, string language) {

        if (Int16.Parse(value.ToString()) < 12) {
            return "尊敬的用户,上午好。";
        } else if (Int16.Parse(value.ToString()) > 12) {
            return "尊敬的用户,下午好。";
        } else {
            return "尊敬的用户,中午好。";
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language) {
        return DateTime.Now.Hour;
    }
}

网友评论

无吐槽,不精彩,期待您的参与。

吐槽几句

"(必填)"

"(选填)"

"(必填,不公开)"

(必填,最多100个字符。)

先说这么多吧