ChiaFong

Posts Tagged ‘C#’

Finally Something to help me blog about source code

In Programming on 17 October, 2008 at 7:00 am

Yes! Finally!  CopySourceAsHtml come to the rescue.  I was just wondering why is it that putting source code on to a HTML page is so difficult and I accidentally bump into a page that talk about using CopySourceAsHtml add in for Visual Studio 2008.  It is still a bit tedious but definitely much much better than manually hand code the html tag or using CSS.  CSS can help a lot as well but when you change your web design, you’ve got to remember to add the necessary CSS back and if you are on a web site that wants you to pay for using CSS then you are out of luck.

Developer who blog should definitely check out this wonderful add in.

Advertisements

To bind or not to bind (Part 2)

In Programming on 25 July, 2008 at 7:00 am

To expand on what I’ve mentioned in my previous post.  The list box declared in XAML will look something like this:

<ListBox
    x:Name="CheckList"
    ItemTemplate="{DynamicResource CheckListTemplate}"
    ItemsSource="{Binding Source={StaticResource CheckedSource},
                  Mode=Default}">
</ListBox>

Under the <Windows.Resources>, the data template will be declared as:

<DataTemplate x:Key="CheckListTemplate">
  <StackPanel Margin="0,3,0,2" Orientation="Horizontal">
    <CheckBox IsChecked="{Binding Path=IsShown}"/>
    <TextBlock Text="{Binding Path=Name}"/>
  </StackPanel>
</DataTemplate>

At the same place or inside a style XAML’s ResourceDictionary, the data source is declared as:

<source:SColorList x:Key="CheckedSource"/>

Inside the C# file the classes are declared as:

    public class SColor : INotifyPropertyChanged
    {
        private string displayName;
        private Color clrShow;
        private bool permanent;
        private bool show;
 
        public bool IsShown
        {
            set
            {
                show = value;
                OnPropertyChanged("IsShown");
            }
            get
            {
                return show;
            }
        }
 
        public string Name
        {
            set
            {
                displayName = value;
                OnPropertyChanged("Name");
            }
            get
            {
                return displayName;
            }
        }
 
        public event PropertyChangedEventHandler 
            PropertyChanged;
 
        private void OnPropertyChanged(String info)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new 
                                PropertyChangedEventArgs(info));
        }
    }
 
    public class SColorList : 
        ObservableCollection<SColor>
    {
 
    }

Under an initialization method, you should then do the following:

        SColorList colorList = 
            (SColorList)FindResource("CheckedSource"); 

After that is done, the binding is complete and you can then add data item into the colorList and it will show up in the list box control.  And the best thing is your designer can do “magic” on the DataTemplate.

To bind or not to bind

In Programming on 24 July, 2008 at 7:00 pm

One feature of WPF is to bind a data providing class to a data receiving control, Microsoft decided to call it data-binding.  Data-binding is a strange topic for developer coming from C++, MFC background.  For them when you create a control, you will then needs to manually write code to add data to the control, there is no magic there.  In WPF, magic is created once you use data-binding, when the data providing class refresh with new data added, the control will automatically get updated to show all the new data items.  To accommodate developer who don’t understand much about data-binding, WPF with C# still provides the traditional way of adding data to a user control.  This is OK until you bring in the designer.

Let’s see an example.  For some reason WPF does not support checked list box, however customizing one from the vanilla ListBox is not that difficult.  I’ve seen developer recommending that since list box can take any control item, thus when you add item, just new a CheckBox item, add that to the ListBox and you have a checked list box.  This is correct as long as no visual changes is going to be done to the CheckBox item.  If you are going to change the square check box into something more fanciful and you want your designer to do that for you, you’re out of luck.  Since these check box items are created in the code and not on the XAML, designer can’t see it in the Microsoft Expression Blend and thus can’t change them.  Global styling you say?  What if in your UI there is another check box item and you don’t want them to look the same?

Data-binding can help you here.  Create a DataTemplate in your XAML file, bind it to the ItemTemplate of the ListBox control and bind the ItemSource to the data class which inherit from the ObservableCollection class.  Now you can customize the DataTemplate to consist of a check box and a text label and since this is created in a XAML file, designer can see it and thus can reshape to whatever she wants.

Advantages and Disadvantages of C# as compared to C++

In Programming on 11 July, 2006 at 3:00 pm

Advantages:

  1. While no programming language lets programmers write entirely bug-free programs, C# goes a long way in comparison with C and C++.
  2. With Windows Form and later on WPF (especially with the aid of Expression Interactive Designer), C# is great for Rapid Application Development (RAD).
  3. C# is safer to run.  Since C# program is compiled into an intermediate language, the OS can always check it to see that no malicious code is about.
  4. C# combines the old and the new in an almost perfect balance.  C# duplicates much of the concise syntax of C and also adds modern, object-oriented features while retaining very little unnecessary baggage from C.
  5. Cost of maintenance for C# is definitely much lower than that of C++.  This is a positive side effect of C# helping programmers to write program that is as bug free as possible.
  6. C# can make use of every feature available in WPF, making it one of the languages that can work perfectly with WPF.

Disadvantages:

  1. C# is slower to run.  This is somewhat taken care of when using WPF, although currently the launching of WPF application is still a bit slow.  However, after the program is launched, the animation effects are all very smooth.
  2. C# is less flexible than C++.  C# depends greatly on .NET framework, anything that is not found in the .NET framework will be difficult to implement.

How to use that Aero Glass API?

In Programming on 19 May, 2006 at 3:00 pm

I have a comment asking me “how do i add instal the shell api on my xp machine”.  The answer to this question is actually quite simple; Just install the latest Windows SDK onto your XP machine and you will get the header file to those Aero API.

However, you can’t test your program under Windows XP; Those API will simply fail.  Desktop Window Manager’s API will only function under Windows Vista.  So you will need the latest compatible Windows Vista CTP build to verify your program.  Have fun!

Where’s my pointer!

In Programming on 26 April, 2006 at 10:40 am

The single most baffling thing about changing to program in C# from C++ is that pointer is not recommended.  So it will take some time to get use to it.  Especially when a lot of algorithm is base on using pointer to access certain data structure.

C# still contain pointer, in fact the syntax is exactly the same as that in C++, just that it is consider unsafe to use pointer.  Your program might access some invalid address using pointer method causing access violation and thus this is consider unsafe by C#.

I hope I can really manage to do without pointer.  I really hope so.

Finally, it is up to date

In Programming on 25 April, 2006 at 9:39 am

After such a long time, I’ve finally got my act together to make my test program up to date with the WinFX Feb CTP release.  Its not that the update need as long as 2 months  plus to make, just that I was really busy with other work related project.  The actual time I took was about 1 and a half day but the amount of changes that has gotten into WinFX was quite a lot.

First there was a change in the namespace URL, this update is making the URL official.  Then the various scripting parameters in XAML has changed, for example the way to refer to a ResourceDictionary is no longer just a single line.  It has changed to become a nested set of ResourceDictionary script.

So after clearing this hurdle, I will progress on to port my C++ code into C# code.  I am already getting into some serious obstacle but I will be clearing them step by step.

Glass, more glass please

In Programming on 20 April, 2006 at 2:01 pm

Using the aero glass API, developer can bring in more glass into the application window, instead of just at the chrome (which is provided free of charge by Windows Vista).

Tim Sneath, Windows Vista Technical Evangelist, blog about how to add glass to a Windows Form application using, you guess it, C#.

The one single API that is important here is the DwmExtendFrameIntoClientArea().  From the name of the API, it seems that it is just instructing Desktop Windows Manager (DWM) to extend the chrome area into the client area.  I should think that when the theme change to Aero Diamond, then we will also get diamond free!

OK, where’s my glass?

C# – Diving into managed code

In Programming on 19 April, 2006 at 1:51 am

Though I’ve been playing around with WPF programming using C#, I haven’t really start to learn C# seriously.

This Monday (specifically 17 April 2006) I’ve started my path to serious C# programming.  Programming in the key of C# by Charles Petzold is really a good book for aspiring C# programmer like me.  Even though I have with me 10 years of experience in C/C++ programming, this book still bring to me some new perspective in programming.

I will recommend this book to anybody who is interested in programming with C#.