Xamarin.forms 在平台上评估<;T>;

Xamarin.forms 在平台上评估<;T>;,xamarin.forms,Xamarin.forms,如果我使用代码从资源字典中检索到OnPlatform的实例,如何为当前平台评估它 public static string FontName => ((OnPlatform<string>) Application.Current.Resources["Font"]) ... 公共静态字符串FontName=> ((OnPlatform)Application.Current.Resources[“Font”])。。。 您可以通过下一种方式实现: <Applica

如果我使用代码从资源字典中检索到
OnPlatform
的实例,如何为当前平台评估它

public static string FontName =>
   ((OnPlatform<string>) Application.Current.Resources["Font"]) ...
公共静态字符串FontName=>
((OnPlatform)Application.Current.Resources[“Font”])。。。

您可以通过下一种方式实现:

<Application.Resources>
    <ResourceDictionary>
        <OnPlatform x:Key="MyPadding"
         x:TypeArguments="Thickness"
            iOS="0, 20, 0, 0"
            Android="0, 0, 0, 0"/>
    </ResourceDictionary>
</Application.Resources>

var thickness = (OnPlatform<Thickness>)Application.Current.Resources["MyPadding"];
var realThickness = (Thickness)thickness;
var thickness = Application.Current.GetResourceOnPlatform<Thickness>("MyPadding");

var thickness=(OnPlatform)Application.Current.Resources[“MyPadding”];
var真实厚度=(厚度)厚度;

var thickness=(thickness)(OnPlatform)Application.Current.Resources[“MyPadding”];
或者,您可以创建一个扩展方法:

public static class ApplicationExtensions
{
    public static T GetResourceOnPlatform<T>(this Application app, string key) => (T)(OnPlatform<T>)app.Resources[key];
}
公共静态类ApplicationExtensions
{
公共静态T GetResourceOnPlatform(此应用程序应用程序,字符串键)=>(T)(OnPlatform)应用程序资源[键];
}
然后用下一种方式消费:

<Application.Resources>
    <ResourceDictionary>
        <OnPlatform x:Key="MyPadding"
         x:TypeArguments="Thickness"
            iOS="0, 20, 0, 0"
            Android="0, 0, 0, 0"/>
    </ResourceDictionary>
</Application.Resources>

var thickness = (OnPlatform<Thickness>)Application.Current.Resources["MyPadding"];
var realThickness = (Thickness)thickness;
var thickness = Application.Current.GetResourceOnPlatform<Thickness>("MyPadding");
var thickness=Application.Current.GetResourceOnPlatform(“MyPadding”);