Xamarin.forms Xamarin.根据平台和设备形成不同的视图
我在Xamarin.Forms中有一个项目,我正在尝试创建4个视图:Android手机、Android平板电脑、iPhone和iPad。我尝试了一个小例子,在XAML中类似这样的例子:Xamarin.forms Xamarin.根据平台和设备形成不同的视图,xamarin.forms,Xamarin.forms,我在Xamarin.Forms中有一个项目,我正在尝试创建4个视图:Android手机、Android平板电脑、iPhone和iPad。我尝试了一个小例子,在XAML中类似这样的例子: <ContentPage.Content> <ContentView> <OnPlatform x:TypeArguments="View"> <On Plat
<ContentPage.Content>
<ContentView>
<OnPlatform x:TypeArguments="View">
<On Platform="iOS">
<!-- Use view for iOS here -->
<OnIdiom x:TypeArguments="View">
<OnIdiom.Phone>
<Frame x:Name="MainFrameiPhone">
<StackLayout BackgroundColor="#FF00FF">
</StackLayout>
</Frame>
</OnIdiom.Phone>
<OnIdiom.Tablet>
<Frame x:Name="MainFrameiPad">
<StackLayout BackgroundColor="#00FF00">
</StackLayout>
</Frame>
</OnIdiom.Tablet>
</OnIdiom>
</On>
<!--Same for android -->
<On Platform="Android">
...
</On>
</OnPlatform>
</ContentView>
</ContentPage.Content>
...
但是我不想重命名我在4个视图中使用的每个框架和组件(例如MainFrameiPhone
和MainFrameiPad
)
我做得对吗?或者我可以创建4个视图并从代码隐藏中调用每个视图吗
做这件事最好的方法是什么
谢谢 你可以用
在内容页中
public class MainFrameiPhone: Frame
{
public TealTemplate ()
{
...
// set the content here
}
}
public class MainFrameiPad: Frame
{
...
}
ControlTemplate mainFrameiPhone= new ControlTemplate(typeof(MainFrameiPhone));
ControlTemplate mainFrameiPad= new ControlTemplate(typeof(MainFrameiPad));
//...
public MainPage()
{
//...
ContentView content = new ContentView() {
Content = {
//...
}
};
if(Device.RuntimePlatform=="iOS")
{
if(Device.Idiom==TargetIdiom.Phone)
{
content.ControlTemplate = mainFrameiPhone;
}
else if(Device.Idiom == TargetIdiom.Phone)
{
content.ControlTemplate = mainFrameiPad;
}
}
else if(Device.RuntimePlatform == "Android")
{
if (Device.Idiom == TargetIdiom.Phone)
{
content.ControlTemplate = xxx;
}
else if (Device.Idiom == TargetIdiom.Phone)
{
content.ControlTemplate = xxx;
}
}
else
{
content.ControlTemplate = xxx;
}
Content = content;
}