也许对你们中的一些人来说,这可能是基本问题,但我真的无法解决System.InvalidCastException:'从'System.String'到'System.TimeSpan'的无效转换。
我有两个 XAML 页面,我需要将 TimeSpan 参数从一个页面传递到另一个页面(以便我可以在另一页面上使用 TimePicker)。
第一个xaml页面:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="TestApp2.Views.DateTimePage"
xmlns:viewmodels="clr-namespace:TestApp2.ViewModels"
xmlns:local="clr-namespace:TestApp2.ViewModels"
xmlns:model="clr-namespace:TestApp2.Models"
x:DataType="viewmodels:DateTimePageViewModel"
Title="DateTimePage"
>
<ContentPage.Content>
<StackLayout>
<Label Text="Tap">
<Label.GestureRecognizers>
<TapGestureRecognizer
NumberOfTapsRequired="2"
Command="{Binding Source={RelativeSource AncestorType={x:Type local:DateTimePageViewModel}}, Path=TimePickerTapped}"
CommandParameter="{Binding .}">
</TapGestureRecognizer>
</Label.GestureRecognizers>
</Label>
<TimePicker x:Name="timeFromTimePicker" Time="{Binding Time, Mode=TwoWay}" />
</StackLayout>
</ContentPage.Content>
</ContentPage>
和
namespace TestApp2.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class DateTimePage : ContentPage
{
public DateTimePage()
{
InitializeComponent();
BindingContext = new DateTimePageViewModel();
}
}
}
第一页后面的ViewModel:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Text;
using TestApp2.Views;
using Xamarin.Forms;
namespace TestApp2.ViewModels
{
public class DateTimePageViewModel: BaseViewModel, INotifyPropertyChanged
{
public Command TimePickerTapped { get; }
private TimeSpan time;
public TimeSpan Time
{
get => time;
set
{
time = value;
OnPropertyChanged(nameof(time));
}
}
public DateTimePageViewModel()
{
TimePickerTapped = new Command(OnTimePickerSelected);
time = new TimeSpan(17, 0, 0);
}
async void OnTimePickerSelected()
{
if (time != null)
{
await Shell.Current.GoToAsync($"{nameof(TransferDateTimePage)}?" +
$"{nameof(TransferDateTimePageViewModel.Time)}={Time}");
}
}
}
}
第二个XAML页面:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="TestApp2.Views.TransferDateTimePage"
xmlns:viewmodels="clr-namespace:TestApp2.ViewModels"
xmlns:local="clr-namespace:TestApp2.ViewModels"
xmlns:model="clr-namespace:TestApp2.Models"
x:DataType="viewmodels:TransferDateTimePageViewModel"
>
<ContentPage.Content>
<StackLayout>
<TimePicker x:Name="timeFromTimePicker" Time="{Binding Time, Mode=TwoWay}" />
</StackLayout>
</ContentPage.Content>
</ContentPage>
和
namespace TestApp2.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class TransferDateTimePage : ContentPage
{
public TransferDateTimePage()
{
InitializeComponent();
BindingContext = new TransferDateTimePageViewModel();
}
}
}
第二页后面的视图模型是:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using Xamarin.Forms;
namespace TestApp2.ViewModels
{
[QueryProperty(nameof(Time), nameof(Time))]
public class TransferDateTimePageViewModel : BaseViewModel, INotifyPropertyChanged //, IQueryAttributable
{
private TimeSpan time;
public TimeSpan Time
{
get => time;
set
{
time = value;
OnPropertyChanged();
}
}
public TransferDateTimePageViewModel()
{
}
}
}
我认为该错误在某种程度上与
有关 await Shell.Current.GoToAsync($"{nameof(TransferDateTimePage)}?" +
$"{nameof(TransferDateTimePageViewModel.Time)}={Time}");
ViewModel behind first page code
并将参数传递到第二页:
[QueryProperty(nameof(Time), nameof(Time))]
我真的不知道如何解决这个问题。我尝试在互联网上广泛搜索了几天,但到目前为止我无法解决这个问题。
我需要用户能够在第二页上编辑第一页上显示的时间。
有什么帮助吗?谢谢你。
来自文档
原始数据可以作为基于字符串的查询参数传递
primitive通常意味着简单的值类型,而不是像
TimeSpan
这样的复杂对象
要传递对象,请使用此语法
var navigationParameter = new Dictionary<string, object>
{
{ "Time", myTimespanObject }
};
await Shell.Current.GoToAsync($"mypage", navigationParameter);