如何将值从弹出窗口传递到我的"PageViewModel"并更新自定义 Pin 属性



我正在用C#学习Xamarin Forms,我下载了TK.CustomMap示例,我想用新功能扩展它。其中之一是在地图上拖动CustomPin,在拖动结束时,我打开一个弹出窗口,用户可以在其中选择日期和选取器项。有了这些值,我想更新CustomPin属性,正如你在下面的C#方法中插入的PseudoCode中看到的那样。我的困难在于,这个例子是基于"命令"操作的,所以经过几天的搜索,我没有找到任何解决方案。Thks

这里的弹出窗口Xaml

<?xml version="1.0" encoding="UTF-8" ?>
<pages:PopupPage x:Name="popupage"
x:Class="WorkingWithMaps.PopupNewTaskView"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:animations="clr-namespace:Rg.Plugins.Popup.Animations;assembly=Rg.Plugins.Popup"
xmlns:pages="clr-namespace:Rg.Plugins.Popup.Pages;assembly=Rg.Plugins.Popup">
<pages:PopupPage.Animation>
xmlns:pages="clr-namespace:Rg.Plugins.Popup.Pages;assembly=Rg.Plugins.Popup">
<pages:PopupPage.Animation>
<animations:ScaleAnimation></animations:ScaleAnimation>
</pages:PopupPage.Animation>
<StackLayout
Margin="12" Padding="24" BackgroundColor="WhiteSmoke" HorizontalOptions="Center" VerticalOptions="Center">
<StackLayout>
<Label Text="what's up?" />
<Entry
x:Name="TaskEntry"
Placeholder="write them here"
TextChanged="TaskEntry_OnTextChanged" />
</StackLayout>
<StackLayout>
<Label Text="Introduza uma data correcta " />
<DatePicker x:Name="dtevento" />
<Picker x:Name="local"
Title="Width"
Grid.Row="0"
Grid.Column="1">
<Picker.Items>
<x:String>Casa</x:String>
<x:String>Farmacia</x:String>
<x:String>MerceMercado</x:String>
<x:String>Talho</x:String>
<x:String>Banco</x:String>
<x:String>Padaria</x:String>
<x:String>Outra</x:String>
</Picker.Items>
<Picker.SelectedIndex>
0
</Picker.SelectedIndex>
</Picker>
</StackLayout>
<Button
x:Name="TaskButton" BackgroundColor="Crimson" Clicked="Button_OnClicked" CornerRadius="10"  FontSize="Large"
IsEnabled="False" Text="Add this task now"  TextColor="White" />
</StackLayout>
</pages:PopupPage>

这是我的弹出窗口c#

using Rg.Plugins.Popup.Services;
using System;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace WorkingWithMaps
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class PopupNewTaskView
{
public PopupNewTaskView()
{
InitializeComponent();
}
private void TaskEntry_OnTextChanged(object sender, TextChangedEventArgs e)
{
if (!string.IsNullOrEmpty(TaskEntry.Text))
TaskButton.IsEnabled = true;
else if (string.IsNullOrEmpty(TaskEntry.Text))
TaskButton.IsEnabled = false;
}
private async void Button_OnClicked(object sender, EventArgs e)
{
await PopupNavigation.PopAsync();
}
}
}

最后是我想在C#中实现的方法(有2行伪代码(,其中在PopupWindow 中使用DatePicker"dtevento"和Picker"local">

public Command<TKCustomMapPin> PinNovaCommand
{
get
{
return new Command<TKCustomMapPin>(async (TKCustomMapPin pin) =>
{
pin.Title = string.Format("Pin {0}, {1}", pin.Position.Latitude, pin.Position.Longitude);
///*****************************************************************************************************
// open (PopupNewTaskView) and get values to update the pin properties
|
//   need something here                     V
await PopupNavigation.PushAsync(new PopupNewTaskView());
pin.DataEvento = dtevento.SelectedDate; // pseudocode
pin.Subtitle = local.selectedItem; // pseudocode    
*********************************************************************************************************************************

});
}
}

我在Leon提到的线程中按照建议(而不是信使(使用了它,并且正在工作。无论如何,我正在使用计数器来获取参数。不确定这是否是的最佳方式

public Command<TKCustomMapPin> PinNovaCommand
{
get
{
return new Command<TKCustomMapPin>(async (TKCustomMapPin pin) =>
{
pin.Title = string.Format("Pin {0}, {1}", pin.Position.Latitude, pin.Position.Longitude);
///********************************************************************************************************************************
string param = String.Empty;
int conta = 0;
var page = new PopupNewTaskView();
page.Action += async (sender, stringparameter) =>
{
param = stringparameter; // Here you can get your data from popup, if you'll use this data for run some 
if (conta == 0)
{
pin.Subtitle = param;
}
else
if(conta == 1)
{
pin.DataEvento = param;
}
conta++;
// here you can handle the parameter and do your stuff
};
await PopupNavigation.Instance.PushAsync(page);
});
}
}

弹出窗口C#按照建议进行了修改。在button_Inclicked,我调用了两次,因为我有两个参数。这里是它的代码

using Rg.Plugins.Popup.Services;
using System;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace WorkingWithMaps
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class PopupNewTaskView
{
public PopupNewTaskView()
{
InitializeComponent();
}
private void TaskEntry_OnTextChanged(object sender, TextChangedEventArgs e)
{
if (!string.IsNullOrEmpty(TaskEntry.Text))
TaskButton.IsEnabled = true;
else if (string.IsNullOrEmpty(TaskEntry.Text))
TaskButton.IsEnabled = false;
}
public EventHandler<string> Action;  //you can change "string" to any parameter you want to pass back.
private async void Button_OnClicked(object sender, EventArgs e)
{
string parameter = local.SelectedItem.ToString();
string parameter2 = dtevento.Date.ToString();
Action?.Invoke(this, parameter); // don't forget to invoke the method before close the popup. (only invoke when you want to pass the value back).
Action?.Invoke(this, parameter2);
await PopupNavigation.PopAsync();
}
}
}

相关内容

  • 没有找到相关文章

最新更新