Archive for category Silverlight
Silverlight 4 quirks: OOB and loading MainWindow size/position
Posted by Valeriu Caraulean in Silverlight on October 12, 2010
Since we’re writing an Out-Of-Browser Silverlight application, we want it to look and behave like a normal windows app. One of the first things I’m adding to new apps, is saving Window’s position and size and reloading them when application starts next time. So, when user launches the app again, it will find the app same size and position as before
In WPF that’s done pretty straightforward and it just works! For Silverlight 4 it took me already half of the day, and it’s not done yet…
To save MainWindow’s position and size in Silverlight 4 you can attach to MainWindow’s Closing event where you can pick values for current size and position. Then, when loading app next time, in Application’s Startup event we can try load previous values.
Where’s the catch? Width and Height are set properly, while setting Left and Top properties has NO EFFECT. Then, if the window is “snapped” to margins (in Windows 7, maximized for full screen’s height, leaving width the same) it will not be able to restore to previous height, saying that value is out of bounds.
The code, very simple and straightforward:
public class MainWindowSettings
{
public static void Initialize()
{
if(!Application.Current.IsRunningOutOfBrowser)
return;
if (!Application.Current.HasElevatedPermissions)
return;
Initialize(Application.Current.MainWindow);
}
private static void Initialize(Window mainWindow)
{
var appSettings = new ApplicationSettings();
var settings = appSettings.Read<Settings>(Settings.Key, null);
if (settings != null)
{
mainWindow.WindowState = settings.WindowState;
if (settings.WindowState != WindowState.Maximized)
{
mainWindow.Height = settings.Height;
mainWindow.Width = settings.Width;
mainWindow.Top = settings.Top;
mainWindow.Left = settings.Left;
}
}
mainWindow.Closing += (sender, args) =>
{
var windowSettings = new Settings
{
Height = mainWindow.Height,
Width = mainWindow.Width,
Top = mainWindow.Top,
Left = mainWindow.Left,
WindowState = mainWindow.WindowState
};
new ApplicationSettings().Write(Settings.Key, windowSettings);
};
}
public class Settings
{
public const string Key = "MainWindowSettings";
public Settings()
{
WindowState = WindowState.Normal;
}
public double Width { get; set; }
public double Height { get; set; }
public double Top { get; set; }
public double Left { get; set; }
public WindowState WindowState { get; set; }
}
}
Anybody have a clue? An advice, other than “wait Silverlight v. Next”?
Update: I’ve copy/pasted this code in new empty app and discovered that saving Top&Left values is working as it should. But the problem with “snap” is still here. Googling it revealed that I’m not alone facing this issue. So, waiting Silverlight v. Next…
Houston, we have a problem. Unit tests in Silverlight…
Posted by Valeriu Caraulean in Development, Silverlight on September 29, 2010
Going deeply into Silverlight development I’ve hit a bump that have destabilized me.
I’m at the point where I want to begin write unit tests for a Silverlight app.
Until this moment, NUnit, Resharper and TeamCity were working formidably well in this regard, covering all my needs: I’ve been productive writing tests and tools were at the hand to pick them up & run. That’s when targeting .NET Framework.
If we’re back to Silverlight, major unit test frameworks are not ready. Silverlight support is either announced or present in some experimental state. And if even I’ll found a way to write my tests, Resharper and TeamCity will not be able to run them automatically, requiring manual tweaks or installing additional things.
Looking deeper for a solution…
This blog is about things I'm passionated - Software Development, User Experience, gadgets and few other facets of IT that are keeping me busy at work as well as fueling my knowledge and self-improvement demons.