How to access tap or click of an toolbar image in android xamarin forms

有些话、适合烂在心里 提交于 2019-12-13 17:07:42

问题


I have placed an image inside toolbar like in the below code. I did this to have an toolbar icon on the left side.

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar 
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:popupTheme="@style/ThemeOverlay.AppCompat.Light">
    <ImageView
        android:id="@+id/homeicon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/appicon"
        android:scaleType="fitStart" />
</android.support.v7.widget.Toolbar>

I need to access the tap or click of the image in Activity.cs. It would be grateful, if you have any other way to place Toolbar icon on the leftside in xamarin forms.


回答1:


I am looking for a way to place toolbaritem on the left side of the toolbar in xamarin forms application

You could refer to my answer : Xamarin.forms toolbar Android .

Place ToolbarItem on the left side of the toolbar in Xamarin.Forms, put this code in yourPage.xaml :

<ContentPage.ToolbarItems>
    <ToolbarItem Text="Share"  Activated="EditClicked_Share"   Order="Primary" Priority="0" />
    <ToolbarItem Text="Create" Activated="EditClicked_Create"  Order="Primary" Priority="1" />
    <ToolbarItem Text="Save"   Activated="EditClicked_Save"    Order="Primary" Priority="2" Icon="ic_action_content_save.png" />
</ContentPage.ToolbarItems>

I need to access the tap or click of the image

In yourPage.cs :

private void EditClicked_Save(object sender, EventArgs e)
{
    System.Diagnostics.Debug.WriteLine("Picture Clicked");
}

Update :

This isn't currently supported in Forms, you could write a Custom Renderer that overrides the base Android Toolbar renderer.

Herer is a simple solution, create a picture which color is same with the Toolbar, in my project, my picture is blue.png :

Place it left side of the Toolbar, add some blank ToolbarItem between the picture and your ToolbarItem to adjust the space.

<ContentPage.ToolbarItems>
    <ToolbarItem  Activated="EditClicked_Save" Order="Primary" Priority="0" Icon="ic_action_content_save"/>
    <ToolbarItem  Order="Primary" Priority="0" />
    <ToolbarItem  Order="Primary" Priority="0" />
    <ToolbarItem  Order="Primary" Priority="0" />
    <ToolbarItem  Order="Primary" Priority="0" />
    <ToolbarItem  Order="Primary" Priority="1" Icon="blue"/>
</ContentPage.ToolbarItems>

Effect :



来源:https://stackoverflow.com/questions/47389936/how-to-access-tap-or-click-of-an-toolbar-image-in-android-xamarin-forms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!