General


Page   of  2

  • 1 comments  /  posted by  Martin Mihaylov  on  Nov 28, 2008 (1 month ago)

    Have you ever noticed that the MouseLeftButtonDown and MouseLeftButtonUp events are not fired when a Silverlight button is clicked? The reason for this is that the button handles these two events itself by overriding the OnMouseLeftButtonDown  and the OnMouseLeftButtonUp  handlers. In the OnMouseLeftButtonDown  override, the Click event is raised and the MouseLeftButtonDown event is marked as handled so it couldn't bubble in the visual tree. The OnMouseLeftButtonUp  override also marks the MouseLeftButtonUp  as handled.

    This thing can be changed using the ClickMode property of the Button control. It has the following values - Hover, Press, Release. The default one is Pressed and we have already explained it. When we have ClickMode set to Release, the Click event will be raised in the OnMouseLeftButtonUp override and the MouseLeftButtonDown and MouseLeftButtonUp events will be handled inside the button again. If we set the ClickMode to Hover, the Click event will be raised with the MouseEnter event and we will also be able to use the mouse button events.

    That's it!



  • 1 comments  /  posted by  Martin Mihaylov  on  Nov 28, 2008 (1 month ago)

    When using the object element to display our Silverlight application, we can use parameters to configure it (source, onerror, background, minRuntimeVersion and etc.):

    <object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
        <param name="source" value="ClientBin/Issues.xap"/>
        <param name="onerror" value="onSilverlightError" />
        <param name="background" value="white" />
        <param name="minRuntimeVersion" value="2.0.31005.0" />
        <param name="autoUpgrade" value="true" />
        <a href="http://go.microsoft.com/fwlink/?LinkID=124807" style="text-decoration: none;">
             <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style: none"/>
        </a>
    </object>

    Some of them are predefined in the automatically created TestPages, but we can also add additional parameters like initParams, onload and etc.

    Be sure always to set the value of each parameter, otherwise browsers different than Internet Explorer will not be able to render the object element properly and the "Install Silverlight" badge will appear instead of the Silverlight application. This issue could be a showstopper when embedding Silverlight applications into a web page and is not related to the Silverlight plug-in.

     

    That's it!

  • 0 comments  /  posted by  Denislav Savkov  on  Sep 18, 2008 (3 months ago)

    Just use the following line.

    C#

    Application.Current.Host.Content.IsFullScreen = true;   

    Entering full-screen mode in Silverlight has some restrictions. To ensure that it is initiated by the user entering full-screen mode is possible only in response to one of these input events: MouseLeftButtonDown. MouseLeftButtonUp, KeyDown, KeyUp.

    That's it!

  • 0 comments  /  posted by  Denislav Savkov  on  Sep 17, 2008 (3 months ago)

    The callback function of a routed event has the second parameter e of type RoutedEventArgs or its inheritor. The Source property of this parameter holds reference to the object that raised the event. In contrast to that the sender parameter holds reference to the object where the event handler was invoked.

    C#

    void RoutedEventRaised( object sender, RoutedEventArgs e )
    {
        object source = e.Source;
    }

    That's it!

  • 6 comments  /  posted by  Denislav Savkov  on  Sep 17, 2008 (3 months ago)

    Some of the new features in C# 3.0 allow you to write shorter code that is easier to read and maintain. The following code couples have the same results.

    • Short syntax for object initialization initialization.

      C#

       

      ObjectType objectName = new ObjectType();
      objectName.PropertyName = newValue;
      objectName.PropertyName2 = newValue2;
       

       

      C#

      ObjectType objectName = new ObjectType(){ PropertyName = newValue, PropertyName2 = newValue2 };
    • Syntaxt for anonymous types

    C#

    SomeVeryLongNameOfType objectName = new SomeVeryLongNameOfType();

    C#

    var objectName = SomeVeryLongNameOfType();
    • Short syntax for collection initialization

    C#

    CollectionType collectionName = new CollectionType();
    collectionName.Add( newItem1 );
    ..
    collectionName.Add( newItemN );

    C#

    CollectionType collectionName = new CollectionType() { Item1,...,ItemN };

     

    That's it!

  • 0 comments  /  posted by  Ilia Iordanov  on  Sep 10, 2008 (3 months ago)

    Probably the first most important thing to mention here is that only inheritors of System.Windows.DependencyObject can be extended with attached properties. This is needed because either to set or to get value for a specific attached property, you need to use the methods SetValue and GetValue which are defined in the DependencyObject class.
    When you declare an attached property, many advantages from the dependency properties model are coming out of the box for you such as caching, data binding, default values, expressions, styling, property invalidation and more.

    In order to declare a attached property in Silverlight, you have to follow few simple steps as explained in the sample code below.

    C#

    public partial class MySilverlightControl : StackPanel
    {
        //1. Declare the attached property as static, readonly field in your class.
       public static readonly DependencyProperty MyPropertyProperty = DependencyProperty.RegisterAttached(
                  "MyProperty",                                 //Property name
                  typeof( string ),                             //Property type
                  typeof( MySilverlightControl ),               //Type of the dependency property provider
                  new PropertyMetadata( MyPropertyChanged ) )//Callback invoked on property value change
     
        public MySilverlightControl()
        {
            InitializeComponent();
        }
     
        //2. Define the appropriate SetXXX and GetXXX methods, 
        //where XXX should be replaced with the property name
        public static void SetMyProperty( DependencyObject obj, string propertyValue )
        {
            obj.SetValue( MyPropertyProperty, propertyValue );
        }
        public static string GetMyProperty( DependencyObject obj )
        {
            return ( string )obj.GetValue( MyPropertyProperty );
        }
     
        private static void MyPropertyChanged( object sender, DependencyPropertyChangedEventArgs args )
        {
            //Do some processing here when the attached property value has changed...
        }
     
        //Just a sample method illustrating the idea how to obtain the value
        //of the MyProperty property for a specific element.
        private void ProcessTabKey()
        {
            foreach ( UIElement element in this.Children )
            {
                string propertyValue = MySilverlightControl.GetMyProperty( element );
                //Perform some processing according to the my property value.............
            }
        }
    }

    Read more about DependencyObject and DependencyProperty classes on MSDN.
    If you need more information about the attached properties check out this article 'Attached properties in Silverlight'.

    That's it!

  • 0 comments  /  posted by  Ilia Iordanov  on  Sep 10, 2008 (3 months ago)

    Probably the first most important thing to mention here is that only inheritors of System.Windows.DependencyObject can be extended with dependency properties. This is needed because either to set or to get value for a specific dependency property, you need to use the methods SetValue and GetValue which are defined in the DependencyObject class.
    When you declare a dependency property, many advantages are coming out of the box for you such as caching, data binding, default values, expressions, styling, property invalidation and more.

    In order to declare a dependency property in Silverlight, you have to follow few simple steps as explained in the sample code below.

    C#

    public partial class MySilverlightControl : UserControl
    {
        //1. Declare the dependency property as static, readonly field in your class.
        public static readonly DependencyProperty MyPropertyProperty = DependencyProperty.Register(
            "MyProperty",                               //Property name
            typeof( string ),                           //Property type
            typeof( MySilverlightControl ),             //Type of the dependency property provider
            new PropertyMetadata( MyPropertyChanged ) );//Callback invoked on property value has changes
     
        public MySilverlightControl()
        {
            InitializeComponent();
        }
     
        //2. Create the property and use the SetValue/GetValue methods of the DependencyObject class
        public string MyProperty
        {
            set
            {
                this.SetValue( MyPropertyProperty, value );
            }
            get
            {
                return ( string )this.GetValue( MyPropertyProperty );
            }
        }
     
        private static void MyPropertyChanged( object sender, DependencyPropertyChangedEventArgs args )
        {
            //Do some processing here...
        }
    }

    Read more about DependencyObject and DependencyProperty classes on MSDN.

    That's it!

  • 0 comments  /  posted by  Martin Mihaylov  on  Sep 09, 2008 (3 months ago)

    In order to invoke a JavaScript function  we must access the DOM from the Silverlight application. This can be done with the help of the System.Windows.Browser.HtmlPage class. Here it is how it's done:

    HtmlPage.Window.Invoke( "Function_Name", new object[] { //Array of paramaters that must be passed to the function } );

    We call the Invoke method of the Window property, which takes the name of the function and an array of its arguments as parameters.

    That's it!

  • 0 comments  /  posted by  Martin Mihaylov  on  Sep 08, 2008 (4 months ago)

    A definition of a color in hexidecimal format looks like this: #000000 for black and #ffffff for white. The first two symbols represent the value for red, the second two - for green and the last two - for blue. (If you're not familiar with the Hexidecimal numeral system take a look at this.) Changing these values we can get every color we like. But as you probably know the colors in Silverlight have not only Red, Green and Blue attributes but also an Alpha attribute which determines the transparency of the color.In order to use the Alpha attribute of the color we add two more digits at the start - #00000000 for black with 0% opacity (full transparency) and #ff000000 for black with 100% opacity (no transparency). Changing this value also changes the transparency.

    That's it!

  • 0 comments  /  posted by  Denislav Savkov  on  Sep 05, 2008 (4 months ago)

    There are two ways to declare objects in XAML. The most common way is to declare elements using opening and closing tag - using the object element syntax

    XAML

    <objectName><objectName/> 

    It has a shorter version

    XAML

    <objectName/>

    that can be used if the object has no child elements. See how to set a property and how to declare a child element.

    The other way is to create an object indirectly by setting a property. This syntax is used relatively rare. The type or property being set is required to support type converter that takes the string value of the property. This way the object is created from the converter using a string.

    That's it!


Page   of  2