I BELIEVE I CAN

加油,努力,奋斗!!

导航

Fifth

     Today I am relatively easy. when I fixed a bug and test in morning, I was free, it spent about two hours. So I have more time to study new technology.

     I searched video about silverlight from base things. There are three panels in silverlight: grid, stackpanel and canvas. Grid like table, can divide by row and column, if we don't set the size of control on it, it will fill the whole panel. StackPanel like container, layout controls one by one. There is a property named 'Orientation' and contains two values 'Horizontal' and 'vertical'. Canvas like div, we should set the top and left distance and index of every controls on it. I aslo practise a demo of binding .net class in silvertlight. We used two interface about class 'INotifyPropertyChanged' and 'IValueConverter', which name space is 'System.ComponentModel' and 'System.Windows.Data'. Codes as follows:

namespace SilverlightApplication1
{
    public enum TrafficStatus
    {
        Stop,
        Ready,
        Go
    }

    public class TrafficLight : INotifyPropertyChanged
    {
        public TrafficStatus Status
        {
            get
            {
                return (status);
            }
            set
            {
                status = value;
                if(PropertyChanged!=null)
                {
                    PropertyChanged(this,new PropertyChangedEventArgs("Status"));
                }
            }
        }

        private TrafficStatus status;

        public event PropertyChangedEventHandler PropertyChanged;
    }
}

public class Converter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            TrafficStatus status = (TrafficStatus) value;

            SolidColorBrush  brush = new SolidColorBrush(Colors.Green);

            switch(status)
            {
                case TrafficStatus.Ready:
                    brush = new SolidColorBrush(Colors.Yellow);
                    break;
                case TrafficStatus.Stop:
                    brush = new SolidColorBrush(Colors.Red);
                    break;
                default:
                    break;
            }
            return (brush);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }

Html:

<UserControl xmlns:basics="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"  x:Class="SilverlightApplication1.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:custom="clr-namespace:SilverlightApplication1" 
    Width="400" Height="300">
    <UserControl.Resources>
        <custom:Converter x:Key="myConverter"/>
    </UserControl.Resources>
        <Grid x:Name="LayoutRoot" Background="White" >
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>       
        <Ellipse Fill="{Binding Status,Converter={StaticResource myConverter} }" Width="100" Height="100"/>
       
        <Button Grid.Row="2" Name="btnTest" Content="Click Me" Click="Button_Click"></Button>
    </Grid>
   
</UserControl>

 

Page.cs

 

public Page()
        {
            InitializeComponent();

            this.Loaded += new RoutedEventHandler(Page_Loaded);
        }

        void Page_Loaded(object sender, RoutedEventArgs e)
        {
            LayoutRoot.DataContext = new TrafficLight()
                                         {
                                             Status = TrafficStatus.Go
                                         };
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            TrafficLight tli = LayoutRoot.DataContext as TrafficLight;           

            int i = (int)tli.Status;

            i++;

            if (i > (int)TrafficStatus.Go)
            {
                i = 0;
            }

            tli.Status = (TrafficStatus) i;
        }

posted on 2008-12-17 16:12  朱小能  阅读(348)  评论(0)    收藏  举报