silverlight interview questions
Top silverlight frequently asked interview questions
I have a very simple test app just to play around with Windows Phone 7. I've just added a TextBox
and a TextBlock
to the standard UI template. The only custom code is the following:
public partial class MainPage : PhoneApplicationPage
{
public MainPage()
{
InitializeComponent();
}
private int counter = 0;
private void TextBoxChanged(object sender, TextChangedEventArgs e)
{
textBlock1.Text += "Text changed " + (counter++) + "\r\n";
}
}
The TextBox.TextChanged
event is wired up to TextBoxChanged
in the XAML:
<TextBox Height="72" HorizontalAlignment="Left" Margin="6,37,0,0"
Name="textBox1" Text="" VerticalAlignment="Top"
Width="460" TextChanged="TextBoxChanged" />
However, every time I press a key when running in the emulator (either the on-screen keyboard or the physical one, having pressed Pause to enable the latter) it increments the counter twice, displaying two lines in the TextBlock
. Everything I've tried shows that the event is genuinely firing twice, and I've no idea why. I've verified that it's only being subscribed once - if I unsubscribe in the MainPage
constructor, nothing happens at all (to the text block) when the text changes.
I've tried the equivalent code in a regular Silverlight app, and it didn't occur there. I don't have a physical phone to reproduce this with at the moment. I haven't found any record of this being a known problem in the Windows Phone 7.
Can anyone explain what I'm doing wrong, or should I report this as a bug?
EDIT: To reduce the possibility of this being down to having two text controls, I've tried removing the TextBlock
completely, and changing the TextBoxChanged method to just increment counter
. I've then run in the emulator, typed 10 letters and then put a breakpoint on the counter++;
line (just to get rid of any possibility that breaking into the debugger is causing issues) - and it shows counter
as 20.
EDIT: I've now asked in the Windows Phone 7 forum... we'll see what happens.
Source: (StackOverflow)
I have a vertical StackPanel with two elements: a Button and a ListBox. How can I have the ListBox stretch to the remaining page height?
<StackPanel Height="Auto" Width="Auto">
<Button Height="30" Width="100" Content="Get Content" x:Name="GetContent"/>
<ListBox Height="Auto" Width="Auto" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"/>
</StackPanel>
Note that I got this to work using a Grid container:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Button Width="100" Height="30" Content="Get Content" Click="OnGetContent" Grid.Row="0" Grid.Column="0"/>
<data:DataGrid x:Name="MyContent" Margin="0,5" Grid.Row="1" Grid.Column="0"/>
</Grid>
Source: (StackOverflow)
My company develops several types of applications. A lot of our business comes from doing multimedia-type apps, typically done in Flash. However, now that side of the house is starting to migrate towards doing Flex development.
Most of our other development is done using .NET. I'm trying to make a push towards doing Silverlight development instead, since it would take better advantage of the .NET developers on staff. I prefer the Silverlight platform over the Flex platform for the simple fact that Silverlight is all .NET code. We have more .NET developers on staff than Flash/Flex developers, and most of our Flash/Flex developers are graphic artists (not real programmers). Only reason they push towards Flex right now is because it seems like the logical step from Flash.
I've done development using both, and I honestly believe Silverlight is easier to work with. But I'm trying to convince people who are only Flash developers.
So here's my question: If I'm going to go into a meeting to praise Silverlight, why would a company want to go with Silverlight instead of Flex? Other than the obvious "not everyone has Silverlight", what are the pros and cons for each?
Source: (StackOverflow)
I've installed Visual Studio 2010 (fresh install), then applied the SP1. Now I've tried to install the silverlight 4 tools, but i'm getting a strange error message :
"Visual Studio 2010 or Visual Web Developer Express 2010 or Visual Phone Developer Express 2010 that matches the language version of Silverlight Tools 4 must be installed before installation of Silverlight Tools can continue. Silverlight Tools is available in other languages at http://go.microsoft.com/fwlink/?LinkId=177432.
The Visual Web Developer feature for Visual Studio 2010 must be installed before installation can continue."
I've triple checked and there's no language conflict. Browsing the web, some guys have the same error, but there are some MSDN posts that say that with SP1 you don't need to install silverlight tools because they're already bundled with SP1, but that's not the case here, I don't have any project templates in VS for silverlight, and the tools aren't installed. Any ideas??
Source: (StackOverflow)
I've got a Windows Phone Silverlight app, and there are some numbers to the right when I run the emulator:
What do they mean, and how can I hide them?
Source: (StackOverflow)
I have the feeling that Flash-based ( or Silverlight-based) websites are generally frowned upon, except when you are creating games or multimedia-content rich applications. Why this is so?
Source: (StackOverflow)
Given a StackPanel:
<StackPanel>
<TextBox Height="30">Apple</TextBox>
<TextBox Height="80">Banana</TextBox>
<TextBox Height="120">Cherry</TextBox>
</StackPanel>
What's the best way to space out the child elements so that there are equally-sized gaps between them, even though the child elements themselves are of different sizes? Can it be done without setting properties on each of the individual children?
Source: (StackOverflow)
I want to pass an enum value as command parameter in WPF, using something like this:
<Button
x:Name="uxSearchButton"
Command="{Binding Path=SearchMembersCommand}"
CommandParameter="SearchPageType.First"
Content="Search">
</Button>
SearchPageType
is an enum and this is to know from which button search command is invoked.
Is this possible in WPF, or how can you pass an enum value as command parameter?
Source: (StackOverflow)
If I call SelectAll
from a GotFocus
event handler, it doesn't work with the mouse - the selection disappears as soon as mouse is released.
EDIT: People are liking Donnelle's answer, I'll try to explain why I did not like it as much as the accepted answer.
- It is more complex, while the accepted answer does the same thing in a simpler way.
- The usability of accepted answer is better. When you click in the middle of the text, text gets unselected when you release the mouse allowing you to start editing instantly, and if you still want to select all, just press the button again and this time it will not unselect on release. Following Donelle's recipe, if I click in the middle of text, I have to click second time to be able to edit. If I click somewhere within the text versus outside of the text, this most probably means I want to start editing instead of overwriting everything.
Source: (StackOverflow)
What is the difference betweeen the following:
All these dependency properties are defined in Selector class. I often confuse SelectedItem
with SelectedValue
, and SelectedValue
with SelectedValuePath
.
I would like to know the difference between them, and also when do we use them, especially SelectedValue
and SelectedValuePath
. Please explain their use with some simple examples.
Source: (StackOverflow)
So, the web, and StackOverflow, have plenty of nice answers for how to bind a combobox to an enum property in WPF. But Silverlight is missing all of the features that make this possible :(. For example:
- You can't use a generic
EnumDisplayer
-style IValueConverter
that accepts a type parameter, since Silverlight doesn't support x:Type
.
- You can't use
ObjectDataProvider
, like in this approach, since it doesn't exist in Silverlight.
- You can't use a custom markup extension like in the comments on the link from #2, since markup extensions don't exist in Silverlight.
- You can't do a version of #1 using generics instead of
Type
properties of the object, since generics aren't supported in XAML (and the hacks to make them work all depend on markup extensions, not supported in Silverlight).
Massive fail!
As I see it, the only way to make this work is to either
- Cheat and bind to a string property in my ViewModel, whose setter/getter does the conversion, loading values into the ComboBox using code-behind in the View.
- Make a custom
IValueConverter
for every enum I want to bind to.
Are there any alternatives that are more generic, i.e. don't involve writing the same code over and over for every enum I want? I suppose I could do solution #2 using a generic class accepting the enum as a type parameter, and then create new classes for every enum I want that are simply
class MyEnumConverter : GenericEnumConverter<MyEnum> {}
What are your thoughts, guys?
Source: (StackOverflow)
In Silverlight 4 app; what does this error mean?:
"Error HRESULT E_FAIL has been returned from a call to a COM component."
It's a very generic error. The VS debugger doesn't point to the exact location of the error when debugging.
Source: (StackOverflow)
Why did Microsoft go the route of making dependency properties and dependency objects instead of using reflection and maybe attributes?
Source: (StackOverflow)