C# 将参数从代码传递到命令
我想使用命令将一些数据传递给服务,但我不想在代码中使用它。这就是我尝试过的: view.xaml.csC# 将参数从代码传递到命令,c#,xamarin,data-binding,xamarin.forms,xamarin.ios,C#,Xamarin,Data Binding,Xamarin.forms,Xamarin.ios,我想使用命令将一些数据传递给服务,但我不想在代码中使用它。这就是我尝试过的: view.xaml.cs ViewModel.FirstVm vm1; public ViewPage(Models.Example example) { InitializeComponent(); NavigationPage.SetTitleIcon(this, "logosmall.png"); Tasks ts = new Tasks()
ViewModel.FirstVm vm1;
public ViewPage(Models.Example example)
{
InitializeComponent();
NavigationPage.SetTitleIcon(this, "logosmall.png");
Tasks ts = new Tasks();
var myLocation = Task.Run(() =>
ts.GetDeviceCurrentLocation()).Result;
var latitudeIm = myLocation.Latitude;
var longitudeIm = myLocation.Longitude;
vm1 = new FirstVm(latitude, longitude, latitudeIm, longitudeIm);
routeInfo.Command = vm1.GetInfoCommand;
}
视图模型
public FirstVm(double latitude, double longitude, double latitudeIm, double longitudeIm)
{
this.latitude = latitude;
this.longitude = longitude;
this.latitudeIm = latitudeIm;
this.longitudeIm = longitudeIm;
}
public double latitude;
public double longitude;
public double latitudeIm;
public double longitudeIm;
public ICommand GetInfoCommand
{
get
{
return new Command(async () =>
{
// how to pass the parameters here
Routes = await _ts.GetRouteInfosAsync(latitude, longitude,latitudeIm,longitudeIm);
await Application.Current.MainPage.Navigation.PushAsync(new View(Routes));
});
}
}
tasks.cs
public async Task<List<Route>> GetRouteInfosAsync(double la, double lo, double laIm, double loIm)
{
var client = new HttpClient();
var json = await client.GetStringAsync("https://maps.googleapis.com/maps/api/directions/json?origin=31.320475 19.824573&destination=31.33765 11.87052&key=my_keyy");
var routes = JsonConvert.DeserializeObject<RootObject>(json);
return routes.routes;
}
公共异步任务GetRouteInfo同步(双la、双lo、双laIm、双loIm)
{
var client=新的HttpClient();
var json=await client.GetStringAsync(“https://maps.googleapis.com/maps/api/directions/json?origin=31.320475 19.824573&destination=31.33765 11.87052&key=my_-key“;
var routes=JsonConvert.DeserializeObject(json);
返回路线;
}
我的方法对吗?!我做错什么了吗?谢谢:)您可以在页面的构造函数中编写
BindingContext=YourViewModel()
:
ViewModel.FirstVm vm1;
public ViewPage(Models.Example example)
{
InitializeComponent();`
BindingContex = new YourViewModel(parameters)
NavigationPage.SetTitleIcon(this, "logosmall.png");
Tasks ts = new Tasks();
var myLocation = Task.Run(() =>
ts.GetDeviceCurrentLocation()).Result;
var latitudeIm = myLocation.Latitude;
var longitudeIm = myLocation.Longitude;
routeInfo.Command = vm1.GetInfoCommand;
routeInfo.CommandParameter = latitudeIm;
routeInfo.CommandParameter = longitudeIm;
routeInfo.CommandParameter = latitude;
routeInfo.CommandParameter = longitude;
}
然后在视图模型中使用此参数创建构造函数并在命令中使用它您可以在页面的构造函数中编写
BindingContext=YourViewModel()
:
ViewModel.FirstVm vm1;
public ViewPage(Models.Example example)
{
InitializeComponent();`
BindingContex = new YourViewModel(parameters)
NavigationPage.SetTitleIcon(this, "logosmall.png");
Tasks ts = new Tasks();
var myLocation = Task.Run(() =>
ts.GetDeviceCurrentLocation()).Result;
var latitudeIm = myLocation.Latitude;
var longitudeIm = myLocation.Longitude;
routeInfo.Command = vm1.GetInfoCommand;
routeInfo.CommandParameter = latitudeIm;
routeInfo.CommandParameter = longitudeIm;
routeInfo.CommandParameter = latitude;
routeInfo.CommandParameter = longitude;
}
然后在视图模型中使用此参数创建构造函数,并在命令中使用它解决方案1:
您是否在构造方法FirstVm()
中设置了值?如果是这样,请先初始化vm,然后再使用它。我想你的构想应该是:
public FirstVm(double la, double lo, double laIm, double loIm)
{
latitude = la;
longitude = lo;
latitudeIm = laIm;
longitudeIm = loIm;
}
在ViewPage中,您应该首先创建vm,然后使用它:
vm1 = new FirstVm(latitude, longitude, latitudeIm, longitudeIm);
routeInfo.Command = vm1.GetInfoCommand;
解决方案2:
也可以使用以下参数定义命令:
public ICommand GetInfoCommand
{
get
{
return new Command<List<double>>(async (List<double> list) =>
{
Routes = await _ts.GetRouteInfosAsync(list[0], list[1], list[2], list[3]);
await Application.Current.MainPage.Navigation.PushAsync(new View(Routes));
});
}
}
//use this command in ViewPage
routeInfo.CommandParameter = new List<double> { latitude, longitude, latitudeIm, longitudeIm };
public ICommand GetInfoCommand
{
得到
{
返回新命令(异步(列表)=>
{
Routes=wait_ts.getRouteInfo同步(列表[0],列表[1],列表[2],列表[3]);
等待Application.Current.MainPage.Navigation.PushAsync(新视图(路由));
});
}
}
//在ViewPage中使用此命令
routeInfo.CommandParameter=新列表{纬度、经度、纬度、经度};
请注意,我们应该先创建实例,然后再使用它。解决方案1:
您是否在构造方法FirstVm()
中设置了值?如果是这样,请先初始化vm,然后再使用它。我想你的构想应该是:
public FirstVm(double la, double lo, double laIm, double loIm)
{
latitude = la;
longitude = lo;
latitudeIm = laIm;
longitudeIm = loIm;
}
在ViewPage中,您应该首先创建vm,然后使用它:
vm1 = new FirstVm(latitude, longitude, latitudeIm, longitudeIm);
routeInfo.Command = vm1.GetInfoCommand;
解决方案2:
也可以使用以下参数定义命令:
public ICommand GetInfoCommand
{
get
{
return new Command<List<double>>(async (List<double> list) =>
{
Routes = await _ts.GetRouteInfosAsync(list[0], list[1], list[2], list[3]);
await Application.Current.MainPage.Navigation.PushAsync(new View(Routes));
});
}
}
//use this command in ViewPage
routeInfo.CommandParameter = new List<double> { latitude, longitude, latitudeIm, longitudeIm };
public ICommand GetInfoCommand
{
得到
{
返回新命令(异步(列表)=>
{
Routes=wait_ts.getRouteInfo同步(列表[0],列表[1],列表[2],列表[3]);
等待Application.Current.MainPage.Navigation.PushAsync(新视图(路由));
});
}
}
//在ViewPage中使用此命令
routeInfo.CommandParameter=新列表{纬度、经度、纬度、经度};
请注意,我们应该先创建实例,然后再使用它。这段代码会遇到什么问题?如果您想查看您的代码,请将其发布到,您不能为CommandParameter指定4个不同的值。如果需要多个值,请将它们组合成一个对象并传递该值这段代码面临什么问题?如果您想查看您的代码,请将其发布到,您不能为CommandParameter指定4个不同的值。如果需要多个值,请将它们组合成一个对象并传递该值。我尝试使用您所说的内容,但tasks.cs中的参数始终为0。请你看一下这个问题,因为我编辑了它@但是你必须像我一样设置绑定上下文。您必须在课堂上完成此操作,而不是xamlI尝试使用您所说的内容,但tasks.cs中的参数始终为0。请你看一下这个问题,因为我编辑了它@但是你必须像我一样设置绑定上下文。你必须在课堂上做这件事,而不是xaml