Xamarin.Forms 应用程序的操作栏中的按钮?
Button in action bar for Xamarin.Forms application?
是否有一种 Xamarin.Forms 方法可以将按钮添加到 actionbar/navigation 项目(无需借助平台特定代码)?
如果通过操作 bar/navigation 你的意思是顶部的导航栏你可以使用这个方法:
private void ShowToolbar()
{
if (Device.OS == TargetPlatform.iOS)
{
// move layout under the status bar
this.Padding = new Thickness(0, 20, 0, 0);
toolbarItem = new ToolbarItem("Sync", "sync_icon.png", () =>
{
//if (!response)
//{
// response = true;
SyncService();
//}
//else
// return;
}, 0, 0);
ToolbarItems.Add(toolbarItem);
}
if (Device.OS == TargetPlatform.Android)
{
toolbarItem = new ToolbarItem("Sync", "sync_icon.png", () =>
{
//if (!response)
//{
SyncService();
//}
//else
// return;
}, 0, 0);
ToolbarItems.Add(toolbarItem);
}
if (Device.OS == TargetPlatform.WinPhone)
{
toolbarItem = new ToolbarItem("Sync", "sync_icon.png", () =>
{
//if (!response)
//{
// response = true;
SyncService();
//}
//else
// return;
}, 0, 0);
ToolbarItems.Add(toolbarItem);
}
}
在MainPage.xaml中,您可以添加以下代码。
<ContentPage.ToolbarItems>
<ToolbarItem Text="Click Me!" Icon="iconName.png" Clicked="ToolbarItem_Clicked"/>
</ContentPage.ToolbarItems>
现在,在 MainPage.xaml.cs 中,应该添加点击处理程序。
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
private async void ToolbarItem_Clicked(object sender, EventArgs e)
{
await Navigation.PushAsync(new NewPage());
}
}
要进行导航,App.xaml.cs 中的构造函数应包含以下代码。
public App()
{
InitializeComponent();
MainPage = new NavigationPage(new MainPage());
}
是否有一种 Xamarin.Forms 方法可以将按钮添加到 actionbar/navigation 项目(无需借助平台特定代码)?
如果通过操作 bar/navigation 你的意思是顶部的导航栏你可以使用这个方法:
private void ShowToolbar()
{
if (Device.OS == TargetPlatform.iOS)
{
// move layout under the status bar
this.Padding = new Thickness(0, 20, 0, 0);
toolbarItem = new ToolbarItem("Sync", "sync_icon.png", () =>
{
//if (!response)
//{
// response = true;
SyncService();
//}
//else
// return;
}, 0, 0);
ToolbarItems.Add(toolbarItem);
}
if (Device.OS == TargetPlatform.Android)
{
toolbarItem = new ToolbarItem("Sync", "sync_icon.png", () =>
{
//if (!response)
//{
SyncService();
//}
//else
// return;
}, 0, 0);
ToolbarItems.Add(toolbarItem);
}
if (Device.OS == TargetPlatform.WinPhone)
{
toolbarItem = new ToolbarItem("Sync", "sync_icon.png", () =>
{
//if (!response)
//{
// response = true;
SyncService();
//}
//else
// return;
}, 0, 0);
ToolbarItems.Add(toolbarItem);
}
}
在MainPage.xaml中,您可以添加以下代码。
<ContentPage.ToolbarItems>
<ToolbarItem Text="Click Me!" Icon="iconName.png" Clicked="ToolbarItem_Clicked"/>
</ContentPage.ToolbarItems>
现在,在 MainPage.xaml.cs 中,应该添加点击处理程序。
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
private async void ToolbarItem_Clicked(object sender, EventArgs e)
{
await Navigation.PushAsync(new NewPage());
}
}
要进行导航,App.xaml.cs 中的构造函数应包含以下代码。
public App()
{
InitializeComponent();
MainPage = new NavigationPage(new MainPage());
}