Silverlight: Place an element at the top of the screen? - silverlight

It is idiomatic in Windows Phone 7 to display a loading indicator at the very top of the screen. (I don't think there is any padding between the loading indicator and the top.)
What is the best way to do that in Silverlight? This XAML works, sorta:
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<!-- this needs to be at the very top of the screen -->
<ProgressBar x:Name="LoadingProgressBar" IsIndeterminate="True" Style="{StaticResource PerformanceProgressBar}" Grid.Row="0"/>
<controls:Pivot Title="SECTIONS" x:Name="pivotControl" SelectionChanged="pivotControl_SelectionChanged" Grid.Row="1" />
</Grid>
It places the ProgressBar near the top of the screen, but there's still space. I could just apply a negative margin, but I suspect there's a more elegant way. What is the preferred way of doing this?

Have a look in the definition of the PhoneApplicationPage. Is shell:SystemTray.IsVisible set to True of False?
My guess is that you've got it set to True (the default).
If this is the case then space at the top of the page will be reserved to display the system tray. (Which includes signal strength, battery level indicator, clock, etc.) If you change this to False then the layout engine will use all available space for your controls.
The downside to this is that the user won't be able to access the tray while your app is running.
While it's common to put an inditerminate wait indicator at the top of the screen, it's also not unusual to see it in the middle of the screen. Subject to what you're actually displaying on the page, you could consider this if you want to preserve the ability to see the system tray.
Alternatively you could use a completely custom wait cursor.

Your code works for me. I pasted your code in, replacing your Pivot control for a button.
What are you seeing?

Inbuilt in the windows phone control templates is metro styling. As you say you can use the likes of negative margins (or paddings in controls I've looked at)... iirc, the progressbar has styling such that if the height is not a minimum of 73 pixels then the progressbar is obscured.
Alternatively you can go to the source of this by retemplating the control in blend and controling the properties in the consituent components that make up the control.
To do this, rclick the control in blend, edit template, edit a copy and you're away.

Related

Connect the sides of two datagrids

I'm having some difficulties with formatting two datagrids so that they resize appropriately when their containing window is resized. Currently, the two datagrids are sitting side by side so that they nearly touch in the center of the window.
The top, bottom, and edge-facing sides of each datagrid are connected/linked to the nearest side of the window (I used the link icon in the center of each side). Is there a way to make the center facing sides of the datagrids connect to each other (or to the center of the window), so that when the window is resized, these center-facing sides stay close to the center?
Thanks in advance for any help!
From the way you ask your question, I believe you're coming from WinForms development. In WPF, things are a lot different. I would suggest that you get a WPF book and start writing the XAML directly and not use the designer.
For your problem, the solution is pretty straightforward. All you have to do is use a grid with 2 columns:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<DataGrid Grid.Column="0"></DataGrid>
<DataGrid Grid.Column="1"></DataGrid>
</Grid>
The Width="*" attribute will make sure that the columns are the same width and that they take up all available space.
The trouble with using the designer is that when you do that, it will start putting hardcoded margins and sizes in your XAML and you will lose all of the cool auto-sizing features of WPF. Therefore I strongly suggest writing XAML manually and not using the designer (except to see what your window/control looks like).

WPF control origin point

After some investigation, I still can't find method to change origin of control.
So, I want just to place one square exactly in center of another square, without margins, so it will be completely independent of first square size.
Theoretically, it can be easily done with HorizontalAlignment and VerticalAlignment set to Center, since it automatically sets Margin of control to half of width and height of parent control. But it is not so simple.
Simplest way to describe problem is next picture
As you can see, margin is counted towards upper left corner. Which is what I call origin. The perfect solution is to change it to center of first square, but this is where I need help - how can I do that?
Point of origin applies when using a transform object, and attaching the transform to your control. It won't actually effect the behaviour of the margin or left, top properties. If using a transform to place your object, point of origin is very useful.
The top, left (if using cavas) and margin (if using say grid) help govern the "auto" placement by the parent control, and this in turn governs where point of origin for the control winds up being relative to the parent control. The transform object then offsets RELATIVE to where that point of origin is.
The other useful thing is that transform overrides the auto placement in the parent control, or rather, forces an offset to where the parent wants to put it, which in some cases is useful - i.e., you might have boxes listed in a grid and want them to "shake" left and right when you hover the mouse over them, their alignment stays in order to the grid, but the transform lets you bump them away from their "forced" position.
For example, attach the same transform object to 2 controls, and set their origins separate, then apply an animation to the transform object - both controls will animate off the one animation object (if you wanted to their movement in perfect sync).
Well, it was weird enough. The given behaviour can be seen only when using Image, and Center alignment. Can be solved by either wrapping Image in Grid, which will be using Center alignment, or using Stretch alignment with Image (which is much simplier).
<Grid Width="500" Height="500">
<Image Width="250" Height="250" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"/>
</Grid>
If you want to reproduce problem I've described in question, replace Stretch with Center in code above.
Probably oversimplifying here but I would just use a Grid to wrap the two items you mentioned like this example (One stretched to fit and one centered):
<Grid>
<Border HorizontalAlignment="Stretch" VerticalAlignment="Stretch" BorderBrush="Black" BorderThickness="10" Margin="4"/>
<Button HorizontalAlignment="Center" VerticalAlignment="Center" Padding="10">InnerButton</Button>
</Grid>

How to make WPF scrollviewer smoother

I'm using a scrollviewer like this:
<ScrollViewer PanningMode="Both" >
<TextBlock FontSize="15" TextWrapping="Wrap">
(a block of random text)
</TextBlock>
</ScrollViewer>
I am using a very decent new laptop with a touchscreen but if I flick the text, the movement is not nice, it is jerky. If I use any windows 8 apps with touching / flicking etc the movement is silky smooth.
Is this an issue with WPF? Is there any way to get smooth scrolling?
Thanks
Ensure that ScrollViewer.CanContentScroll is set to false.
<ScrollViewer PanningMode="Both" CanContentScroll=false>
<TextBlock FontSize="15" TextWrapping="Wrap">
(a block of random text)
</TextBlock>
</ScrollViewer>
Physical vs. Logical Scrolling
Physical scrolling is used to scroll content by a predetermined
physical increment, typically by a value that is declared in pixels.
Logical scrolling is used to scroll to the next item in the logical
tree. Physical scrolling is the default scroll behavior for most Panel
elements. WPF supports both types of scrolling.
http://msdn.microsoft.com/en-us/library/system.windows.controls.scrollviewer.cancontentscroll%28v=vs.110%29.aspx
Alternatively, you could try this fella:
SurfaceScrollViewer Class
Users can flick the content of a SurfaceScrollViewer control by
touching the control, rapidly moving their finger a short distance,
and then lifting their finger. When their finger is lifted, the
content of the SurfaceScrollViewer control continues to move. The
content then decelerates and comes to a stop. You can programmatically
stop the movement caused by flicking by calling the StopFlick method.
You can manipulate the content of a SurfaceScrollViewer either by
moving a scroll bar (like with the WPF SurfaceScrollViewer) or by
direct touch interaction with the content itself. The ability to
manipulate the content directly is called panning. By default, panning
is enabled. You can disable it by setting the IsManipulationEnabled
property to false.
http://msdn.microsoft.com/en-us/library/microsoft.surface.presentation.controls.surfacescrollviewer.aspx
(Archived link)

MediaElement audio/video out of sync when inside a ViewBox

I'm using Viewbox controls to proportionally size content in my app at a certain display size. I don't know what the display size of the target computer will be, but I need the app's layout, fonts, graphics, etc to look the same on the target computer as on my machine. Viewbox has worked great for me in the past to accomplish this.
However, when I include a MediaElement (to play a WMV) inside the Viewbox, the media's audio and video get out of sync. The video lags behind, then speeds up, then lags behind, then speeds up, etc.
My actual layout looks something like this:
<Grid>
<Viewbox>
<Grid Width="888" Height="500"> <!-- 16:9 screen size -->
<!-- row definitions -->
<Viewbox Grid.Row="1">
<Grid>
<custom:TransitionFrame />
<!-- TransitionFrame inherits from Frame -->
<!-- TransitionFrame holds a Page that
looks like this:
<Grid>
<MediaElement />
</Grid>
-->
</Grid>
</Viewbox>
</Grid>
</Viewbox>
</Grid>
Any ideas on why this would impact the video? Any ideas on how to resolve it?
Ugh. This has caused me sooooooo much issues. Its really depends on the machine. I have a pretty solid development machine and have never seen any issues. Then I ran the application on another normal machine and it was choppy as hell. I had to play with the following settings:
Switching monitors
Switching screen refresh rates
Switching resolution
Switching graphic cards.
Then when I got the out-of sync issues to < 500ms I created another layout and put in an option saying low graphics and it played fine. I made the other layout very basic with no more than one ViewBox.
I recommend staying away from Viewboxes with MediaElements

Silverlight MultiscaleImage fill available height

I have a sample app with a MultiScaleImageControl. By default it fills the available ViewPort width. How can i make it use the available height?
Edit (Copy from comment)
It's nothing more than
<Grid x:Name="LayoutRoot" Background="White">
<MultiScaleImage HorizontalAlignment="Left" Name="multiScaleImage1" VerticalAlignment="Top" />
</Grid>
My question doesn't aim at the width of the control itself, but at the displayed MultiScaleImage...
I would recommend that you remove the VerticalAlignment property. In fact get rid of the HorizontalAlignment as well. This lets the containing grid size the control for you.
You may also be seeing the control simply maintaining the aspect ratio of the image. If the image is wider than its high then at zoom level 1 the image will not fill the entire height.

Resources