如何处理按钮从iOS和Android PAR分别从XAMARIN iOS和XAMARIN Android应用程序使用XAMARIN表格使用PCL
我是C#和Xamarin的新手,我正在创建Xamarin表单应用程序,希望创建一个按钮,可以处理来自不同平台(即iOS和Android)的不同任务。 假设我想使用一个绑定库(android绑定库和另一个是iOS绑定库)。在Xamarin Forms应用程序中单击一个按钮,我想执行特定于平台的库方法,例如,假设在Android绑定库中有一个方法如何处理按钮从iOS和Android PAR分别从XAMARIN iOS和XAMARIN Android应用程序使用XAMARIN表格使用PCL,xamarin,xamarin.forms,Xamarin,Xamarin.forms,我是C#和Xamarin的新手,我正在创建Xamarin表单应用程序,希望创建一个按钮,可以处理来自不同平台(即iOS和Android)的不同任务。 假设我想使用一个绑定库(android绑定库和另一个是iOS绑定库)。在Xamarin Forms应用程序中单击一个按钮,我想执行特定于平台的库方法,例如,假设在Android绑定库中有一个方法void ShowAndroidContent()在另一个iOS绑定库上有一个方法-(void)ShowiOSContentso在按钮上单击设备是否为And
void ShowAndroidContent()
在另一个iOS绑定库上有一个方法-(void)ShowiOSContent
so在按钮上单击设备是否为AndroidShowAndroidContent()代码>应该执行,如果是iOS,则ShowiOSContent()代码>应该被执行。
我的解决方案名称是XamarinFormsAppDemo
TestXamarinFormAppPage.xaml代码是:-
<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:TestXamarinFormApp" x:Class="TestXamarinFormApp.TestXamarinFormAppPage">
<AbsoluteLayout>
<StackLayout AbsoluteLayout.LayoutBounds="0,1,1,0.1"
AbsoluteLayout.LayoutFlags="All"
BackgroundColor="Transparent">
<Button Text="Click Me !" Clicked="OnButtonClicked"></Button>
</StackLayout>
</AbsoluteLayout>
<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:TestXamarinFormApp" x:Class="TestXamarinFormApp.TestXamarinFormAppPage">
<AbsoluteLayout>
<StackLayout AbsoluteLayout.LayoutBounds="0,1,1,0.1"
AbsoluteLayout.LayoutFlags="All"
BackgroundColor="Transparent">
<Button Text="Click Me !" Clicked="OnButtonClicked"></Button>
</StackLayout>
</AbsoluteLayout>
using System;
using Xamarin.Forms;
using System.Runtime.CompilerServices;
using System.ServiceModel.Channels;
namespace TestXamarinFormApp
{
public partial class TestXamarinFormAppPage : ContentPage
{
public TestXamarinFormAppPage ()
{
InitializeComponent ();
}
public void OnButtonClicked (object sender, EventArgs args)
{
DependencyService.Get<IR_Btn_Click> ().BtnPressed ();
}
}
我用的是Xamarin工作室。
有人能帮我做这件事吗。请帮帮我。谢谢。使用
您可以使用Device.OS
将设置为枚举值之一:
- iOS
- 安卓
- WinPhone(针对Windows 8 Silverlight返回)
- 窗户
这使特定于平台的检查能够改变布局或功能,如以下if语句:
public void OnButtonClicked (object sender, EventArgs args)
{
if (Device.OS == TargetPlatform.iOS)
{
//iOS
}
else if (Device.OS == TargetPlatform.Android)
{
//Android
}
}
我得到了上述问题的解决方案,可以使用DependencyService
来处理。为了实现这个目标,我为Interface对象创建了一个.cs类,并使用DependencyService
来处理平台特定类中的单击。以下是执行此操作的代码
接口对象类.cs
TestXamarinFormAppPage.xaml代码是:-
<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:TestXamarinFormApp" x:Class="TestXamarinFormApp.TestXamarinFormAppPage">
<AbsoluteLayout>
<StackLayout AbsoluteLayout.LayoutBounds="0,1,1,0.1"
AbsoluteLayout.LayoutFlags="All"
BackgroundColor="Transparent">
<Button Text="Click Me !" Clicked="OnButtonClicked"></Button>
</StackLayout>
</AbsoluteLayout>
<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:TestXamarinFormApp" x:Class="TestXamarinFormApp.TestXamarinFormAppPage">
<AbsoluteLayout>
<StackLayout AbsoluteLayout.LayoutBounds="0,1,1,0.1"
AbsoluteLayout.LayoutFlags="All"
BackgroundColor="Transparent">
<Button Text="Click Me !" Clicked="OnButtonClicked"></Button>
</StackLayout>
</AbsoluteLayout>
using System;
using Xamarin.Forms;
using System.Runtime.CompilerServices;
using System.ServiceModel.Channels;
namespace TestXamarinFormApp
{
public partial class TestXamarinFormAppPage : ContentPage
{
public TestXamarinFormAppPage ()
{
InitializeComponent ();
}
public void OnButtonClicked (object sender, EventArgs args)
{
DependencyService.Get<IR_Btn_Click> ().BtnPressed ();
}
}
我不能这样做,因为我们正在为android和iOS使用不同的绑定库,并且iOS绑定库在PCL中不可访问,因此我可以从iOS部分处理它,因此我需要调用我应用程序的iOS部分中定义的方法。请建议我如何实现此目标。设备。OS
已过时。现在应该使用Device.TargetPlatform
,我不能这样做,因为我们正在为android和iOS使用不同的绑定库,并且iOS绑定库在PCL中不可访问,因此我可以从iOS部分处理它,所以我需要调用我应用程序的iOS部分中定义的方法。请建议我如何实现此目标。使用DI注入每个平台的相关代码是的,我得到了,我的问题已经解决了,谢谢大家。Device.OnPlatform(…)
已经过时了。现在应该使用Device.TargetPlatform
using System;
using AVFoundation;
using Xamarin.Forms;
using TestXamarinFormApp.iOS;
[assembly: Dependency (typeof (MainPage))]
namespace TestXamarinFormApp.iOS
{
public class MainPage : IR_Btn_Click
{
public MainPage ()
{
}
public void BtnPressed ()
{
Console.WriteLine ("Hurray ! the button is clicked from an iOS Device.");
}
}
}