This is my first time using Xamarin.Forms and I want to know how I can add an icon or an image. I tried to look it up, but it's very complicated
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:hello"
x:Class="hello.MainPage">
<StackLayout >
<Label Text="Hello"
VerticalOptions="Center"
HorizontalOptions="Center"
TextColor="White"
FontSize="30"
FontAttributes="Italic"/>
<Image /> I am trying to add it here
<Icon/> I am trying to add it here
<Label Text="Name"
VerticalOptions="Center"
HorizontalOptions="Center"
TextColor="White"
Font="large"/>
<entry placeholder="Enter your name here"/>
</StackLayout>
</ContentPage>
In Xamarin.Forms it is recommended to add images in native project's only because every platform needs different dimensions & resolutions images. So we are adding images here how we do for Android & iOS project.
Android:
drawable-mdpi
drawable-hdpi
drawable-xhdpi
drawable-xxhdpi
hdpi, mdpi drawable-xhdpi drawable-xxhdpi refer to the screen density in dots-per-inch.
It’s best practice to place your app launcher icons in mipmap (not the
drawable- folders)
Keep in mind that in Android, all images are in different folder but with same naming convention.
Add Images in Drawable folder:
iOS:
This method of working with images in an iOS app has been deprecated by Apple since iOS 9. You should use the Asset Catalog instead.
For more information, see Image Sizes and Filenames.
For iOS project, expand the Resources folder and add your images here. In iOS, we follow a naming convention to differentiate our images.
If your image name is abc.png, for high version of this image, you may use abc#2x.png or abc#3x.png etc.
Keep in mind that in iOS, all images are in same folder but with different naming convention.
Add Images in Resource folder:
UWP:
Universal Windows Platform (UWP) - Place images in the application's root directory with Build Action: Content.
UWP image file names can be suffixed with .scale-xxx before the file extension, where xxx is the percentage of scaling applied to the asset, e.g. myimage.scale-200.png. Images can then be referred to in code or XAML without the scale modifier, e.g. just myimage.png. The platform will select the nearest appropriate asset scale based on the display's current DPI.
Using Images in Shared project
<Image Source="Image1" WidthRequest="80" HeightRequest="80" ></Image>
You can set the Icon for a Page, but I do not know of an Icon control. Setting the icon for page can be done pointing to a local resource or online resource.
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:hello"
x:Class="hello.MainPage"
Icon="Icon.png">
Set show an Image you need to set the Source of the Image
<Image Source="waterfront.jpg" />
Related
I am trying to put a background image, so far I have this code:
<Grid.Background>
<ImageBrush ImageSource="ms-appx:///Assets/Windows-10-Hero-Ninja-Cat-1024x576-03a71eed2a427425.jpg" Stretch="UniformToFill"/>
</Grid.Background>
Which appears this way on UWP on Windows 10:
But when I Build the WASM or Android, the Background image does not appear.
The file property is set Build action: Content, Copy to output directory: Do not copy.
Uno's WASM target is still experimental and some features are not available yet. The only yet implemented background brush is SolidColorBrush.
It's implemented for iOS (source code here), but not Android.
Since you're already in a <Grid>, you can simply put your image as first element:
<Grid>
<Image Source="ms-appx:///Assets/Windows-10-Hero-Ninja-Cat-1024x576-03a71eed2a427425.jpg" Stretch="UniformToFill" />
[... put your other controls here]
</Grid>
In case you need to use different background approaches for each platform use xaml conditional prefixes.
https://platform.uno/docs/articles/platform-specific-xaml.html#xaml-conditional-prefixes
You can use <wasm:Image ... />
At the end of the day you can have the same result with flexible markup.
I am writing a UWP 14393 app using mediaplayerelement in xaml file, I am wondering how can I display extra information in text on top of custom transport media control so that when player control is up, the text will show up at the same time?
For example, for a video player showing an online stream, and at the top left corner shows streamer name, view count, etc. The information only shows up when player control shows up.
Obviously, the best way to do this is to put the text inside custom transport media control, is it doable? If not, how can I achieve this?
I am a newbie at UWP, so any help will be welcome, thanks.
You can custom the MediaTransportControls's style, and add your own content to <Border x:Name="ControlPanel_ControlPanelVisibilityStates_Border">. See my test.
First download MediaTransportControls's style from my gist.
Or you can find it in your pc's generic.xaml file.
Then add this style to App.xaml.
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/MediaTransportControls.xaml"/>
</ResourceDictionary.MergedDictionaries>
Then you can use your style in MediaTransportControls.
<MediaPlayerElement
Width="300" Height="500"
AreTransportControlsEnabled="True"
Source="ms-appx:///Assets/elephantsdream-clip-h264_sd-aac_eng-aac_spa-aac_eng_commentary-srt_eng-srt_por-srt_swe.mkv">
<MediaPlayerElement.TransportControls>
<MediaTransportControls Style="{StaticResource myMediaTransportControlsStyle}">
</MediaTransportControls>
</MediaPlayerElement.TransportControls>
</MediaPlayerElement>
<MediaPlayerElement AreTransportControlsEnabled="True">
<MediaPlayerElement.TransportControls>
<MediaTransportControls>
<Grid>
<...Put any content here like textblocks and buttons all this content will be part of controls so they will appear and disappear along with the controls, this Grid covers all the area on the screen ( above the controls bar )...>
</Grid>
</MediaTransportControls>
</MediaPlayerElement.TransportControls>
</MediaPlayerElement>
What I'm trying to do was extremely easy in SL/WP 8 but seems to be impossible in WP 8.1 without redefining the Hub template myself. I want to create a hub with a header that:
Scrolls horizontally.
Has a background that scrolls along with it.
Has no margins on either side.
I know this can probably be solved by just having my background image include the background and the hub just being transparent, but I wanted to know if there was a way to solve it in XAML.
Putting a Grid with a background into the Hub's header just highlights the background as much as the hub needs--not stretching all the way across:
<Hub>
<Hub.Header>
<Grid Background="Red" Height="60">
<TextBlock Text="My Header" />
</Grid>
</Hub.Header>
</Hub>
The above makes the header with the text "My Header" but only the text part has a background. Furthermore, the Hub itself seems to have inner margins of 16 on each side so the background doesn't stretch across the whole phone screen.
Should I just be going with a background or deconstructing the template to remove the margins?
Far from an elegant solution but basically I put the background outside the Hub and gave it negative margins like so. Hacky but I guess it works.
<Grid>
<!-- This is the header bar -->
<Grid Height="64" Background="Red" />
<Hub>
<Hub.Header>
<StackPanel Margin="-6,0,0,0">
...
</StackPanel>
</Hub.Header>
<!-- actually just defined the margin in my ResourceDictionary to target all HubSections -->
<HubSection Header="section 1" Margin="-2,-20,-4,8" />
<HubSection Header="section 2" Margin="-2,-20,-4,8" />
</Hub>
</Grid>
Tried adding a Coding4Fun RoundButton to my WP8 XAML. Looked around and copied from samples (i.e. this site), but despite the nice images I see everywhere, the text is displayed on top of my icon:
The XAML code is:
<c4f:RoundButton x:Name="buttonShrtn" Click="buttonShrtn_Click" ImageSource="Assets/Images/icon-button-32.png" FontSize="18" Content="Shrtn" VerticalAlignment="Center" HorizontalAlignment="Right"/>
I just can't find a similar case anywhere. Could there be something else in my XAML that can cause this?
You have to use Label instead of Content.
EDIT
RoundToggleButton, RoundButton, OpacityToggleButton, Tile, and ImageTile content property shifted to Label property. (geekchamp)
Toolkit description.
So I have a grid with a background. Inside the grid is a WebView and then some space on the left hand side of the screen where I have just placed a Button for now.
As the program runs, the left hand bar (that shows the grid with the background and the button laid out on it) doesn't render, instead I get the background, no controls on it and a black triangle (or geometric shape) at the bottom.
I suspect it's an issue with the VM and the video driver. I had a similiar issue with WPF a few years ago and MS's response was that I had an incompatible video driver that was causing the form to not render correctly at all times (this is very much the same behavior).
What can I do to prevent this? I'm including an image.
I'm going to include the small XAML I used and then a screenshot of the behavior (The XAML I rekeyed by hand):
<Grid>
<Grid.Background>
<ImageBrush ImageSource="Media/Background.jpg" />
</Grid.Background>
<TextBlock FontSize="24" Margin="15,15,0,0">Sample Label</TextBlock>
<WebView x:Name="wv1" Margin="250,0,0,0"></WebView>
<Button Content="Do Something" HorizontalAlignment="Left" Height="42" Margin="57,131,0,0" VerticalAlignment="Top" Width="170" Click="Button_Click1" />
</Grid>
VMs don't work well with multimedia. You should expect all sorts of problems with video.