WPF WTF #2: Opacity

29 August 2009 Tags  , ,

3866195696_feccd17706

On the left is WPF, on the right is WinForms, both have this.Opacity = 0.5; as the only code behind the button.

The WinForms example, as you can see, is semi-transparent and would shown whatever is behind it (if there was something behind it)

The WPF example, however, just "dulls" the contents of the Window rather than making the whole thing semi-transparent. The way to do it is to set AllowTransparency="true". However, that means WindowStyle must be set to None, which means you lose the chrome (the ‘glass’ around it), the minimise, maximise and close button, so you have to create and control those, as well as manually resizing the Window and manually controlling moving the Window.

I tried P/Invoking the Win32 libraries to set opacity and they work perfectly… on WinForms. They fail miserably in WPF.

I’m happy to be corrected if anybody knows a solution.

Opacity earns this weeks WPFWTF award and leaves us scratching our heads with the slogan, WinForms: Back To The Future.


Comments Off
 

WPF Controls incompatibilities? WTF

27 July 2009 Tags  ,

While reviving MahTweets I ran into severe performance problems, which were incredibly difficult to track down as none of the profiling tools I tried could really pinpoint the issues. The two issues were, while there were tweets being displayed

  1. Typing into a textbox with TextWrapping enabled could cause up to 25% CPU usage on my quad core desktop machine (or ~60% CPU usage on my laptop) and,
  2. When the webcam was enabled (so I’d presume the InteropBitmap that was being used to display it), everything would still function except the UI would ‘lock up’ and not respond to mouse/keyboard controls, and consume similar levels of CPU cycles.

It turns out both performance issues were related and the key was the datatemplate I was using in the ItemsControl (or ListBox or ListView) to display the tweets. Inside the datatemplate, I used two custom controls which both made us of the Hyperlink inline element to act as.. well.. links to either other functions to launching browsers at particular urls (eg, going to the home page of whatever twitter client people were using).

If I turned off those two custom controls, performance was awesome (amazingly awesome with virtualisingstackpanel enabled). If I turned on those controls but commented out hyperlinks, it was just as awesome.

I found (actually, @shiftkey found it for me) an example of making the WinForms style LinkLabel by extending Label. While Labels can be inserted into inline containers (such as a RichTextBox, which is what one of my custom controls was based on) they’re not selectable. While you can select over them, it doesn’t select the content inside… what I mean is if you have "fee fi fo fum", with fi fo being the contents of a LinkLabel,

public class InlineLink : Underline 

{ 

    public Uri Url 

    { 

        get { return (Uri)GetValue(UrlProperty); } 

        set { SetValue(UrlProperty, value); } 

    } 

 

    public String Text 

    { 

        get { return (String)GetValue(TextProperty); } 

        set { SetValue(TextProperty, value); } 

    } 

 

    public static readonly DependencyProperty UrlProperty = 

        DependencyProperty.Register("Url", typeof(Uri), typeof(InlineLink), 

        new FrameworkPropertyMetadata(null, new PropertyChangedCallback(OnUrlChanged))); 

 

    public static readonly DependencyProperty TextProperty = 

        DependencyProperty.Register("Text", typeof(String), typeof(InlineLink), 

        new FrameworkPropertyMetadata(string.Empty, new PropertyChangedCallback(OnUrlChanged))); 

 

    private static void OnUrlChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args) 

    { 

        InlineLink il = (InlineLink)obj; 

        il.Inlines.Clear(); 

 

        Run r = new Run(il.Text); 

        il.Inlines.Add(r); 

        il.Cursor = Cursors.Hand; 

    } 

} 

link.Click can be simulated by just using MouseLeftButtonDown (events didn’t seem to fire for ButtonUp’s)

inlinelink.MouseLeftButtonDown += new System.Windows.Input.MouseButtonEventHandler(conversationLink_Click);

Yes, I know that code is far from graceful, but it works well enough for a first stab at it. The funny thing is this extends Underline, which Underline extends Span – Hyperlink on the other hand.. well, this is the class signature public class Hyperlink : Span, ICommandSource, IUriContext

I wonder if it was something in ICommandSource, IUriContext, or within Hyperlink that was causing the problem?

My advice? Don’t use the Hyperlink control. Either roll your own "LinkLabel", or if you need an inline element that does linking use something like what I’ve got above.


Comments Off
 

MahTweets: It’s back

26 July 2009 Tags  , , ,

MahTweets is my twitter client of choice. Why? Well, I wrote it. Okay, that isn’t the only reason, it has some pretty awesome features distinguishing it from many Twitter clients.

Features

  • Inline media viewing
    View twitpic, flickr, direct images, and youtube (working on it, an IE8 update broke it) all inside MahTweets. Support will be expanded to support yfrog and any other services people request.
  • Webcam support
    Use your webcam to directly upload to twitpic (flickr, yfrog, and others coming later) or as your avatar on Twitter
  • Conversation viewing
  • Tracking
    This was common in the "old days" of Twitter, but now it seems to be called saved/inline searches?  Either way, if you’re particular interested in tweets on a particular subject, track keywords using twitter search.
  • Filtering
    No more "#followfriday" clogging up my Twitter stream
  • Scalable UI
    Because MahTweets is coded in WPF, I can scale everything (not just change font sizes) neatly at a click of a button.
  • Short URL expansion
    Tired of being Rick Rolled through tinyurl, is.gd, bit.ly (and many others)?
    By auto-expanding the shortened url’s (using Long URL Please), you won’t be launched into sites you know are bad
  • Multi-account support & grouping Coming soon!

Tech Goodness (Requirements and other details)

Through Windows 7’s "XP Mode" I’ve tested Windows XP support, and even in the virtualised environment it was smooth – so smooth that it took me a good 10 minutes or so to notice I’d launched it in XP Mode instead of "natively"!1

Below is a screenshot of an earlier build, running XP Mode and Windows 7 "natively"1 the two running side by side.

mahtweets_crossplatform It’s so weird to see the Luna theme in Windows 7

Big thanks to WillHughes, Shiftkey, CADBloke, Chickz0r, and digihal for help, support, code and/or encouragement on the MahTweets project

1 – by "natively" I mean running in a non-virtualised environment.


1 Comment
 

WPF Community Workshop, Melbourne

18 March 2009 Tags  , , ,

Windows Presentation Foundation Community Workshop

Ok, so you’ve seen Windows Presentation Foundation (WPF) and you thought the technology looked interesting. You know your applications are starting to look dated, you understand WPF has matured, now in its 3rd release; but life gets in the way and you haven’t had the chance to get down and dirty with it yet. So here’s your opportunity to skill up, and raise money for the Red Cross Bushfire Appeal, in this One-Day Workshop!!

To help you get to grips with this great technology quickly, Microsoft has created a series of Hands on Labs and Presentations. These will grow your skill set, putting you on the path to building the rich user interfaces your customers are demanding.

This training event comes to you courtesy of the .NET User Groups across Australia, and Cliftons, who are generously providing their training facilities at minimal cost. This is your chance to learn new skills, network with other professionals, and have a bit of fun along the way.

Registration and Payment

There is a nominal charge of $100. With the help of Cliftons we are keeping costs minimal and if we fill all workshops across the country then this event will raise close to $23,000 for the Red Cross "Victorian Bushfire Appeal 2009".

Places are limited and to register interest please send a mail to mahesh.krishnan@readify.net

Workshop Format

To make it easy for you to attend we are running the workshops on a Saturday in all major cities. The sessions will commence at 8.30am with registration. Check the schedule below for the date and location in Melbourne.

Following a 10 to 15 minute introduction to each topic, you will kick start Visual Studio 2008 and Expression Blend and work on the relevant lab!! Regardless of your current level of experience, you can work at your own pace; a facilitator will be on hand to guide your learning; and you’ll be able to take the lab content home for further learning.

Content

The WPF Skills day will cover

  1. Creating layouts, compositions and templates
  2. Building custom controls
  3. Working with Styles and control templates (includes using Expression Blend to restyle)
  4. Using the Ribbon control to effortlessly create applications that are as familiar to your customers as Office 2007 (not to mention the Windows 7 Core Applications)
  5. Working with the new DataGrid control to display tabular and editable data
  6. Binding data with ease to your user interface
  7. And more…
Event Dates

The Windows Presentation Foundation Community workshop in Melbourne is to be held on Saturday, March 28th, 2009 at Cliftons on Collins Street.

Be in to Win

The workshop will also feature a prize draw to win a copy of Visual Studio 2008. Nice!!

So get clicking and sign up by sending a mail to mahesh.krishnan@readify.net and tell your mates about the day, learn something new and help out the Bushfire victims!!

via Dave Glover


Comments Off
 

WPF Image creating file lock work around

2 December 2008 Tags  , ,

While working on a new version of TVScout, I’ve been working on adding a management tool which will allow you to choose and fetch from the variety of posters on thetvdb or themoviedb, not just the first poster found. The problem is, I’m displaying the current poster in an (System.Windows.Controls.) Image first. This creates a situation where my program wants to overwrite a resource that’s already in use.The simple fix would be set the Image.Source to null first? Partially right, but the problem is with how you set the file in the first place as there there is still a file lock.

The solution I found was a weird one to say the least. You have to make sure caching is enabled so that WPF will load the image into memory, and release the file lock and then tell it to always ignore the cache. Yes, you read that right.

You have to use the cache and then ignore the cache.

If you don’t ignore the cache, it won’t refresh the image to the new image until the next launch of the application.

private void SetImage(String filename)
{
    BitmapImage bi = new BitmapImage();
    bi.BeginInit();
    bi.CacheOption = BitmapCacheOption.OnLoad;
    bi.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
    bi.UriSource = new Uri(filename);
    bi.EndInit();
    image.Source = bi;
}

So you’ll end up with something like this in your code

SetImage("YourImage.jpg");GetNewImage();Image.Source = null;SaveNewImage("YourImage.jpg");SetImage("YourImage.jpg");

2 Comments
 

WPF DataGrid CTP Available!

13 August 2008 Tags  , ,

I picked this one up via Rob Relyea (of the WPF Team), the long awaited and much requested DataGrid for WPF is available on CodePlex. For more information (including a screenshot), Vincent Sibal has a detailed ‘how to use it’ post

Requires .NET 3.5 SP1 to run, and it’ll eventually will make its way into the WPF core libraries (I think?). Out of band releases are nice.


2 Comments
 

Screencast: WPF+SL2 Silverlight Clients

2 August 2008 Tags  , , , , ,

My Demo’s Happen Here entry (entries closed last month) was on Visual Studio 2008, how it can rock your socks by creating Twitter clients in WPF and Silverlight.

Or you can stream the original video in WMV from Silverlight.Live.Com. You can download the solution (73kB) containing the three projects, which requires the Silverlight 2 Beta 2 SDK.

For interests sake, the screencast was recorded using Microsoft’s Community Clips Recorder and edited with Windows Movie Maker – both free (well, providing you have Windows). Community Clips Recorder is fairly basic when you compare it against Camtasia, as it has no editing, zooming, or highlighting capabilities. However depending on the situation Camtasia is overly complex and the price difference is something to be considered.


1 Comment
 

Remix Melbourne 2008

25 May 2008 Tags  , , , ,

remix_logo1 With Remix over, I thought I’d sum up my thoughts on the event. Last year’s Remix was my first Microsoft event, but now I have a few more under my belt. This year I hung around Stephen Price, whose Quokka cartoons were featured all over the Remix blog. Stephen’s a very cool bloke, even if he gets lost too easily.

 

Keynote

While Mark Pesce’s keynote speech was fantastic, I’m not sure how much relevance there was to most of Remix. The content of Remix’s sessions were always going to be about about XAML (Silverlight/WPF), IE8/ASP.NET, and Expression Studio – that is technically focused, rather than the social implications. The Live Platform session (the third session) certainly did expand on "hyper-connectivity"  (social) and the technological side of things, but the rest of the Remix "conversation" was perhaps a bit too focused on the technical or product side of things. That aside, I will repeat, it was a fantastic presentation. Get yourself on Twitter now!

If you weren’t at Remix, watch the video above (text version)

Session 1 – What’s New in Windows Presentation Foundation 3.5 and beyond

Speaker: Joseph Cooney

I quite like WPF, but I haven’t really seen the need to move to .NET 3.5….until Joseph’s presentation.

.NET 3.5 cool things are:

  • New (I think?) Addins space is in a secure isolate (separate app domains), and are able to have different security levels, such as (AddInSecurtyLevel.)Internet or FullTrust.
    For security purposes, Addins do not see a "parent" GUI object – they cannot "walk up the VisualTree".
  • Under .NET 3.5 SP1, ClickOnce and XBAPs supported in Firefox
  • Now possible to "brand"/customise the setup program (generated by the VisualStudio deploy wizard)

IMG_2870

The WPF cool things are:

  • Interactive 2D on 3D is now "native", rather than a third party/unsupported library
  • WPF can make use of some DirectX stuff natively, rather than having to Interop/P/Invoke
  • Better debugging for WPF Databinding (this stuff is gold – will be making use of it for MahTweets!)
  • Formatting in DataTemplates/DataBinding (ie, <TextBlock Text="{Binding textField, StringFormat= – \{1\}}" /> will prepend " – " to the string) Nothing "wow", but so much more logical to do that on the presentation side of things, rather than needing to modify the business objects so that you can present the data correctly.
  • Recycling Virtualisation. Emphasis because this is particularly cool. In .NET 3.0, UI controls such as a ListBox would be virtualised, generating the ListBoxItems for the items that are visible at the time (+5 items above or below). When the ListBox is scrolled, and a new bunch of ListBoxItems is visible, the old ones are destroyed.
    Recycling Virtualisation in .NET 3.5 doesn’t destroy the "old’ ListBoxItems, but reuses them. This means memory usage while scrolling stays about the same and doesn’t continuously grow the more you scroll!
  • Out of band releases for new controls, much better than having to wait for .NET vNext
  • ShaderEffects sort of replace BitmapEffects (both still exist, but no reason to use BitmapEffects now) implemented in hardware so performance is much much better, and scales properly. You can create your own ShaderEffects using HSHL/PS

Ugly things:

  • .NET 3.5 is 200MB in size, compared to 50B for 3.0, and ~20meg for v1/1.1/2.0. 3.5 does include both x64 and x86 binaries, which partially explains the size. In VS2008/.NET3.5 SP1, there will be a ".NET Client Only Framework" (compile option in VS2008 SP1) that is aiming for ~25MB download, but wont include all the .NET libraries (such as System.Web), but only the ones that are most commonly used in client applications.

Joseph’s slides and demos are up on his blog already!

Session 2 – Introducing Microsoft Expression Studio 2

Speaker: Tim Aidlin

This was a fairly run-of-the-mill "I have a new application, let me show you it" presentation, covering Expression Studio 2 (except Expression Encoder 2) as well as Deep Zoom Composer. Unfortunately, for any attendees of Remix 07 or Mix On Campus, this sort of stuff (albeit for xStudio1) was pretty much what the events were all about last time, and it felt like the audience knew a bit more (about their favourite specific application) than Tim did.

While the list of new features to xMedia2 are neat (RAW image handling, batch renaming, metadata browsing, voice annotations, gallery generation), I still don’t really know what its purpose is in the Expression Studio suite. If it was a free app I could probably find a use for it, but for photo/image management Live Gallery is "good enough", and I manage all my music in Media Player…maybe its great for video management?

Contrasting with statements from Lee Brimelow from last year (that "everything you can do in Design, you can do in Blend, so I don’t see the point of xDesign"), Tim showed off xDesign2 and some of the reasons why you’d use it over xBlend. Yes, you can probably do everything in xBlend, just like everything you can do in Photoshop can be done in Paint. Being a developer, I think I’ll still be sticking to Blend, but I could see how the more artistic parts of XAML would be easier in xDesign.

Despite the improvements to xWeb2, as a developer and somebody who has been generated CSS/(X)HTML for years, I will not get any value out of xWeb2. VS2008 does all the stuff I need to do, or Notepad++ steps in when I need to go kung fu on my CSS. PHP IntelliSense/support has made it in, but this should have been a feature in xWeb2.

Session 3 – Windows Live Platform: Take the best of Windows Live and make it yours

Speakers: Angus Logan, Bronwen Zande, John O’Brien

I didn’t really know what to expect from this session, the Live Platform session sounded like it would be pretty boring, but I wasn’t overly interested in the other session which was upstairs, so the Live Platform session it was! I was pleasantly surprised, as this was a very cool session, possibly my favourite for the day! My laziness paid off!

The key things were how you can use Microsoft’s Live Platform to create incredibly interactive websites by making use of the Live services such as Virtual Earth, Live Messenger (/Hotmail) contacts/presence, Spaces, Storage (FolderShare/SkyDrive), Notifications (via email, SMS for North America, or via WLM through the alerts service).

For a few projects I have in mind, the Live ID login system looks appealing, although I’m wondering if a service like OpenID is more ‘acceptable’ (by end users, since Microsoft is so evil and all, apparently). I’d be very interested in the Live Platform Team’s view on OpenID vs LiveID, or if they can coexist.

IMG_2937

Angus left Twhirl running while giving his presentation, so I managed to get a few tweets popping up on the screen!

Session 4 – Building an Immersive, Integrated Media Experience in Silverlight

This session showed off the new ABC Silverlight Store, which while cool, is all Silverlight v1 stuff. It just seemed to lack the "wow", going over very similar things that were covered at Remix 07, without the edge the original presentations on Silverlight v1 had because it wasn’t new. I walked out (I needed a break/fresh air, not because I was bored) before it finished, so the last 15 minutes may have been awesome, its hard to tell.

The Silverlight Store also had a matching desktop client…written in Silverlight? I think (as a demonstration of the power of Silverlight and WPF), it would have been mucho cooler to do that in WPF. The technical reasons for not doing so are more than understandable – WPF weighs in at 20meg, and Silverlight at about 4meg. Both clients being Silverlight means just one framework download/install, which is much more friendly for the target audience.

The presentation was done using DeepZoom, zooming into each slide or diagram to show more detail, such as exploding a file overview into the actual code behind that file. That bit was cool.

Session 5 – Skipped

I skipped session five, not because of the content available, but because I ran into Long Zheng, and we got to chatting. Long has a new Zune ("Long Zhune"). He’s a cool guy, with or without the Zune!

Session 6 – Using Microsoft Silverlight for Creating Rich Mobile User Experiences

Speakers: Shane Morris, Michael Kordahi and David Lemphers. Originally meant to be presented by Leslie Nassar

I’ve been looking at creating a mobile version of MahTweets, using .NET CF. The three problems I have with .NET CF are limited controls available, it’s all WinForms crap, and only available on Windows Mobile phones. Silverlight, however, will be on Windows Mobile phones and Nokia’s S60 and S40 OS’ phones, uses XAML solving both WinForms problem, and amount of controls available!

A good list of S60 phones can be found at the Nokia Gaming Blog – I think the cool thing is that it includes the popular E65, and all (I think) of the powerful N series phones! It is foreseeable that other phones (or browsers) will eventually be able to play Silverlight as well!

Shane talked about how Mobile is already big, but is already accelerating faster than PC/laptop markets, and the ways designs will have to change not just for the limited capability or screen real estate, but the way the mobile user "snacks" on content.

IMG_2961

Michael demo’d Silverlight on a HTC WinMo phone, but unfortunately its "pre-pre-pre-beta", so we aren’t able to play with anything yet. Apparently some of the other Remix events around the world pulled the Silverlight mobile content! The goal of the Silverlight mobile project is to use the exact same Silverlight tools, and allow all existing Silverlight stuff to just work – you wont have to compile to "Silverlight Mobile", ala .NET and .NET CF.

Imagine Cup

During Session 5, I had Long talk me through what his teams project was all about. It is very cool, but rather than fumbling around to describe what it is, he’s already blogged about the team SOAK entry.

IMG_2990

Congratulations again to Long, David Burela, Edward Hooper, and Dimaz Pramudya! Good luck in France guys.

Overall

It was fantastic to see that some of the feedback from last year made the event change this year, such as including free wifi and ‘recharge’ stations. Unfortunately, the wifi/net connection weren’t too stable up until ~3pm, and other suggestions such as including pens for the feedback forms didn’t make it through, so Stephen and I pinched one of the vendor’s pens.

I can’t remember if I wrote down "better food", but this year had a lot less salmon and cold wedges! There were even TimTams! (’cause, you know, this it totally the most important part of the day).

This year the event was split across Melbourne and Sydney, and cut down to one day (each). This year’s venue (Melbourne Town Hall) was both better and worse than last year. More room to move between sessions and chairs to sit on, but higher ceilings (which created echo’s and "lost the vibe"), consistently bad lighting and uncomfortable chairs during the sessions all worked against the Town Hall. A few others agreed on the venue being ’so-so’, and Ed Hooper suggested that the Melbourne Convention Centre, which is where Heroes Happened was held, would have been a better choice – which I agree with.

Remix is still in an infant state, its still learning about itself, but it is developing, experimenting and evolving. While not everything was perfect, I still will be attending next year because despite all my complaints it was still a great (albeit exhausting) day. Next year, however I think I’ll just take my camera and a notepad, rather than laptop + camera, which is fairly weighty. I’ll also sit a bit closer so that some more of my photos turn out. Argh!

Just like last year, Nick Hodge has a summary post of activity on the blogosphere about Remix.


2 Comments
 

TweetSaver: WCF + WPF + fun

20 April 2008 Tags  , , ,

TweetSaver has slowly been creeping into the CodePlex project for MahTweets over the last week or so, and now the first alpha release of it is live!

tweetsaver

What is TweetSaver?

  • Twitter + Screensaver
  • Uses WCF to connect to MahTweets – you can have it on multiple machines with no extra API usage!
  • Uses WPF for display
  • Name is by Will, so blame him

It uses MahTweets (requires MahTweets “Alpha 3″ and up – Alpha 3 is designated by Change Sets 4309 or higher) and WCF for communication (TCP endpoint). Just like MahTweets, TweetSaver will get tweets in real time as MahTweets pushes out the tweets via WCF, which means no more Twitter API calls are made!

The really cool part of this would be if you had one MahTweets ’server’ (for lack of a better word), and TweetSaver on an entire office of computers. One connection/set of API calls could do an entire office (of…your twitter feed). If you went to any of the Australian Heroes Happens Here events, you’d have noticed they had lots of twitter screens setup, all logged into the Twitter webpage – this would get around having each one logged in, and look prettier ;)

Requirements

  • .NET 3.0
  • Twitter account
  • Jabber (G-Talk will do) account preconfigured for IM with Twitter
  • For TweetSaver to run correctly, MahTweets must be open

Installation

  • Download
  • Extract
  • Run MahTweets, click allow to the Windows Firewall request (if you want TweetSaver to work) and configure MahTweets
  • Right click on TweetSav.scr, and select ‘Install’ – no configuration is needed unless you want to try it out over a network

Disclaimer

You’re downloading this of your own free will, so we take no responsibility if your computer crashes, hard drive self formats, you develop a drinking problem, and so on and so forth. While it is an “alpha” release, at worst, it should either crash itself or hog resources – nothing “deadly”.

Download the release-build of changeset 4309


2 Comments
 

Mah Tweets: Now available and on Codeplex

5 April 2008 Tags  

What is Mah Tweets? According to the description (that Will wrote, I corrected),

Mah Tweets is a Twitter client with a difference.

In addition to using the regular Twitter API, it uses the Jabber protocol to get instant notifications from Twitter. This also means it’s capable of using Twitter’s Keyword Tracking notification services.

To sum that up, Mah Tweets gives you your twitter feed in real time (for people you have on device/im notifications), rather than what the usual Twitter client does, which is scrape the API feed every minute or so. Mah Tweets does that too, so those you don’t have on notification will still be seen!

image 

Requirements

  • Microsoft .NET Framework 3.0
  • Twitter Account
  • Jabber/XMPP-compliant IM Account (such as Gmail / Gtalk)

Download

PLEASE NOTE THIS IS AN ALPHA RELEASE.
Mah Tweets Alpha v2.1

Thanks to the help of Will, MahTweets is now also available on Codeplex…that means source code and all!

Codeplex for those who don’t know, is sort of like Microsoft’s answer to SourceForge. Through TeamExplorer 2005/2008, it allows you to manage it all through Visual Studio, instead of using a standalone tool (which for me, I’d always forget. My tests of CVS/SVN have failed because of that)


Comments Off