【问题标题】:Separate UI for iPhone and iPad in Xamarin.Forms using xaml使用 xaml 在 Xamarin.Forms 中分离 iPhone 和 iPad 的 UI
【发布时间】:2016-04-15 09:53:54
【问题描述】:

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

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

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

【问题讨论】:

    标签: c# android ios xaml xamarin.forms


    【解决方案1】:

    您可以在 xaml 中使用 Idiom 值 OnIdiom

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

    这样,您可以在手机或平板电脑的 xaml 中使用不同的值。

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

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

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

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

    XAML:

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

    代码隐藏:

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

    【讨论】:

    • 感谢您的回复!!将检查并返回。
    • @DipankarSutradhar 我使用了相同的方法,但使用了ContentView 的平板电脑和手机版本,而不是创建单独的页面。效果基本一样。
    • @hvaughan3 你能否发布一个完整的答案并解释该技术,会很有用,谢谢
    • @WickedW 我发布的代码是否存在特定问题?或者您需要关于哪一部分的更多信息?
    • @hvaughan3 你发布了任何代码吗?您提到了关于 ContentView 切换的一个可能的不错的解决方案,只是想看看您是如何做到的。
    猜你喜欢
    • 1970-01-01
    • 2014-12-02
    • 2018-06-16
    • 1970-01-01
    • 1970-01-01
    • 2019-06-04
    • 2014-12-28
    • 2016-01-20
    • 1970-01-01
    相关资源
    最近更新 更多