Change background position when WP7 Page changes to Landscape - xaml

I am working on a Windows Phone 7 project with a panorama on the MainPage and multiple simple pages. All my pages have a background set this way:
<local:PhoneApplicationPage>
<Grid Background="{StaticResource PageBackground}">
content here
</Grid>
</local:PhoneApplicationPage>
PageBackground is an application resource set in default.xaml and light.xaml this way:
<ImageBrush x:Key="PanoramaBackground" ImageSource="/Resources/PanoramaBackground01Dark.jpg" Stretch="None" />
<ImageBrush x:Key="PageBackground" ImageSource="/Resources/PageBackground01Dark.jpg" Stretch="None" />
The PageBackground01Dark.jpg picture is of size 800x800 px.
When a page is displayed in the Portrait orientation, the picture is centered correctly horizontaly and the picture height corresponds to the page height. This is fine.
When a page is displayed in the Landscape orientation, the picture width corresponds to the page width but the picture is then centered vertically.
I would like my background picture to be "topped" in the page.
The Background property of a Grid is a brush with no interesting options. I would like not to create 2 pictures for this. There should be an obvious solution. Here is the result I would like to have:

Oh, the solution is simple. The Background property is of type Brush but it's in fact an ImageBrush. So the solution is:
<ImageBrush x:Key="PageBackground"
ImageSource="/Resources/PageBackground01Dark.jpg"
AlignmentY="0" />

Related

NavigationPage with iOS PrefersLargeTitles snaps title when scrolling

I tried to get the same large title behavior like the settings app on iOS when the user scrolls down. The transition between the large title and the small title is smooth. But on Xamarin Forms the header snaps.
In my NavigationPage i set PrefersLargeTitles="true". In the embedded content page i set LargeTitleDisplay="Always" and UseSafeArea="true". The content in the page looks like this:
<ContentPage.Content>
<AbsoluteLayout>
<ListView />
<Frame x:Name="LoadingFrame" /> <!-- Only visible until ListView is loaded -->
</AbsoluteLayout>
</ContentPage.Content>
I tried setting NavigationPage.IsNavigationBarTranslucent="True". Then it works, but only if i disable SafeArea (Page.UseSafeArea="False") in the ContentPage. Disabling SafeArea is not what i want, because now the content is behind the notch.
I had the same issue.
The trick was to change top constraint of list to superView instead of safe area which only works on Native.
extendedLayoutIncludesOpaqueBars = true;
On Xamarin.Forms, I tried these solutions:
Use a custom renderer to set the LayoutConstraints of the scrollable view (Doesn't work)
Create a UITableViewController in a custom renderer then convert the Xamarin TableView element to a UITableView and set it to the TableView property of the UITableViewController then PushViewController to the new UITableViewController. (work)
You can raise the problem on github for better support:
https://github.com/xamarin/Xamarin.Forms/issues

Border element with a background hides the other element under it. (UWP)

I have a grid with multiple buttons as children. Now I want to give this Grid rows a background color. So I added a border with a background color like this
Border brd = new Border
{
Margin = new Thickness(0, 2, 0, 2),
Background = (SolidColorBrush) Application.Current.Resources["RedBrush"],
CornerRadius = new CornerRadius(22),
};
Grid.SetColumn(brd,startColumn);
Grid.SetColumnSpan(brd, 9- startColumn);
Grid.SetRow(brd, startRow);
GridMain.Children.Add(brd);
All looks good to me, But when this is rendered all buttons are hidden as the Background color of Border only is visible in the row. How can I overcome this?
Instead of adding the Border from code behind, If I added in Xaml all works fine (border background is visible as button background and button text is visible)
<Border Grid.Row="5" Grid.Column="1" Grid.ColumnSpan="8" Background="{StaticResource RedBrush}" CornerRadius="22" Margin="0 2 0 2" />
But for various reasons I wan the ability to add this border from code behind itself.
If you want to set the background color for Grid, you can directly use Grid.Background to set, without adding a new Border.
But if you need to add a Border as a background, please pay attention to the calling sequence in the code.
Generally speaking, the element added later will be placed on the top layer of the previous element, forming a visual effect of covering.
From your problem description. You can put the code that adds Border first.
Thanks.

Removing background of a transparent image in Xamarin Forms?

I am simply displaying an Image on my Xamarin Forms page. The image is transparent and this is how it displaying on page:
How can I remove those white and grey background?
This is my source code:
<ContentPage.Content>
<StackLayout>
<Image x:Name="myimage" Aspect="Fill">
</Image>
</StackLayout>
</ContentPage.Content>
Code-behind
myimage.Source = ImageSource.FromResource("RailwayApp.bull.jpg");
My try:
I tried setting IsOpaque property to True/False neither worked. Also declared BackgroundColor="Transparent" on XAML page but not worked.
In this case you can't, the background is actually not transparent - it would have to be a transparent PNG or GIF to support this. This is a JPG, so the background is actually grey and white.
To make this work you will first have to actually make the image transparent by converting in one of the two transparent formats (and actually remove the background) and then it should display as transparent as expected.

uwp NavigationView control Titlebar

I have a problem with the NavigationView Control and the titlebar.
I have tried to extend the view into the titlebar to play with the acrylic effects of the standard NavigationView. But then I´ve noticed that the back and menu buttons are underneath the titlebar, so you´re not able to click them properly.
In the attached image, you can see that everything under the red line is clickable but when you go over it, you are targeting the titlebar.
Is there anything I can do to fix this behavior?
I don't know which version OS you are working on, I didn't see this problem in recent Windows insider OS.
You may workaround the not clickable problem by set a dummy Titlebar like below:
<Grid>
<Grid x:Name="AppTitleBar" Background="Transparent" />
<NavigationView IsBackEnabled="True" PaneDisplayMode="Top">
<NavigationView.MenuItems>
<NavigationViewItem Icon="Accept" Content="Accept" />
</NavigationView.MenuItems>
</NavigationView>
</Grid>
public MainPage()
{
this.InitializeComponent();
var coreTitleBar = CoreApplication.GetCurrentView().TitleBar;
coreTitleBar.ExtendViewIntoTitleBar = true;
Window.Current.SetTitleBar(AppTitleBar);
}

Xamarin forms: setting up a background image

I want to setup a background image for a page. Right now I am using
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="M.LaunchPage"
Title ="M" BackgroundImage="Mars.jpg">
<ContentPage.Content>
<StackLayout Padding="10,0,10,10" >
<Button VerticalOptions="CenterAndExpand" Text="Sign Up" TextColor="White" Clicked="OnSignUp" BackgroundColor="Maroon" />
<StackLayout Padding="10,0,10,10" Orientation="Horizontal" VerticalOptions="EndAndExpand" />
</StackLayout>
</ContentPage.Content>
</ContentPage>
This works well but the image is being filled multiple times to fit the screen. Cn anyone suggest better code so that I can fit the image on the page perfectly?
Thanks.
This is platform dependent, for example on iOS the following is used in the Form's iOS's PageRender:
View.BackgroundColor = UIColor.FromPatternImage(UIImage.FromBundle(bgImage));
The View.BackgroundColor will end up tiling your image when it is smaller the UIController's View dimensions as FromPatternImage is designed to be used with a small repeating pattern (for memory and draw performance reasons) and not full screen/page images.
In order for this auto-tiling not to happen, you would be to assign the background image in code based upon the device's screen resolution and page size at runtime and not statically assign it in XAML.
Note: You could bind the BackgroundImage property in a ViewModel where the calculations of which image to use could be done.
You should use each platform specifications, for example in iOS you need to have Mars#2x.jpg and Mars#3x.jpg,in our platform project in Forms you can specify from "Mars" it will pick the correct one.
On Android set the image in the correct dpi folder.