【问题标题】:Xamarin.Forms Different View Per PlatformXamarin.Forms 每个平台的不同视图
【发布时间】:2018-04-12 13:10:35
【问题描述】:

所以我是 Xamarin 开发的新手,我有一个问题我无法找到答案。是否可以实现一个视图,使其在每个平台甚至一个平台上看起来都不同? IE - 该视图在 iOS 和 Android 上看起来很相似,但对于 UWP 桌面应用程序,它可能具有更多可用控件,因为屏幕空间更大。提前致谢。

【问题讨论】:

  • 看看Device类Device Class
  • 感谢您的信息。我想这正是我想要的。

标签: xamarin.forms


【解决方案1】:

当然可以。最简单的方法是在 XAML 中使用OnPlatform(请参阅here)标签

<OnPlatform x:TypeArguments="View">
    <On Platform="iOS">
        <!-- Use view for iOS here -->
    </On>
    <On Platform="Android">
        <!-- Use view for Android here -->
    </On>
    <On Platform="UWP">
        <!-- Use view for UWP here -->
    </On>
</OnPlatform>

在您的情况下(对 Android 和 iOS 使用相同的视图),您可以将其简化为

<OnPlatform x:TypeArguments="View">
    <On Platform="iOS, Android">
        <!-- Use view for iOS here -->
    </On>
    <On Platform="UWP">
        <!-- Use view for UWP here -->
    </On>
</OnPlatform>

如果你在代码中创建视图,你可以使用Device.RuntimePlatform

View CreateViewForCurrentPlatform()
{
    switch(Device.RuntimePlatform)
    {
        case Device.iOS:
        case Device.Android:
            return new View1();
        case Device.UWP:
            return new View2();
    }
    throw new Exception("Unsupported platform");
}

不管怎样,既然你这么说

[...] 对于 UWP 桌面应用,它可能具有更多可用控件,因为屏幕空间更大。

您应该考虑改用Device.Idiom(请参阅here),它可以让您区分平板电脑、台式机和手机。 Android 和 iOS 也在平板电脑上运行,如果在平板电脑上浪费屏幕空间,那就太可惜了。

【讨论】:

    猜你喜欢
    • 2020-04-30
    • 2015-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-15
    • 2014-08-30
    相关资源
    最近更新 更多