【问题标题】:Unable to read double from application resource within the viewmodel class无法从视图模型类中的应用程序资源中读取双精度
【发布时间】:2020-01-23 14:16:22
【问题描述】:

我在 App.xaml 中定义了一个试图从视图模型类中读取的资源。

尝试过的不同版本

<OnPlatform x:Key="HandpickedPhone"
                    x:TypeArguments="x:Double"
                    Android="250"
                    iOS="240" />
        <OnPlatform x:Key="HandpickedTablet"
                    x:TypeArguments="x:Double"
                    Android="375"
                    iOS="360" />
        <OnIdiom x:Key="HandpickedHeight"
                 x:TypeArguments="x:Double"
                 Phone="{StaticResource HandpickedPhone}"
                 Tablet="{StaticResource HandpickedTablet}" />

<OnPlatform x:Key="HorizontalListHeight"
                    x:TypeArguments="x:Double">
            <OnPlatform.Android>
                <OnIdiom x:TypeArguments="x:Double"
                         Phone="250"
                         Tablet="375" />
            </OnPlatform.Android>
            <OnPlatform.iOS>
                <OnIdiom x:TypeArguments="x:Double"
                         Phone="240"
                         Tablet="260" />
            </OnPlatform.iOS>
        </OnPlatform>

我正在尝试阅读它并动态设置高度请求,

double height = (double)App.Current.Resources["HorizontalListHeight"];
                        horizontalStack.HeightRequest = height;

但是转换为 double 会引发 IllegalCastException,同时,我能够从 VS 中的观察者转换为正确的值。

【问题讨论】:

  • 试试 Double.Parse()
  • 直接在你的xaml代码中使用
  • @HamidShaikh 不能在 xaml 中动态使用我必须设置它,这取决于场景
  • 在 Xaml 中,您也可以使用触发器动态执行此操作.... ......

标签: xamarin xamarin.forms resourcedictionary idioms


【解决方案1】:

您必须在 x:TypeArguments 中使用宽度或高度或厚度 像这样:-

       <OnPlatform x:TypeArguments="Thickness">
            <OnPlatform.Platforms>
                <On Platform="iOS" Value="0, 20, 0, 0" />
                <On Platform="Android" Value="0, 0, 0, 0" />
                <On Platform="UWP" Value="0, 0, 0, 0" />
            </OnPlatform.Platforms>
        </OnPlatform>

【讨论】:

  • 所以“x:Double”不是有效的 TypeArgument。 HeightRequest 需要一个 double 因此我给 Double 作为参数。会检查身高。我的要求是对不同的成语也有不同的价值。
  • 无法找到类型参数高度
【解决方案2】:

首先,检查https://forums.xamarin.com/discussion/comment/354754/#Comment_354754

这里height不是double,而是OnPlatform&lt;double&gt;,所以修改你的代码为

 var height = (OnPlatform<double>)App.Current.Resources["HorizontalListHeight"];
 horizontalStack.HeightRequest = height;

但是,这不是推荐的方式,我们经常在 ResourceDictionaty 中使用Style

       <Style x:Name="HorizontalListHeight" TargetType="StackLayout">
            <Setter Property="HeightRequest">
                <Setter.Value>
                    <OnPlatform  x:TypeArguments="x:Double">
                        <OnPlatform.Android>
                            <OnIdiom x:TypeArguments="x:Double"  Phone="250" Tablet="375" />
                        </OnPlatform.Android>
                        <OnPlatform.iOS>
                            <OnIdiom x:TypeArguments="x:Double" Phone="240" Tablet="260" />
                        </OnPlatform.iOS>
                    </OnPlatform>
                </Setter.Value>
            </Setter>
        </Style>

horizontalStack.Style = Application.Current.Resources["HorizontalListHeight"] as Style;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多