Frage Silverlight applications learning.

  • 6 марта 2012 г. 19:36
     
      С кодом

    Hi,

    I am just curious, why is Silverlight not used as much as other web languages such as HTML/CSS or javascript?

    Is it worth learning?

    Here, i am attaching a little example of silverlightApp.

    **** MAIN ****

    <UserControl x:Class="SilverlightAppRevision2.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        d:DesignHeight="300" d:DesignWidth="400"
                 xmlns:riaControls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.DomainServices"
                 xmlns:my="clr-namespace:SilverlightAppRevision2.Web"
                 xmlns:utils="clr-namespace:SilverlightAppRevision2.myClass"
                 xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">
    
        <UserControl.Resources>
            <utils:myImageConverter x:Key="ImgValConv"/>
        </UserControl.Resources>
    
        <Grid x:Name="LayoutRoot" Background="Silver">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="0.2*" />
                <RowDefinition Height="*" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <Border Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2">
                <StackPanel Orientation="Horizontal" Margin="10">
                    <sdk:Label FontSize="16" FontWeight="Bold" Foreground="AliceBlue">Choice of Color:</sdk:Label>
                    <TextBox Width="250" Height="25" x:Name="txtSearchColor" TextChanged="txtSearchColor_TextChanged"></TextBox>
                </StackPanel>
            </Border>
            <riaControls:DomainDataSource AutoLoad="True" d:DesignData="{d:DesignInstance my:Product, CreateList=true}" Height="0" LoadedData="productDomainDataSource_LoadedData" Name="productDomainDataSource" QueryName="GetProductsQuery" Width="0">
                <riaControls:DomainDataSource.DomainContext>
                    <my:DomainService1 />
                </riaControls:DomainDataSource.DomainContext>
                <riaControls:DomainDataSource.SortDescriptors>
                    <riaControls:SortDescriptor PropertyPath="Color" />
                </riaControls:DomainDataSource.SortDescriptors>
            </riaControls:DomainDataSource>
            <sdk:DataGrid Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="2" Margin="10" AutoGenerateColumns="False" ItemsSource="{Binding ElementName=productDomainDataSource, Path=Data}" Name="productDataGrid" RowDetailsVisibilityMode="VisibleWhenSelected" VerticalAlignment="Top">
                <sdk:DataGrid.Columns>
                    <sdk:DataGridTextColumn x:Name="colorColumn" Binding="{Binding Path=Color}" Header="Color" Width="SizeToHeader" />
                    <sdk:DataGridTextColumn x:Name="listPriceColumn" Binding="{Binding Path=ListPrice}" Header="List Price" Width="SizeToHeader" />
                    <sdk:DataGridTemplateColumn x:Name="modifiedDateColumn" Header="Modified Date" Width="SizeToHeader">
                        <sdk:DataGridTemplateColumn.CellEditingTemplate>
                            <DataTemplate>
                                <sdk:DatePicker SelectedDate="{Binding Path=ModifiedDate, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}" />
                            </DataTemplate>
                        </sdk:DataGridTemplateColumn.CellEditingTemplate>
                        <sdk:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Path=ModifiedDate, StringFormat=\{0:d\}}" />
                            </DataTemplate>
                        </sdk:DataGridTemplateColumn.CellTemplate>
                    </sdk:DataGridTemplateColumn>
                    <sdk:DataGridTextColumn x:Name="nameColumn" Binding="{Binding Path=Name}" Header="Name" Width="SizeToHeader" />
                    <sdk:DataGridTextColumn x:Name="productCategoryIDColumn" Binding="{Binding Path=ProductCategoryID}" Header="Product Category ID" Width="SizeToHeader" />
                    <sdk:DataGridTextColumn x:Name="productIDColumn" Binding="{Binding Path=ProductID, Mode=OneWay}" Header="Product ID" IsReadOnly="True" Width="SizeToHeader" />
                    <sdk:DataGridTextColumn x:Name="productModelIDColumn" Binding="{Binding Path=ProductModelID}" Header="Product Model ID" Width="SizeToHeader" />
                    <sdk:DataGridTextColumn x:Name="productNumberColumn" Binding="{Binding Path=ProductNumber}" Header="Product Number" Width="SizeToHeader" />
                    <sdk:DataGridTextColumn x:Name="rowguidColumn" Binding="{Binding Path=rowguid}" Header="Rowguid" Width="SizeToHeader" />
                    <sdk:DataGridTemplateColumn x:Name="sellEndDateColumn" Header="Sell End Date" Width="SizeToHeader">
                        <sdk:DataGridTemplateColumn.CellEditingTemplate>
                            <DataTemplate>
                                <sdk:DatePicker SelectedDate="{Binding Path=SellEndDate, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true, TargetNullValue=''}" />
                            </DataTemplate>
                        </sdk:DataGridTemplateColumn.CellEditingTemplate>
                        <sdk:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Path=SellEndDate, StringFormat=\{0:d\}}" />
                            </DataTemplate>
                        </sdk:DataGridTemplateColumn.CellTemplate>
                    </sdk:DataGridTemplateColumn>
                    <sdk:DataGridTemplateColumn x:Name="sellStartDateColumn" Header="Sell Start Date" Width="SizeToHeader">
                        <sdk:DataGridTemplateColumn.CellEditingTemplate>
                            <DataTemplate>
                                <sdk:DatePicker SelectedDate="{Binding Path=SellStartDate, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}" />
                            </DataTemplate>
                        </sdk:DataGridTemplateColumn.CellEditingTemplate>
                        <sdk:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Path=SellStartDate, StringFormat=\{0:d\}}" />
                            </DataTemplate>
                        </sdk:DataGridTemplateColumn.CellTemplate>
                    </sdk:DataGridTemplateColumn>
                    <sdk:DataGridTextColumn x:Name="sizeColumn" Binding="{Binding Path=Size}" Header="Size" Width="SizeToHeader" />
                    <sdk:DataGridTextColumn x:Name="standardCostColumn" Binding="{Binding Path=StandardCost}" Header="Standard Cost" Width="SizeToHeader" />
                    <sdk:DataGridTemplateColumn x:Name="thumbnailPhoto" Header="Image" Width="SizeToHeader">
                        <sdk:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <Image Source="{Binding Path=ThumbNailPhoto, Converter={StaticResource ImgValConv}}" />
                            </DataTemplate>
                        </sdk:DataGridTemplateColumn.CellTemplate>
                    </sdk:DataGridTemplateColumn>
                    <sdk:DataGridTextColumn x:Name="thumbnailPhotoFileNameColumn" Binding="{Binding Path=ThumbnailPhotoFileName}" Header="Thumbnail Photo File Name" Width="SizeToHeader" />
                    <sdk:DataGridTextColumn x:Name="weightColumn" Binding="{Binding Path=Weight}" Header="Weight" Width="SizeToHeader" />
                </sdk:DataGrid.Columns>
            </sdk:DataGrid>
            <sdk:DataPager Grid.Row="1" Grid.ColumnSpan="2" HorizontalAlignment="Center" VerticalAlignment="Bottom" Source="{Binding ElementName=productDomainDataSource, Path=Data}"
                                   Name="dataPager1" PageSize="15" DataContext="{Binding}" />
            <ListBox Grid.Row="2" Background="LightBlue" Margin="10" BorderBrush="Black" BorderThickness="5" ItemsSource="{Binding ElementName=productDomainDataSource, Path=Data}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock FontSize="16" Text="{Binding Path=Name}" Margin="0,0,5,0" FontStyle="Italic"></TextBlock>
                            <Image Source="{Binding Path=ThumbNailPhoto, Converter={StaticResource ImgValConv}}"/>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
            <StackPanel Grid.Column="1" Grid.Row="2" DataContext="{Binding ElementName=productDomainDataSource, Path=Data}">
                <TextBlock FontSize="16" FontWeight="Bold" Text="{Binding Name}" Width="130" Height="30" />
                <Image Margin="10" Height="250" Width="250" Source="{Binding ThumbNailPhoto, Converter={StaticResource ImgValConv}}" />
            </StackPanel>
        </Grid>
    </UserControl>
    

    **** Main c# ****

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using SilverlightAppRevision2.Web;
    
    namespace SilverlightAppRevision2
    {
        public partial class MainPage : UserControl
        {
            DomainService1 dc = new DomainService1();
    
            public MainPage()
            {
                InitializeComponent();
            }
    
            private void productDomainDataSource_LoadedData(object sender, System.Windows.Controls.LoadedDataEventArgs e)
            {
    
                if (e.HasError)
                {
                    System.Windows.MessageBox.Show(e.Error.ToString(), "Load Error", System.Windows.MessageBoxButton.OK);
                    e.MarkErrorAsHandled();
                }
            }
    
            private void UserControl_Loaded(object sender, RoutedEventArgs e)
            {
                productDomainDataSource.DataContext = dc;
                refreshColorDataGrid();
            }
    
            private void refreshColorDataGrid()
            {
                // ******* GetProductByColorQuery is not in this database
                //string selColor = txtSearchColor.Text;
                //dc.Load(dc.GetProductByColorQuery(selColor), LoadOperation =>
                //{
                //    if (LoadOperation.HasError)
                //    {
                //        MessageBox.Show(LoadOperation.Error.Message);
                //        LoadOperation.MarkErrorAsHandled();
                //    }
                //    this.productDataGrid.DataContext = LoadOperation.Entities;
                //}, null);
            }
    
    
            private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                //((Product)lbxProducts.SelectedItem).ProductID;
                //(int)lbxProducts.SelectedValue;
            }
    
            private void txtSearchColor_TextChanged(object sender, TextChangedEventArgs e)
            {
                dc.EntityContainer.GetEntitySet<Product>().Clear();
                refreshColorDataGrid();
            }
        }
    }
    

    **** Converter ****

    using System;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Ink;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using System.Windows.Data;
    using System.ComponentModel;
    using System.Windows.Media.Imaging;
    using System.IO;
    using Jillzhang.GifUtility;
    using Silverlight.Samples;
    
    namespace SilverlightAppRevision2.myClass
    {
        public class myImageConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
    
                //At design time there won't be an image so show a place holder
                if (DesignerProperties.IsInDesignTool)
                {
                    Rectangle rect = new Rectangle();
                    rect.Width = 70d;
                    rect.Height = 50d;
                    rect.Fill = new SolidColorBrush(Colors.LightGray);
                    WriteableBitmap bmp = new WriteableBitmap(rect, null);
                    return bmp;
                }
    
                if (value != null)
                {
                    byte[] bytearray = value as byte[];
                    using (MemoryStream stream = new MemoryStream(bytearray))
                    {
                        GifImage gif = GIFDecoder.Decode(stream);
                        if ((gif != null) && (gif.Frames.Count > 0))
                        {
                            return new WriteableBitmap(gif.Frames[0].Image);
                        }
                    }
                }
    
                return null;
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                return null;
            }
        }
    }
    

    • Перемещено Neddy Ren 8 марта 2012 г. 2:15 is not C# issue (From:Visual C# General)
    •  

Все ответы

  • 6 марта 2012 г. 20:32
     
     

    Silverlight is proprietary and requires a plugin to be installed. It is also available for a very limited number of platforms. Plugins like Flash and Silverlight are possibly doomed to go the way of the Dinosaurs when Windows 8 becomes mainstream and disables plugins by default in IE.

    Silverlight is/was a decent platform but it was never given time or effort to mature and I think that it's life is very short lived as a result of MS concentrating efforts elsewhere and SL not being very platform independent or very robust.

    If you're interested in developing applications on a very limited platform you may look at Silverlight or WinRT,

    If you wish to develop applications for all types of platforms HTML4/HTML5/CSS/JavaScript, while greatly limited will be obvious potential choices.

    Weight what you want to do, who you want to reach (intended audience) and how quickly you want to get there.

  • 8 марта 2012 г. 2:17
     
     

    Hi,

    Your questions should be asked in Sliverlight forum:

    http://forums.silverlight.net/ 

    Best Regards


    Knowledge will change the destiny.