Showing posts with label Silverlight. Show all posts
Showing posts with label Silverlight. Show all posts

Tuesday, August 16, 2011

Trouble with resources in WP7

I started a few projects on WP7. It was done just for fun and for improving my skills (Fun was profitable - I won in Ukrainian WP7 competition). So I had completed my projects and tried to localize it.
I met some issues:
  1. If you have experience in developing for web silverlight - first isn't new for you. When I edit/add/remove a item in resources's file I need to open resources designer's file and change access modifiers of parameterless constructor from "internal" to "public".
    It is know issue and know fix - proof.
  2. Second was new for me. It is strange issue because it appeared only in one of my projects. I created resources file and added key in Resources section of app.xaml (for binding to resources in my xaml files). My project started to crash after this.
    I had spent some time and found source of the issue. I noticed that Resource class (auto generated) is internal. I checked "Custom tool" field in properties of resource file. It was filled "ResXFileCodeGenerator" value. In my other projects this field contained "PublicResXFileCodeGenerator" value. I didn't found detailed description of "ResXFileCodeGenerator" but looks like that it is obsolete tool. I no idea how to it was set in my project.
One more note: it is for WP7 without "Mango" update. I hope I will not meet such issues after this update.

Monday, February 7, 2011

Trimmed TextBlock for Windows Phone 7.

Unfortunately WP7 is based on Silverlight 3. So we can't use some nice features of Silverlight 4 i.e. dynamic key word or TextBlock's trimming. But I've solved issue with trimming for Silverlight 3. Solution is here. Let me explain which code I use in WP7 project:
  • Generic.xaml: File contains style and template for custom control. This file is located in Themes folder. WP7 tools doesn't contains Resources.xaml file template. So I created xml file and name it as "Generic.xaml".
  • TrimmedTextBlock.cs: It is implementation of custom control. I just copied this file. One more note: I decrement c_POINTS_COUNT to "1" because screen resolution of desktop and mobile versions are different.
Easy coding.

Wednesday, March 24, 2010

Trimmed TextBlock for Silverlight.

Download source files - here

Not always Silverlight TextBlock that contains long text has enough space for shows all this text. TextBlock clips text in this case - this behaviour isn't best way for this. Because I wait that TextBlock not only trims but also adds "..." to clipped text. WPF has brilliant property that helps to resolve this trouble - TextTrimming. I implemented this possibility for Silverlight and now want to present it for you. Furthermore I implemented ToolTip that shows trimmed text (only if text is trimmed!)
Enjoy!

UPD: I have just updated performance and behaviour when width defined zero.

Wednesday, February 24, 2010

Trips&Ticks: Easy way to navigate from XAML to class's definition.

Working with XAML code in VS2008 I have just found that I can easy jump to class's or property definition. If I press CTRL and click left mouse button on certain class or property in XAML I will be immediately switched to Object Browser (if it is class or property of .NET or external dll) or to class's or property definition in my code (if it is class or property from my sources).
Eugene Dolinskyy just pay my attention that "clear" VS2008 doesn't have this possibility. This feature is presented by ReSharper.

Friday, October 16, 2009

Performance degrades in your Silverlight App!? What can you do?


Some time ago I had written Silverlight app and I confronted with difficulties of difference between WPF and Silverlight :). When I had vanquished mismatches I found that performance of my app was serous degraded. I have done a little investigate of ways that can help to correct this issues.
  1. MaxFrameRate
  2. EnableGPUAcceleration & BitmapCash
  3. CompositionTarget.Rendering
MaxFrameRate - presents the maximum number of frames that Silverlight can render per second. The default value is 60 second. I've set 30 in my app - looks pretty. This property likes DesiredFrameRate in WPF. Target is clear - reduce frame rate. It is useful when more animations are started in Silverlight app.
You can set it from Silverlight code:

App.Current.Host.Settings.MaxFrameRate=30;

or as parameter of object on HTML page:

<param name="framerate" value="30">

Pay attention there is a mismatch naming in Silverilght code and HTML. Why is it? You can read there.

EnableGPUAcceleration & BitmapCash - allows to cache visual object when it is possible. You should switch on EnableGPUAcceleration that Cache has any effect. This approach is useful when your app has opacity or transform operations applied to it.
You can set it from Silverlight code:

You can't change it from Silverlight because it should be set before app's initialization.

or as parameter of object on HTML page:

<param name="EnableGPUAcceleration" value="true" />

and set CacheMode. In current moment BitmapCache is supported only.
You can set it in C# code:

LayoutRoot.CacheMode = new BitmapCache();

or in XAML:

<Grid x:Name="LayoutRoot" CacheMode="BitmapCache">

Some objects don't look well (high-resolution images for example) you can increase RenderAtScale property of BitmapCache class for avoid this mess. Default value of this property is 1, when I set 4 (like MSDN's samples) quality restores but performance degrades again. A value between 2-3 is god for my app.
You can set it in C# code:

LayoutRoot.CacheMode = new BitmapCache
{
RenderAtScale = 2.5,
};


or in XAML:

<Grid x:Name="LayoutRoot">
<Grid.CacheMode>
<BitmapCache RenderAtScale="2.5" />
</Grid.CacheMode>
</Grid>


CompositionTarget.Rendering - this event rises before the objects in the composition tree are rendered. How does it can help? In common case - doesn't. But if you zoom your object in app (or in same other manner move out of bounds some item) - it is very useful. Look in my case:
First - subscribe event handler's method in constructor of your main window:

CompositionTarget.Rendering += new EventHandler( OnCompositionTargetRendering );

Second - handler method:

private void OnCompositionTargetRendering( object sender, EventArgs e )
{
//--- Start 1 -- //
foreach ( GalleryItem item in xGalleryCanvas.Children )
{
if ( Visibility.Collapsed == item.Visibility )
{
item.Visibility = Visibility.Visible;
}
}
//--- End 1 -- //
//--- Start 2 -- //
if ( _isZoomed && 0 < rect =" new" visibleitems =" VisualTreeHelper.FindElementsInHostCoordinates(" f =""> f.GetType() == typeof( GalleryItem ) );

foreach ( GalleryItem item in xGalleryCanvas.Children )
{
if ( !visibleItems.Contains( item ) )
{
item.Visibility = Visibility.Collapsed;
}
}
//--- End 2 -- //
}

Some explanation: You should ascertain that all items are visible (block 1).
Note: Dependency property works not so good as in WPF. So you should change Visibility only of collapsed items. In other case you serious reduce the performance.
Use mix of FindElementsInHostCoordinates() of VisualTreeHelper and LINQ for create collection all visible items in current moment and collapse all other items (block 2).

Promise these three approaches can help as these help me.

PS: Recently I've found useful tips of Silverlight performance. You can read it there - Performance Tips.