如何在Android Xamarin表单中访问点击或单击工具栏图像 [英] How to access tap or click of an toolbar image in android xamarin forms
问题描述
我已将图像放置在工具栏中,如下面的代码所示.我这样做是为了在左侧具有工具栏图标.
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>
我需要访问Activity.cs中图像的点击或单击. 如果您还有其他方法可以将工具栏图标以xamarin的形式放置在左侧,将不胜感激.
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.
推荐答案
我正在寻找一种在xamarin表单应用程序中将工具栏项目放置在工具栏左侧的方法
I am looking for a way to place toolbaritem on the left side of the toolbar in xamarin forms application
您可以参考我的回答: Xamarin.forms工具栏Android .
You could refer to my answer : Xamarin.forms toolbar Android .
将ToolbarItem
放置在工具栏左侧的Xamarin.Forms
中,将此代码放置在yourPage.xaml
中:
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
在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是一个简单的解决方案,创建一个颜色与Toolbar
相同的图片,在我的项目中,我的图片是blue.png
:
Herer is a simple solution, create a picture which color is same with the Toolbar
, in my project, my picture is blue.png
:
将其放在Toolbar
的左侧,在图片和ToolbarItem
之间添加一些空白的ToolbarItem
,以调整空间.
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>
效果:
这篇关于如何在Android Xamarin表单中访问点击或单击工具栏图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!