Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xamarin.forms Xamarin:字符串中的日期_Xamarin.forms - Fatal编程技术网

Xamarin.forms Xamarin:字符串中的日期

Xamarin.forms Xamarin:字符串中的日期,xamarin.forms,Xamarin.forms,我是xamarin的新手,我在xamarin表单项目中面临一个问题。 我在listview viewcell中有一个标签,用于在UI中显示时间。该日期以数字形式接收,如1510822596449(Java时间戳)。我想以字符串形式显示日期,如“n天前”。如何实现这一点 <StackLayout> <ListView> <ListView.ItemTemplate> <Data

我是xamarin的新手,我在xamarin表单项目中面临一个问题。 我在listview viewcell中有一个标签,用于在UI中显示时间。该日期以数字形式接收,如1510822596449(Java时间戳)。我想以字符串形式显示日期,如“n天前”。如何实现这一点

    <StackLayout>
        <ListView>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                    <StackLayout>
                        <Label Text="{Binding createdTime}"/>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
          </ListView>           
    </StackLayout>

任何人请建议一个解决方案与工作代码。
提前谢谢

正如评论中所建议的,您可以使用

在共享代码中编写类似的转换器

    public class TicksToDateTimeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (!Int64.TryParse(value, out long ticks))
                return DatTime.Now;

            // TODO you can do a ToString and format it you want here but also in XAML
            return new DateTime(ticks);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            // Not needed
            throw new NotImplementedException();
        }
    }
现在在您的XAML中,在页面的根目录下像这样声明te value CONVERT,我假设它是一个
ContentPage

<ContentPage.Resources>
    <ResourceDictionary>
        <local:TicksToDateTimeConverter x:Key="TicksConverter" />
    </ResourceDictionary>
</ContentPage.Resources>
根据您是否从转换器返回字符串或
日期时间
,在后一种情况下,您也可以在这里用XAML对其进行格式化,如下所示:

<Label Text="{Binding createdTime, Converter={StaticResource TicksConverter}, StringFormat='{0:dd-MM-yyyy}'}"/>


或者,您可以选择完全不同的方式,并按照注释中的建议,转换绑定到
ViewCell

的模型内的值,您可以使用

在共享代码中编写类似的转换器

    public class TicksToDateTimeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (!Int64.TryParse(value, out long ticks))
                return DatTime.Now;

            // TODO you can do a ToString and format it you want here but also in XAML
            return new DateTime(ticks);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            // Not needed
            throw new NotImplementedException();
        }
    }
现在在您的XAML中,在页面的根目录下像这样声明te value CONVERT,我假设它是一个
ContentPage

<ContentPage.Resources>
    <ResourceDictionary>
        <local:TicksToDateTimeConverter x:Key="TicksConverter" />
    </ResourceDictionary>
</ContentPage.Resources>
根据您是否从转换器返回字符串或
日期时间
,在后一种情况下,您也可以在这里用XAML对其进行格式化,如下所示:

<Label Text="{Binding createdTime, Converter={StaticResource TicksConverter}, StringFormat='{0:dd-MM-yyyy}'}"/>


或者,您可以选择完全不同的方式,并转换绑定到
ViewCell
的模型内的值。首先,创建一个类DateTimeToString Converter并添加以下代码:

   public class DatetimeToStringConverter : IValueConverter
      {
    #region IValueConverter implementation

     public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value == null)
            return string.Empty;

       return new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)
            .AddMilliseconds((long)value) // put your value here
             .ToLocalTime().ToString("g");
    }

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

    #endregion
}
然后在xaml中,在页面的根目录下添加以下代码:

    <ContentPage.Resources>
        <ResourceDictionary>
            <local:DatetimeToStringConverter x:Key="cnvDateTimeConverter"></local:DatetimeToStringConverter>
        </ResourceDictionary>
    </ContentPage.Resources>
然后更改标签文本,如下所示:

Text="{Binding createdTime,  Converter={StaticResource cnvDateTimeConverter}}"

在模型中保持createTime类型的长度不变,否则会出现无效的强制转换异常

首先,创建一个类DatetimeToStringConverter并添加以下代码:

   public class DatetimeToStringConverter : IValueConverter
      {
    #region IValueConverter implementation

     public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value == null)
            return string.Empty;

       return new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)
            .AddMilliseconds((long)value) // put your value here
             .ToLocalTime().ToString("g");
    }

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

    #endregion
}
然后在xaml中,在页面的根目录下添加以下代码:

    <ContentPage.Resources>
        <ResourceDictionary>
            <local:DatetimeToStringConverter x:Key="cnvDateTimeConverter"></local:DatetimeToStringConverter>
        </ResourceDictionary>
    </ContentPage.Resources>
然后更改标签文本,如下所示:

Text="{Binding createdTime,  Converter={StaticResource cnvDateTimeConverter}}"

在模型中保持createTime类型的长度不变,否则会出现无效的强制转换异常

您也可以使用值转换器来实现这一点。从滴答声(我假设它们是滴答声)转换到日期时间,然后格式化它是一个简单的谷歌“滴答声到格式化的日期时间”,你也可以用一个值转换器来实现这一点。将刻度(我假设它们是刻度)转换为日期时间,然后格式化它是一个简单的谷歌“刻度到格式化的日期时间”,添加var ticks=value作为long;显示错误:如果(ticks==null)返回,则as运算符必须与引用类型或可空类型一起使用(“long”是不可空的值类型);显示了另一个错误:需要一个类型为可转换为object的对象。你说得对,这是我从头开始写的。让我来解决这个问题。不要粗鲁,但请至少试着理解,而不仅仅是希望在StackOverflow上复制/粘贴答案。在页面的根目录中,您将找到更多的xmlns声明,只需将其添加到那里,并将转换器类的
YourApp.Namespace
替换为
Namespace
之后的内容。例如,如果您的转换器类如下所示:
namespace MyApplication.Converters{public class TicksToDateTimeConverter{…}
add
xmlns:local=“clr namespace:MyApplication.Converters”
作为页面的一个属性。@GeraldVersluis我对使用值转换器做出了最初的评论,因为我已经在另一个问题中介绍了OP。他似乎只想要代码,并不试图理解;显示错误:如果(ticks==null)返回,则as运算符必须与引用类型或可空类型一起使用(“long”是不可空的值类型);显示了另一个错误:需要一个类型为可转换为object的对象。你说得对,这是我从头开始写的。让我来解决这个问题。不要粗鲁,但请至少试着理解,而不仅仅是希望在StackOverflow上复制/粘贴答案。在页面的根目录中,您将找到更多的xmlns声明,只需将其添加到那里,并将转换器类的
YourApp.Namespace
替换为
Namespace
之后的内容。例如,如果您的转换器类如下所示:
namespace MyApplication.Converters{public class TicksToDateTimeConverter{…}
add
xmlns:local=“clr namespace:MyApplication.Converters”
作为页面的一个属性。@GeraldVersluis我对使用值转换器做出了最初的评论,因为我已经在另一个问题中介绍了OP。他似乎只想要代码,不想去理解。它在工作,但像2009年9月28日4:20,有没有转换成n天前的代码?它在工作,但像2009年9月28日4:20,有没有转换成n天前的代码