Archive for the 'WPF' category
WPF ControlTemplate Trigger tip
January 5, 2008 2:11 amI didn’t fully realize how ControlTemplate Triggers work (or don’t) until I solved a problem with a UserControl today, with the help of this post on the MSDN forums.
A Triggers’ EnterActions or ExitActions don’t seem to get fired when a UserControl is constructed. If you want, say, an animation in your control to start on a property change, you can put a BeginStoryboard element into a Trigger’s EnterActions and a reverse BeginStoryboard into the ExitActions.
But for the control to start out right at initialization you also need to have the right Setter element on the Trigger to set initial states (in addition to setting the opposite initial states on the elements to be animated/initialized.)
Here’s a tiny example:
1 <UserControl> <!-- Namespaces omitted for brevity --> 2 <Grid> 3 <ToggleButton x:Name="Toggle"> 4 <ToggleButton.Template> 5 <ControlTemplate TargetType="{x:Type ToggleButton}"> 6 <Grid> 7 <!-- Initial opacity is 1 for the initially unchecked ToggleButton state. A trigger changes it to 0 for an initial checked state. --> 8 <Grid x:Name="HideMe" Opacity="1"> 9 <TextBlock Text="Hidden"/> 10 </Grid> 11 <!-- Other parts of the control go here --> 12 </Grid> 13 <ControlTemplate.Triggers> 14 <Trigger Property="ToggleButton.IsChecked" Value="True"> 15 <!-- This setter hides the desired element when the ToggleButton's initial state is checked --> 16 <Setter TargetName="HideMe" Property="Opacity" Value="0"/> 17 <Trigger.EnterActions> 18 <BeginStoryboard> 19 <Storyboard> 20 <DoubleAnimation BeginTime="00:00:00" Duration="00:00:00.05" Storyboard.TargetName="HideMe" Storyboard.TargetProperty="(UIElement.Opacity)" To="0" /> 21 </Storyboard> 22 </BeginStoryboard> 23 </Trigger.EnterActions> 24 <!-- ExitActions go here to make the Grid with the TextBlock visible again --> 25 </Trigger> 26 </ControlTemplate.Triggers> 27 </ControlTemplate> 28 </ToggleButton.Template> 29 </ToggleButton> 30 </Grid> 31 </UserControl>
The ToggleButton contains a TextBlock in line 9 that I want hidden when the ToggleButton’s IsChecked state is True. The animation in line 18 takes care of doing that when the user interacts with the control, but when the control is initialized, I need the Setter in line 16 to hide the TextBlock initially if the ToggleButton is checked by default (through data binding, for example.)
Categories: Software development, WPF
No Comments »
Introducing CardSharkV
November 25, 2007 1:06 amCheck out this program I’ve developed, called CardSharkV. It’s a digital picture storage card/camera “downloader” with a twist.
It’s a bit of an experiment, and I’m looking for some real-world “beta” testers. My plan is to try my hand at charging for the program. But to see if word will spread, I’m planning to give out 50 or so licenses without charge while I work out any kinks that might be in the program.
If you’re interested in trying it out, download the program, install and run it, and then click the “Request License” button.
Read more about it on the CardSharkV page.
Update (2007-11-25): CardSharkV depends on the .NET Framework 3.0. It’s thus meant to be installed on Vista systems only. If you have XP and the .NET Framework 3.0, it should still work. Also, since I posted the whole thing Saturday night after a whole day of re-shaping my first, never published version, I didn’t test the install too well. I’ve since had a little time to test and found some issues, especially with the license mechanism. I’ve posted version 1.0.2.0 to correct those issues and to improve the installer as well.
Categories: Software, Software development, Vista, WPF
2 Comments »
Microsoft deprecates support for XBAP WPF apps in version 5.3 of the Media Center SDK
November 19, 2007 10:02 pmI downloaded and installed the latest version of the Media Center SDK just now, and was a bit surprised to see that WPF apps are now officially deprecated as a supported way of extending Windows Media Center on Vista. There’s no big announcement around this so far, it’s almost just a footnote in the “What’s New” section:
Deprecated features
The following features have been deprecated:
- Hosting for Microsoft .NET Framework 3.0 Extensible Application Markup Language (XAML) browser applications (XBAPs).
- The Triple-tap/Soft-keyboard ActiveX control for hosted HTML applications.
- Support for using alternative shells to run and host HTML applications.
The reason I’m surprised is that Microsoft announced the support for WPF based extensibility apps at PDC05 with some fanfare.
I guess interest among developers just hasn’t been there for WPF-based Media Center apps, and Microsoft needs to make judgment calls on where to invest in their extensibility platform. The rest of the deprecated features seem to have been made for the same reason: limited interest among developers for these features. In addition I think there might be issues of an architectural nature that just make the three things that got dropped too expensive to maintain or improve.
I’m sad about this development, mostly because I still don’t see a great set of development and design tools around for MCML. WPF seems to me to have a whole lot more momentum than MCML (WPF has the Expression suite of tools and has been billed as the “GDI for the next 20 years”), but who knows what may be coming for Windows 7, or even an earlier interim update for Media Center?
Categories: Microsoft, Opinion, Software development, Vista, WPF
8 Comments »
Debugging XBAP applications on Vista using Visual Studio 2005
January 23, 2007 5:35 pmI ran into an interesting little quirk with Visual Studio 2005 on Vista yesterday. I was trying to start an XBAP debugging session from VS (using the F5 key), and none of my breakpoints got triggered. Upon exiting the IE instance that hosts PresentationHost.exe, VS considered the session as still “running”.
I had set my system up according to Tim Sneath’s post on the perfect WPF developer system, including VS 2005 SP1 and the beta version of the Vista extensions for VS 2005. I was running VS “as administrator”.
As it turns out there are some technical details around starting XBAP debugging sessions from an elevated VS process that cause this behavior if you already have another instance of IE running when starting the debugging session.
Thanks to Tim Sneath and Chango Valtchev at Microsoft for helping in tracking this down!
Moral of the story: If you want to debug an XBAP from VS 2005 under Vista, run VS non-elevated, or make sure you have no other instances of IE running when you start the debugging session.
Categories: Software development, Vista, WPF
4 Comments »
Localizing an XBAP application without using LocBaml
December 12, 2006 4:23 pmUsing the LocBaml sample application that Microsoft provides with the Windows SDK for Windows Presentation Foundation can be very frustrating. By default it seems to pick up a lot of things that clutter the resulting CSV file. If your application contains a lot of image resources, those will get duplicated in the localized resource dlls as well, increasing the space that your application occupies on the hard drive. And finally, getting an XBAP application to be deployable after localization with LocBaml requires opening up the application manifest and adding all the localized resource files to it. Otherwise a deployment error will bite you.
So with all those problems, we decided to take a different approach for our WPF XBAP applications. We put all our strings into a standalone XAML file, producing a ResourceDictionary. This way our application can pick up strings using Text=”{StaticResource strXYZ}” for XAML markup and/or use Application.Current.FindResource(“strXYZ”) for codebehind. We name the file “Strings_en-US.xaml”. We can localize this file nicely, and name the resulting set appropriately: “Strings_de-DE.xaml”, “Strings_fr-FR.xaml” and so on. These files can then be included as resources in the application. They can also be added as “loose” XAML files, so we can add languages or make corrections after compilation.
The string resource file looks something like this:
<?xml version="1.0" encoding="utf-8" ?>
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
>
<sys:String x:Key="strButtonOK">OK</sys>
<sys:String x:Key="strButtonCancel">Cancel</sys>
</ResourceDictionary>
At application load time, we look at the CurrentCulture and attempt to load the ResourceDictionary object like this:
ResourceDictionary rd = null;
System.Globalization.CultureInfo ci =
System.Globalization.CultureInfo.CurrentCulture;
string langCountry = ci.Name;
string languageFile =
string.Format("/Resources/Strings/Strings_{0}.xaml", langCountry);
try
{
rd = (ResourceDictionary)Application.LoadComponent(
new Uri(languageFile, UriKind.RelativeOrAbsolute)
);
}
You can imagine this with some catch {} fallbacks for when a specific language-country combination doesn’t exist. We’ll fall back to a file that’s only language specific, and if that doesn’t work out, we’ll fall back to en-US.
We’re not using CurrentUICulture, since that seems to be tied to the UI language of Vista, and we want our apps to follow what the user sets in Control Panel. CurrentCulture seems to work for that.
To load the strings into the global resource dictionary, we just do
Application.Current.Resources.MergedDictionaries.Add(rd);
To load loose XAML files that are not part of the application’s resources, we do something like this:
ResourceDictionary rd1 = null;
string languageFileLoose = string.Format(
"pack://siteoforigin:,,,/Resources/Strings/Strings_{0}.xaml",
langCountry
);
Uri uri = new Uri(languageFileLoose, UriKind.Absolute);
System.Windows.Resources.StreamResourceInfo info;
System.Windows.Markup.XamlReader reader;
try {
info = Application.GetRemoteStream(uri);
reader = new System.Windows.Markup.XamlReader();
rd1 = (ResourceDictionary)reader.LoadAsync(info.Stream);
}
Again, we extend this with catch {} blocks to fall back to a file that’s only language specific. We do the loose XAML loading after we’ve loaded embedded resources, so we can rely on a basic set of languages and strings, but can add languages, fix string translation errors or make improvements in the loose XAML after compiling things.
It may work for you as well, but ymmv.
Update: Changed the first Uri to UriKind.RelativeOrAbsolute
Categories: Software development, WPF
8 Comments »
Breaking all rules of software release management
November 13, 2006 11:01 pmThe project I’m currently working on is getting really close to being done, with Windows Vista having been “RTM”ed recently. We have a few more days to get last minute bugs ironed out and then it’s “off to the factories”… I can’t say much more about the project right now, but it’s a pretty significant and highly visible piece of software coming out somehwere around February 2007. I hope it does well. But I digress…
At this late a stage in a product’s delivery cycle it is common to make very few changes to the code in order not to create more bugs than one fixes. Ideally each change should be peer-reviewed and thouroughly tested before getting checked in (like with Windows Vista and the contortions people have to go through in the “shiproom” to get a bugfix approved). Too bad for me that I’m the only “peer” on the project (i.e. I’m the only developer). There’s nobody I can go to for a review.
And in the last few weeks I’ve broken the “as few changes as possible” rule a number of times. Why? Because I’m confident in the features that WPF provides. And because I do tend to run extensive tests before I check stuff in.
So what kinds of changes have I put in at the “last minute”? I’ve shuffled buttons around on the page, I’ve added code for drawing the user’s eyes to input fields containing invalid data, I’ve added data-bound and data-driven UI elements.
WPF has given me the confidence to be able to do this at the last minute. The fact that moving buttons around is a simple XAML markup change, for example, is just great for last minute UI adjustments.
But I’m not exactly proud of doing it. Oh well. So far everything seems to have worked out.
Categories: WPF
No Comments »
WPF XBAP apps and UAC
November 4, 2006 12:40 pmHere’s an interesting thing I learned the other day, which wasn’t obvious to me.
If you have an XBAP WPF application that tries to access files at the location identified by CSIDL_COMMON_APPDATA (Environment.SpecialFolders.CommonApplicationData in .Net 2.0/3.0) they need to be marked with Read/Write permissions for ”Everyone”. Otherwise File Virtualization kicks in and Vista creates copies of the files in the VirtualStore.
This shouldn’t be a problem per se, but if you have data integrity issues with your application reading/writing files it’s nice to eliminate the virtualization for troubleshooting.
Also, if your application needs to share the same data among users you don’t want virtualization, since that is a per-user thing, and you’d end up with one set of data files for each user.
If you’re installing some default data files for the application using Windows Installer (which will run elevated), you need to make sure you change the permissions on the data files so “Everyone” gets read/write permissions. This can be done using the LockPermissions table of Windows Installer.
Categories: WPF
No Comments »
How did I end up working with Windows Presentation Foundation?
October 9, 2006 7:20 amIn one of my past posts, I talked a little about my impressions of Vista as a platform for software developers. Here’s the story of how I got involved with WPF.
My last two projects before starting on WPF were built around hosting IE in “something”.
The first “something” was an exe hosting IE and adding a bunch of window.external functionality. I took the lead in moving a somewhat old, pretty static UI infrastructure for presenting internet-updatable content to the XML/CSS/XSLT age. That involved a lot of using dynamic expressions to enable reflowing of the UI for different monitor sizes (the app was a fullscreen app). Towards the end of that project I started hearing about an XML-based application development thing coming from Microsoft, and I remember thinking “How cool! No more need for the mess of HTML / CSS / XSLT and JScript/dynamic expressions to glue it together.”
The second “something” was an application for Windows Media Center, which at the time only offered hosted IE as its application development model (with a bunch of window.external functionality thrown in to get at the Media Center stuff, similar to how our exe had worked). Three of us wrote a pretty interesting little application that shipped for about a year and then was discontinued when circumstances changed on us drastically. Around the time this happened, the XML-based app development thing from Microsoft, better known then as “Avalon”, took more shape.
I was lucky enough to get to experience the energy that Microsoft projected at PDC05. It felt like an explosion of enthusiasm around the newly re-christened “Windows Presentation Foundation”. My eyes were popping at Greg Schechter’s talk. Manuel Clement’s talk was chock-full infectious excitement. I wanted to work with this new stuff.
Luckily the project(s) we were given to work on next required a level of “polish” that I doubted we could achieve using HTML, WinForms or any other technology we were proficient in. I advocated strongly for using WPF, and for some reason management went along. One of the requirements for one of the projects was that it had to run under Vista and more specifically under Media Center to get a great user experience. The level of “polish” that was required pretty much ruled out using HTML. We could have done all ActiveX controls, I suppose, or an HTML/Flash hybrid, but none of us had the required expertise to implement that, and the time frame was extremely tight to begin with.
To me the only way to solve the problem was using the incredible leverage of WPF to “get things done”, even with the steep, steep initial learning curve. I worked on some throw-away applications just to show my manager that this new platform had incredible potential and that I would be able to pick it up quickly enough to work with it seriously. He liked what he saw. The rest is material for another post. Let me just say now that the devil is in the details, and we had lots of challenges on the project overall. Once it’s out there I can talk more about it, but not yet.
Categories: WPF
No Comments »
The WPF developer’s toolbelt
October 5, 2006 10:32 pmThis is a collection of tools that should be in every WPF developer’s toolbelt, if you ask me:
XAMLPad (from the Windows SDK [Full ISO|Express Web Download])
Snoop (from Pete Blois)
Perforator (from the Windows SDK) (Don’t forget to turn on the required debugging setting in the registry - mentioned in the article)
CLR Profiler
.Net Reflector (from Lutz Roeder)
If you’re a designer / developer you also want:
Microsoft Expression: Interactive Designer
MOBIFORM Aurora XAML Designer
Electric Rain Zam3D
And of course the staples:
.Net Framework 3.0 September 2006 CTP (I usually pick the complete installer to avoid download problems – it’s hidden a few paragraphs down under a link that says “for x86″)
Visual Studio 2005 / Visual Studio 2005 Express
“Cider” / “Orcas Preview for WPF”
Did I miss anything?
Categories: WPF
No Comments »



