Showing posts with label WPF. Show all posts
Showing posts with label WPF. Show all posts

Monday, May 9, 2011

Switching to C#

neon sign - foreign language bookstoreAfter more than 7 years of VB.Net development, I have made the conscious decision to split my development into primarily VB.Net for most of my ASP.Net projects and C# for my Windows Phone 7, Silverlight, and WPF work. This transition was due to three primary driving forces:

  • Early Windows Phone 7 development (pre-RTM) was limited to C# due to the lack of availability for VB.Net support on the WP7 platform.
  • There appears to be a larger codebase of Silverlight and WP7 utilities, samples, and tutorials in C# to learn from.
  • I have had an easier time finding advanced Linq syntax examples in C#.

The syntax differences are not much different. I was already used to the most common syntactic differences, such as parenthesis, squiggly brackets, and semi-colons from my earlier work in PHP and Java. I would have started in C# if it were not for the Classic ASP and VB.Net applications I was supporting years ago in my .Net 1.1 beginnings.

Of course, the language selection is always trumped by complying with my clients’ corporate development standards and selecting the appropriate language for the job where the differences are relevant.

photo credit: avlxyz / CC BY-SA 2.0

Tuesday, June 1, 2010

IndyTechFest 2010

I am a Windows 7 phone programmer. At least that’s what Dave Bost, Bill Steele, and Jesse Liberty told me. That makes me feel pretty trendy.

With eight tracks of sessions I found myself wanting to attend two or three presentations simultaneously every hour. Unfortunately I had to choose just one.

Sessions I attended

A. Keynote with Jesse Liberty.

1. Windows Presentation Foundation for Developers

2. Introducing Windows Phone 7 Series

B. Lunch: Networking. It’s pretty fun to go to a regional event with over 400 attendees and realize that you know about 10% of the people there. I wanted to attend the Building a Company and Operating a Consultancy lunch and learn, but the room was packed full by the time I got my lunch.

3. Building Applications on Windows Phone 7 with Silverlight

4. Building a Data Mart 101

5. Application Development with Silverlight 4. Jesse mentioned Adam Kinney’s name in this session. That always makes me think I know a celebrity.

Thank you!

A big thanks goes out to the organizers, speakers, sponsors, volunteers, attendees, and anyone else that had their hand in the event! Thank you to my buddy Dave Fancher for giving me a ride from the Marriott North to the Marriot East.

The logistics, food, venue, prizes, etc. get better every time they hold the IndyTechFest. There were so many prizes from the sponsors it literally took an hour to raffle them all off – and they were even doing it quickly and efficiently.

Wednesday, November 11, 2009

Designing Applications for the Occasionally Connected Scenario – NUFW Presentation

The presentation slides from my presentation at NUFW last night are below. It’s not much value without the demo’s and audio except for the links which I used to research and debug while creating the demo application. I have the presentation audio and may turn it into a webcast at some point in the future.

Wednesday, October 28, 2009

Using Local Database Cache for Occasionally Connected Data Synchronizing

After getting everything setup using the Visual Studio template and configuration wizards, I found that the server table was not being updated during the synchronization. Using SQL Server Profiler, I could see the SELECT queries that were interrogating the server instance to see if there were changes on the server, but I saw no indication of UPDATE or INSERT statements. I ran across a helpful blog post http://videoworld-rong.blogspot.com/2009/10/adonet-sync-services.html that indicated the default behavior of the Sync process is DownloadOnly where changes to the central database are downloaded to the local cached database. A simple change to OnInitialized event in the code-behind file of the .sync file resolved that issue easily.

   1: Private Sub OnInitialized()
   2:  ' ProductionHistoryDetail is
   3:  ' the name of the cached table
   4:  Me.ProductionHistoryDetail.SyncDirection = _
   5:     Microsoft.Synchronization.Data.SyncDirection.Bidirectional
   6:  
   7: End Sub

There were two other issues to overcome:



  1. While testing what would happen if the database was taken offline between synchronization, I found that the .Net SQLClient Data Provider connection that the sync framework provider was using would block the process where the command was issued to set the database offline (ALTER DATABASE <databasename> SET OFFLINE).
  2. Once the database went offline, the application would throw an exception when it attempted to connect to the server database to synchronize with the local SQL CE 3.5 database.

Solution:



  1. ?? Still investigating.
  2. Wrap the call to syncAgent.Synchronize() in a try/catch block. Surely there are methods to determine ability to connect to the db more efficiently than catching an exception.

Tuesday, October 27, 2009

Local Database Cache – Cached Table Add Button Grayed Out

I was experimenting with adding a Local Database Cache to a WPF client app I was working on when I ran into a situation where I could not configure the Cached table. There are a number of tutorials for how to set up the local database cache so I won’t go into much about those details except a brief introduction and where my experience differed.

The local database cache uses the Microsoft Synchronization Framework to synchronize tables between a local SQL Compact Edition .sdf file with a standard SQL Server database which is a really powerful feature for those occasionally connected scenarios. Visual Studio 2008 has a project template to make the process painless. See the photo below.

local database cache VS template

The step in the process that slowed me down was the point where you set connection to the client and server databases, then select the tables you want to keep synchronized. On my screen, the “Add” button below the Cached Tables list box was grayed out as in the following illustration.

local database cache table selection screen

After considerable searching and retries, I found a that I had neglected to set the primary key in the table I wanted to synchronize. I configured it as an Identity column, but forgot the click the key button in the SSMS table designer on the “Id” field. After correcting that, the “Add” button was no longer disabled on the table selection.

local-data-cache1

Tuesday, October 20, 2009

NullReferenceException on String.IsNullOrEmpty in WPF

I really like the power of what can be done in WPF, but every time I turn around there is some trivial thing that gets in my way of being productive. This time it is a NullReferenceException being thrown when I do a String.IsNullOrEmpty check on the Label text (Content) in a button click event.

   1: If PlayButtonText = "Play" Then
   2:    SetValue(PlayButtonTextProperty, "Pause")
   3:    ' NullReferenceException thrown on following line
   4:    If String.IsNullOrEmpty(lblStartTime.Content.ToString) Then
   5:       lblStartTime.Content = DateTime.Now
   6:    End If
   7:    myTimer.Start()
   8:  
   9: Else
  10:    SetValue(PlayButtonTextProperty, "Play")
  11:    myTimer.Stop()
  12: End If

A similar bug was first reported in 2006 .Net 2.0 when used in a loop in a console app with an empty construct, and was subsequently fixed. It seems odd that I would experience a similar bug (or feature, if that is what it would be classified as) that has yet to be fixed as of .Net 3.5 SP1 in Visual Studio 2008. My usage above does not seem to be that uncommon.


Workaround


Replace the Label control with a TextBlock control, obviously replacing the lblStartTime.Content property with lblStartTime.Text property since there is no Content property on the TextBlock control.


Josh Smith wrote a good blog post explaining the differences between a WPF Label control vs. a TextBlock control.

WPF Binding app.config settings in XAML

I tried several different syntaxes for exposing an app.config MySettings property (VB.Net, not C#) for databinding in the XAML. I was met with several compile and/or designer load errors like:

  1. could not create an instance of type StaticExtension
  2. ‘MemberReferenceSerializer’ ValueSerializer cannot convert from ‘System.String’
  3. A TwoWay or OneWayToSource binding cannot work on the read-only property of ‘TargetShiftCount’ of type PacingChart.MySettings

The databinding syntax that worked was:

   1: <Window x:Class="Window1"
   2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   4:     xmlns:p="clr-namespace:PacingChart"       
   5:     Name="window1"
   6:     Title="Pacer" Height="300" Width="300">
   7:     <Grid>
   8:         <TextBox Text="{Binding Source={x:Static p:MySettings.Default}, Path=TargetShiftCount, Mode=OneWay}" Name="txtShiftTarget" Margin="126,111,32,128"   />
   9:     </Grid>
  10: </Window>

On code snippet line 3, note the clr-namespace:<project root namespace> where <project root namespace> is PacingChart in this example. It did not work in my project with clr-namespace:PacingChart.Properties as some examples suggested. Those examples also indicated that the binding source should be "{Binding Source={x:Static p:Settings.Default}, Path=TargetShiftCount}". Using p:Settings.Default, not the p:MySettings.Default which worked in my example. Also note the lack of the Mode=OneWay attribute which caused the third error message listed above.


Most of the examples I attempted to follow were from people who asked the question about the correct VB.Net syntax, but the people responding were documenting the C# syntax. The Properties.Settings (in C#) and My.Settings (in Visual Basic) are helper classes. It is important to understand the syntax differences. In the VB.Net codebehind, you would access the settings using the syntax My.Settings.TargetShiftCount where in the XAML, you would use the MySettings class.


For completeness, I created the app.config property settings by double-clicking the “My Project” entry in the Solution Explorer and switching to the “Settings” tab, which created the following snippet in the app.config file.



   1: <applicationSettings>
   2:     <PacingChart.MySettings>
   3:         <setting name="TargetShiftCount" serializeAs="String">
   4:             <value>200</value>
   5:         </setting>
   6:     </PacingChart.MySettings>
   7: </applicationSettings>

Monday, July 27, 2009

WPF controls not showing a state change

I created a dependency property in the code behind to enable/disable a button during processing.wpf code-behind

In the XAML, I bound the IsEnabled property to the dependency property. XAML button binding

The button wasn’t showing the state change. Don’t forget to add the Name property to the window or the property won’t show the state change. XAML window name

Thursday, July 16, 2009

Encoding Media with Expression Blend and Encoder 2 SDK

This week I presented for the Fort Wayne .Net Users Group (NUFW). There were about ten people in attendance and the presentation lasted about one and a half hours with an Encoder GUI walkthrough and two demos. The Power Point presentation and source code can be downloaded here. The theme of the main Demo was ZooTube a simple WPF app to encode media with an Intro and an in-video watermark (screenshot of app is below the video). The code was based on the VB sample code that is included in the Encoder 2 SDK download. The finished video is shown below.
zootube wpf app screenshot