gpt4 book ai didi

Xamarin 样式优先顺序

转载 作者:行者123 更新时间:2023-12-02 03:00:17 26 4
gpt4 key购买 nike

我正在研究 Xamarin.Forms 中的样式,并且对应用样式的顺序/规则有疑问。我正在寻找有关其工作原理的某种引用,但除了一般的“如何设置样式 [随便选择]”类型的帖子外,我没有找到太多其他信息。

具体来说,我有这两个项目:

MyProject.Shared
MyProject.Android

共享项目有我的App.xaml和主视图/ View 模型等

我可以在多个地方应用样式:

  • App.xaml 资源字典
  • MainPage.xaml“内联”(在 xaml 元素本身中,不使用本地资源)
  • MyProject.Android\Resources
  • 自定义渲染器(我还没有完全探索这些)

应用程序资源和给定 View 的资源之间的优先级似乎很简单,但是与共享库样式相比,Android 应用程序(或 iOS/Win)之间的样式如何解决?

我设置的一些样式似乎被 android 样式覆盖了,但其他样式似乎忽略了 android 样式并应用了共享样式。有时直接通过内联 xaml 设置样式属性并不像我期望的那样,有时使用应用程序级资源样式,样式中只有少数设置“采用”。

是否有关于这些不同的样式设置方法的优先顺序的明确文档,或者是否有一些正式的指导方针应该遵循以正确设置应用主题?

最佳答案

好问题 - 我正在寻找官方 Xamarin 文档和 this 的具体答案是我能找到的最接近的:

Resources can be defined in a ResourceDictionary that's attached to the Resources collection of a page or control, or to the Resources collection of the application. Choosing where to define a ResourceDictionary impacts where it can be used:

  • Resources in a ResourceDictionary defined at the control level can only be applied to the control and to its children.
  • Resources in a ResourceDictionary defined at the page level can only be applied to the page and to its children.
  • Resources in a ResourceDictionary defined at the application level can be applied throughout the application.

但这不是您问题的完整答案!这是我使用 Xamarin.Forms 的经验:

  1. App.xaml 资源字典是最通用的,将被任何内容覆盖
  2. 页面级资源字典是下一个最通用的。它将覆盖 App.xaml 中定义的任何样式
  3. 页面级内联样式将覆盖所有页面范围的样式
  4. 任何特定于平台的样式(在自定义渲染器、资源字典等中定义)将覆盖任何其他样式

我希望尽快更新此答案,并提供有关此事的官方评论的链接。我记得它在 XAM140 中被讨论过,如果你有 Xamarin 大学订阅,你可以观看

关于Xamarin 样式优先顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46532453/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com