Silverlight: Data binding from a WCF service

In Silverlight all data loading is asynchronous. Below is a simple example of loading some data from a WCF sevice.

First is the call to bindData, which is called when this control is loaded. In the code below, service is our WCF web service.


private MedfordLandService.MedfordLandInfoClient service;

        public HTEDetailRequest HTEDetails
        {
            get { return m_HTEDetails; }
            set { this.m_HTEDetails = value; }
        }

        public void bindData()
        {
            try
            {
                service = new MedfordLandService.MedfordLandInfoClient();

                service.ReturnBPInfoCompleted +=new EventHandler(service_ReturnBPInfoCompleted);
                service.ReturnBPInfoAsync(Int32.Parse(HTEDetails.LocationID), HTEDetails.ItemYear, Int32.Parse(HTEDetails.ItemKey));     

                this.txtMainDetailsHeader.Text = "Building Permit Application Details: (" + HTEDetails.ItemYear + "-" + HTEDetails.ItemKey + ")";
                this.gdMainDetails.Visibility = System.Windows.Visibility.Visible;
                this.expBPDetails.Visibility = System.Windows.Visibility.Visible;
                this.expBPDetails.IsExpanded = true;
            }
            catch (Exception ex)
            {
                // do something with that error!!!
            }
            finally
            {
                service = null;
            }
        }

Next come the events that handle the completed events:


void  service_ReturnBPInfoCompleted(object sender, ReturnBPInfoCompletedEventArgs e)
        {
 	        List infoList = (List) e.Result;
            BuildingPermitInfo info = infoList.First(a => (a.BPPYER == HTEDetails.ItemYear.ToString()) && (a.BPPCNB == HTEDetails.ItemKey));
            CSubs oSubs = new CSubs();

            if (info != null)
            {
                this.txtYear.Text = oSubs.ConvertHTEYear(Int32.Parse(info.BPPYER));
                this.txtAppNum.Text = info.BPPCNB;
                this.txtTenantNum.Text = info.BPTNNB;
                this.txtStatus.Text = info.BPASTS;
                this.txtDate.Text = oSubs.ConvertHTEDate(info.BPASDT);
                this.txtAppDate.Text = oSubs.ConvertHTEDate(info.BPADAT);
                this.txtPlanReview.Text = info.BPAPRB;
                this.txtEstVal.Text = info.BPEVAL;
                this.txtRespDept.Text = info.BPRDCD;
                this.txtAppSqFt.Text = info.BPASQF;
                this.txtPinNum.Text = info.BPAPPN;
                this.txtDesc.Text = info.BPAPDS;

                if (service == null)
                {
                    service = new MedfordLandService.MedfordLandInfoClient();
                }

                service.ReturnBPStatusCompleted += new EventHandler(service_ReturnBPStatusCompleted);
                service.ReturnBPStatusAsync(info.BPASTS);
                service = null;
            }
        }

        void service_ReturnBPStatusCompleted(object sender, ReturnBPStatusCompletedEventArgs e)
        {
            string sStatusCode = this.txtStatus.Text;
            List statusList = (List) e.Result;
            BPStatus status = statusList.First(a => a.Code == sStatusCode);

            if (status != null)
                this.txtStatus.Text = status.Definition.Trim() + System.Environment.NewLine + "(" + sStatusCode.Trim() + ")";
        }

-

-

January 25, 2012 · david · No Comments
Tags:  Â· Posted in: General Programming

Silverlight: Application.Current.Resources

Now this is a trivial little thing I always seem to manage to forget.

The scenario is this: I have a TextBlock UIElement. I want to create a mouse over, to change the color of the font when the mouse goes over the text. In Javascript this is really easy, but in Silverlight, I had trouble figuring it out.

So, the first thing is to add an application level style to your App.xaml. This style, because it is in App.xaml, will be available in the Resources collection for the entire application. It looks like this:

    <Application.Resources>
       ...
        <Style x:Name="standardLinkMouseOver" TargetType="TextBlock">
            <Setter Property="FontSize" Value="11"></Setter>
            <Setter Property="FontWeight" Value="Bold"></Setter>
            <Setter Property="Foreground" Value="#FFED6519"></Setter>
        </Style>
        <Style x:Name="standardLinkMouseLeave" TargetType="TextBlock">
            <Setter Property="FontSize" Value="11"></Setter>
            <Setter Property="FontWeight" Value="SemiBold"></Setter>
            <Setter Property="Foreground" Value="#FF023670"></Setter>
        </Style>

    ...
    </Application.Resources>

So, now the style is available to the entire application and we can get to it via code like so:


        void HTEItem_MouseEnter(object sender, MouseEventArgs e)
        {
            this.Cursor = Cursors.Hand;

            TextBlock tb = sender as TextBlock;
            tb.Style = Application.Current.Resources["standardLinkMouseOver"] as Style;
        }

        void HTEItem_MouseLeave(object sender, MouseEventArgs e)
        {
            this.Cursor = Cursors.Arrow;

            TextBlock tb = sender as TextBlock;
            tb.Style = Application.Current.Resources["standardLinkMouseLeave"] as Style;
        }

-

-

January 25, 2012 · david · No Comments
Tags:  Â· Posted in: General Programming

Silverlight: Move a UIElement

In my current application, I need to move UIElements in and out. I like to do things from the code side, as I am not as comfortable with XAML.

The function below takes in 5 function parameters
1). UIElement element — the UIElement to move (ie: a grid or panel)
2). double dblFrom — the start value of the property
3). double dblTo — the end value of the property
4). double dblDuration — how long should it take?
5). PropertyPath path — the property that you want to animate (ie: Width or Height)

In the code below, I am closing the search results pane. The expResults is an expander pane. When the results are shown, its width is 390px. After close, it will be 21px wide (to allow for my expander image). The duration of the close event will be .5 seconds.


        private void closeResultsPane()
        {
            CUISubs oSubs = new CUISubs();
            setRightPanelExapandVisibility();
            oSubs.moveElement(expResults, 390, 21, .5, new PropertyPath("(Width)"));

            expResults.IsExpanded = false;
        }

This is the implementation of the moveElement:


        public void moveElement(UIElement element, double dblFrom, double dblTo, double dblDuration, PropertyPath prop)
        {
            Duration duration = new Duration(TimeSpan.FromSeconds(dblDuration));

            DoubleAnimation doubleAnim = new DoubleAnimation();
            doubleAnim.Duration = duration;

            Storyboard sb = new Storyboard();
            sb.Duration = duration;
            sb.Children.Add(doubleAnim);

            Storyboard.SetTarget(doubleAnim, element);
            Storyboard.SetTargetProperty(doubleAnim, prop);

            doubleAnim.From = dblFrom;
            doubleAnim.To = dblTo;

            sb.Begin();
        }

This function works with any UIElement and any property that can altered, like Opacity, Height, Width, etc.

-

-

January 25, 2012 · david · No Comments
Tags:  Â· Posted in: General Programming

Silverlight: Open URL from a UIElement

In Silverlight, it seems like there is a click event for everything. But it is not always clear as to how to open link into a browser window.

I keep an OpenLink function in my subs class. It does that for me.

This is how it is called:


        private void txtCountyDetails_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            string sUrl = c_COUNTY_ACCOUNT_DETAILS + this.AccountNum;
            CSubs oSubs = new CSubs();
            oSubs.openLink(sUrl, 800, 800, null);
        }

And this is the actual function:


public void openLink(string url, int iWidth, int iHeight, HtmlPopupWindowOptions opts)
        {
            HtmlPopupWindowOptions options;
            if (opts == null)
            {
                options = new HtmlPopupWindowOptions();
                options.Left = 0;
                options.Top = 0;
                options.Width = iWidth;
                options.Height = iHeight;
                options.Scrollbars = true;
                options.Location = false;
                options.Directories = false;
                options.Menubar = false;
                options.Resizeable = false;
                options.Status = false;
                options.Toolbar = false;
            }
            else
            {
                options = opts;
            }

            HtmlPage.PopupWindow(new Uri(url), "new", options);
        }

-

-

January 25, 2012 · david · No Comments
Tags:  Â· Posted in: General Programming

Silverlight: ServiceReferences.ClientConfig as App.Config

I have been storing application level values in my “ServiceReferences.ClientConfig” file for my Silverlight application. I have been using it like an app.config or a web.config, since there is not one in my Silverlight application.

Below is just a simple function to get values from the “ServiceReferences.ClientConfig” file


       public string getAppSetting(string strKey)
        {
            string strValue = string.Empty;

            System.XML.LINQ.XDocument doc = new XDocument();
            doc = System.XML.LINQ.XDocument.Load("ServiceReferences.ClientConfig");

            foreach (var node in doc.Descendants("add"))
            {
                if (node.FirstAttribute.Value.ToString() == strKey)
                {
                    strValue = node.Attribute("value").ToString().Replace("\"","").Replace("value=","");
                    break;
                }
            }

            return strValue;
        }

-

-

January 25, 2012 · david · No Comments
Tags:  Â· Posted in: ArcGIS Server, General Programming


site tracking with Asynchronous Google Analytics plugin for Multisite by WordPress Expert at Web Design Jakarta.