c# - 使用 xaml 在 Xamarin.Forms 中为 iPhone 和 iPad 分离 UI

标签 c# android ios xaml xamarin.forms

我一直在开发支持 iOS 和 Android 平台的 Xamarin.Forms 应用程序。 UI 主要是使用 Forms PCL 项目中的 XAML 构建的。我知道可以通过代码使用

if(Device.OS == TargetPlatform.iOS && Device.Idiom == TargetIdiom.Tablet)

如何通过 XAML 为 iPad 构建新的 UI?

最佳答案

你可以在xaml中使用成语值OnIdiom:

<Grid VerticalOptions="FillAndExpand">
  <Grid.ColumnSpacing>
    <OnIdiom x:TypeArguments="x:Double"
             Phone="20"
             Tablet="40"/>
 </Grid.ColumnSpacing>
</Grid>

有了这个,您可以在 xaml 中为手机或平板电脑设置不同的值。

当您的页面设计对于手机和平板电脑有很大不同时,您最好实现两个不同的页面并导航到正确的页面,使用您在问题中发布的 idom 代码:​​

if (Device.Idiom == TargetIdiom.Tablet || Device.Idiom == TargetIdiom.Desktop)
    await Navigation.PushAsync(new TabletPage());
else
    await Navigation.PushAsync(new Page());

this blog post来自 James Montemagno,它准确地描述了您的问题。

*编辑:根据习语在 2 个 ContentView 之间切换可以用完全相同的方式完成:

XAML:

<ContentPage x:Name="MyPage"></ContentPage>

代码隐藏:

if (Device.Idiom == TargetIdiom.Tablet || Device.Idiom == TargetIdiom.Desktop)
    MyPage.Content = new LargeDisplayContentView();
else
    MyPage.Content = new SmallDisplayContentView();

关于c# - 使用 xaml 在 Xamarin.Forms 中为 iPhone 和 iPad 分离 UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34715613/

相关文章:

c# - 如何使用 Selenium 日志记录功能(使用 C#)?

c# - 使用 Autofac Autowiring 通用装饰器

c# - 并行执行多对并发任务

android - 推出 SDK OTA 更新

ios - 如何在 Swift 3 的 UIWebview 中 session 过期?

c# - CORS asp.net 核心 webapi - 缺少 Access-Control-Allow-Origin header

java - 通过反射调用抽象方法

javascript - androidInterface 未定义。是什么赋予了?

ios 将字符串 "5 hours 10 min"转换为整数小时和分钟

ios - 重新加载并滚动到特定的 Collection View 单元格