Xamarin 3.3.0 表单错误:从 Xamarin 2.5 迁移到 3.3.0 时出现很多问题 [英] Xamarin 3.3.0 forms error: a lot of issues when migrate from Xamarin 2.5 to 3.3.0

查看:23
本文介绍了Xamarin 3.3.0 表单错误:从 Xamarin 2.5 迁移到 3.3.0 时出现很多问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题一:

我更改了旧代码(以支持 xamarin 3.0):

I changed old code (to support xamarin 3.0) from:

<OnPlatform x:Key="CrossPlatformStackOrientation" x:TypeArguments="StackOrientation">
          <On Platform="iOS" Value="Vertical"/>
          <On Platform="UWP" Value="Horizontal"/>
</OnPlatform>

<OnPlatform x:Key="CrossPlatformStackOrientation" x:TypeArguments="StackOrientation" iOS="Vertical" UWP="Horizontal" />

我遇到以下错误:

未找到UWP"的属性、可绑定属性或事件,或值和属性之间的类型不匹配.

No property, bindable property, or event found for 'UWP', or mismatching type between value and property.

如果我删除 x:TypeArguments,没有错误发生,但在运行时,我得到了

If I remove x:TypeArguments, no error occur but on runtime, I got

无法确定要为其提供值的属性

Cannot determine property to provide the value for

问题二:

我收到错误

预期的}"

为了

<Label Text="{Binding Description.CreationDateTime,Converter={StaticResource StringFormatConverter}, ConverterParameter='{0:dd-M-yyyy HH:mm:ss}'}"
                     Style="{StaticResource MyResourceText}" Grid.Row="0" Grid.Column="3" Margin="0,3,5,0"/>

问题三:

为了

<GridLength x:Key="TileSeparatorHeight">0</GridLength>

我得到了:

'GridLength' 类型不支持直接内容.

The type 'GridLength' does not support direct content.

它也会发生在厚度上:

<Thickness x:Key="TileStartDatePadding">0,0,0,0</Thickness>

如何解决它们?

推荐答案

问题 1,3 的解决方法:

在您的应用中定义类:

public class XamlConsts
{
    public readonly Thickness TileStartDatePadding = new Thickness(0);
    public readonly GridLength TileSeparatorHeight = new GridLength(0);
    public StackOrientation CrossPlatformStackOrientation
    {
        get
        {
            switch (Device.RuntimePlatform)
            {
                case Device.UWP:
                    return StackOrientation.Horizontal;
                case Device.iOS:
                    return StackOrientation.Vertical;
                default: return StackOrientation.Horizontal;
            }
        }
    }

}

将其添加到资源:

<local:XamlConsts x:Key="XamlConsts"></local:XamlConsts>

使用它:

<StackLayout Padding="{Binding Source={StaticResource XamlConsts},Path=TileStartDatePadding}"  Orientation="{Binding Source={StaticResource XamlConsts},Path=CrossPlatformStackOrientation}">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="{Binding Source={StaticResource XamlConsts},Path=TileSeparatorHeight}"/>
            </Grid.RowDefinitions>

问题一

此方法不再有效,因为 OnPlatform 从使用它的属性推断类型,当您在资源中声明它时,它无法确定类型.

This approach is no longer valid since OnPlatform infer types from properties in which it is used, when you declare it in resources it cannot determine type.

您可以使用替代方法:1. 定义转换器:

You can use alternative approach: 1. Define Converter:

public class OrientationConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            switch (Device.RuntimePlatform)
            {
                case Device.UWP:
                    return StackOrientation.Horizontal;
                case Device.iOS:
                    return StackOrientation.Vertical;
                default: return StackOrientation.Horizontal;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

如何使用这个转换器:

<local:OrientationConverter x:Key="CrossPlatformStackOrientationConverter"/>
<x:Int32 x:Key="intConst">1</x:Int32>
       </ContentPage.Resources>
    <StackLayout Orientation="{Binding Source={StaticResource intConst}, Converter={StaticResource CrossPlatformStackOrientationConverter}}">

其中1"只是任何 int 常量,没有任何意义.

Where "1" is just any int constant and does not have any sense.

问题二

这是一个错误.

解决方法:

在资源中定义常量:

<x:String x:Key="dateFormat">{0:dd-M-yyyy HH:mm:ss}</x:String>

使用它:

Text="{Binding Description.CreationDateTime,Converter={StaticResource StringFormatConverter}, ConverterParameter={StaticResource dateFormat}}"

问题三

看起来像 xamarin 错误.您可以按照我在第一期中的描述创建转换器,但它应该是开箱即用的.

Seems like xamarin bug. You can create converter as I described in Issue one, but it should work out of the box.

这篇关于Xamarin 3.3.0 表单错误:从 Xamarin 2.5 迁移到 3.3.0 时出现很多问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆