Android toolbar menu Xamarin Forms custom renderer [NOT RESOLVED] - xamarin

I am new to custom renderers and can only do basic things like customizing entries, pickers etc. I am really trying to implement an android toolbar menu into my navbar on x.forms and I know I will need to do this through a custom renderer.
I just really do not know where to start. It has proven difficult to find what I need online so I am hoping someone could point me in the right direction.
What I want is something that looks like this:
Which simply drops down and presents a list of options. Thanks in advance.

If you don't need it on iOS then all you need to do is:
<ToolbarItem Title="title" Order="Secondary"/>
If you need it on iOS then even Custom Renderer won't help you.

As lvan said, if you want to implement this like picture, you just add ToolbarItem for ContentPage.
<ContentPage.ToolbarItems>
<ToolbarItem
Name="MenuItem1"
Order="Secondary"
Priority="0"
Text="Item 1" />
<ToolbarItem
Name="MenuItem2"
Order="Secondary"
Priority="1"
Text="Item 2" />
</ContentPage.ToolbarItems>
You don't need to custom render.
You can take a look the following article for more info.
https://xamarinhelp.com/xamarin-forms-toolbar/

Related

Change ToolbarItem Text/Icon parameters

I am new to MAUI. And I haven't done anything complex with Xamarin as well.
The application I am testing with, has Shell navigation. And I can't find a way to change the toolbar items.
I would like to be able to change either the FontSize of the text, or the color of the SVG image. And I do not want those changes to affect the rest of my application, if possible. But if there is no other way, I can live with it.
I have managed to add styling to buttons, Labels etc... If styling is an option to this, it will be even better.
If I am on the wrong path, if you point me out why, I will be also grateful.
Thank you in advance!
If you want to just change a part of pages in your project. You can try to use a <Shell.TitleView> instead of the <Shell.ToolBarItem> in the content pages you want. Such as:
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MauiAppTest.MainPage">
<Shell.TitleView>
<HorizontalStackLayout Margin="5" HorizontalOptions="End">
<Label Text="hello" TextColor="Red" FontSize="Medium" VerticalOptions="Center" HorizontalOptions="End" />
<Image Source="your image" Margin="10" Background="green"/>
</HorizontalStackLayout>
</Shell.TitleView>
You can change style of TabBar in Style.xaml in path \Resources\Style\. Search in file for Shell.TabBarForegroundColor. You will get TargetType="Shell" and this is style for AppShell.xaml.

How to create LibVLCSharp custom playback controls in Xamarin Forms?

I've been searching for days now for a guide on how to create the custom playback controls for LibVLCSharp that everyone seems to talk about, which I never found a guid for.
I simply want to create other buttons with event handlers for the bottom playback control panel, I tried this but throws a System.NullReferenceException exception on startup while getting into break mode...
<vlc:MediaPlayerElement MediaPlayer="{Binding MediaPlayer}" LibVLC="{Binding LibVLC}">
<vlc:MediaPlayerElement.PlaybackControls>
<vlc:PlaybackControls>
<vlc:PlaybackControls.ControlTemplate>
<ControlTemplate>
<Grid>
<StackLayout Orientation="Horizontal" HorizontalOptions="CenterAndExpand">
<Button Grid.Column="0" Text="Test 1"/>
<Button Grid.Column="1" Text="Test 1"/>
<Button Grid.Column="2" Text="Test 1"/>
</StackLayout>
</Grid>
</ControlTemplate>
</vlc:PlaybackControls.ControlTemplate>
</vlc:PlaybackControls>
</vlc:MediaPlayerElement.PlaybackControls>
</vlc:MediaPlayerElement>
I want it to act just like the original one (Auto hides, overlays on tapping, etc...) but with my own layout and controls. I also thought about using the existing one and try to override their handler to implement my own code and override the text property for each button to change its icon but no luck of finding any help.
Thanks in advance ^_^
The code you are interested in is here: https://code.videolan.org/videolan/LibVLCSharp/-/blob/3.x/src/LibVLCSharp.Forms/Shared/Themes/Generic.xaml
I also thought about using the existing one and try to override their handler to implement my own code and override the text property for each button to change its icon
That'd be the way to go.
This previous SO question might answer your question: https://stackoverflow.com/a/14217500/4064749
Just create a new Style based on PlaybackControlsStyle, override what you want and then set it on the PlaybackControls element.
I created https://code.videolan.org/videolan/LibVLCSharp/-/issues/309 recently to track the need of a tutorial to customize the MediaElement.
Further docs on style inheritance: https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/styles/xaml/inheritance
I finally found the problem which was making exceptions, when I create custom control template which completely correct, the MediaPlayerElement code behind by LibVLCSharp developers itself cannot find the elements with the names defined anymore as they used hardcoded names for the buttons and views instead of using bindings and dynamic setters.
Thus, several workarounds could be made to fix such issue, here are some of my ideas:
Use the generic style documented here and modify it without removing any elements but rather hide them out or overlay them.
Create your own style with controls obtaining the same names of the original ones.
Find a way to modify or maybe create a whole new playback control element using the original one which can be found here and here.
Thanks to mfkl's answer which helped me find out how everything worked under the hood to come up with the explaination, even though this took me a couple of days to figure out.

Removing right swipe gesture from list view using xamarin forms

Currently on my app i am populating a list view with various results which contain a slider, if the user right swipes they have the option to delete it, when they left swipe it nothing happens. Currently the slider can be hard to use because of the swipe motion in the list view, i want to keep the delete function in but is there any way i can remove the right swipe action? My app is cross platform but i would prefer to remove this from iOS.
This is how i am adding swipe functions:
<ViewCell.ContextActions>
<MenuItem Clicked="OnDelete" CommandParameter="{Binding .}"
Text="Remove" IsDestructive="True" />
<!-- <MenuItem Clicked="OnEdit" CommandParameter="{Binding .}"
Text="EDIT" IsDestructive="false" /> -->
</ViewCell.ContextActions>
The best solution I have discovered so far is to use the IsEnabled property. The swipe action/button will still appear, but it will be greyed out.
https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/menuitem#enable-or-disable-a-menuitem-at-runtime
Please try to use SwipeView control inside Listview or CollectionView
https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/swipeview

Duplicate Xamarin Navbar Toolbar Items

I'm making an app in Xamarin Forms and when I deploy it to Android, I get a weird problem. It appears that I have two toolbar items when I only declared one in XAML. Also, I believe this is proper behaviour, but I was wondering if it's possible to not display the navbar title on a lower line and rather display it upper center. Thanks!
Screenshot
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Biziboards.Views.MerchantListPage"
Title="Merchants">
<ContentPage.ToolbarItems>
<ToolbarItem Text="Home"
x:Name="btnHome"
Clicked="btnHome_Clicked"
Order="Primary"/>
</ContentPage.ToolbarItems>
In your pages Xaml.cs file:
NavigationPage.SetHasNavigationBar(this, false);

How can I make platform specific RelativeLayout.XConstraint in XAML?

I want to take RelativeLayout.XConstraint with some platform specific value. And also i want to do this in xaml with OnPlatform keyword.
I tried this one without luck,
<RelativeLayout>
<RelativeLayout.XConstraint>
<OnPlatform x:TypeArguments="Constraint" Android="{ConstraintExpression Type=Constant,Constant=6" iOS="{ConstraintExpression Type=Constant,Constant=3}" />
</RelativeLayout.XConstraint>
</RelativeLayout>
And also tried this one https://forums.xamarin.com/discussion/57281/how-can-i-make-platform-specific-relativelayout-xconstraint-in-xaml but it is showing exception "Constraints as specified contain an unsolvable loop".
Here what would be the x:TypeArguments for RelativeLayout.XConstraint or any other approach? Looking forward to any hints/suggestion on this.
You are applying the constraint to the RelativeLayout itself, but constraint are meant to be applied on items of the layout, like this.
<RelativeLayout>
<Label Text="Foo">
<RelativeLayout.XConstraint>
<OnPlatform x:TypeArguments="Constraint" Android="{ConstraintExpression Type=Constant,Constant=6}" iOS="{ConstraintExpression Type=Constant,Constant=3}" />
</RelativeLayout.XConstraint>
</Label>
</RelativeLayout>
Also, you're missing a closing curly brace (}) on the first ConstraintExpression markup.
I tested this, it works just fine with XamlC turned off.
If you are running XF 2.3.3, you might be hitting https://bugzilla.xamarin.com/show_bug.cgi?id=48242, but a fix exists (https://github.com/xamarin/Xamarin.Forms/pull/580) and will be released as part of the coming service release.

Resources