如何将值从 PopupWindow 传递到我的 "PageViewModel" 并更新 CustomPin 属性

How to pass values from a PopupWindow to my "PageViewModel" and update CustomPin properties

我正在使用 C# 学习 Xamarin Forms,我下载了 TK.CustomMap 示例,我想使用新功能对其进行扩展。其中之一是在地图上拖动一个 CustomPin,在拖动结束时我打开一个 PopupWindow,用户可以在其中选择一个 Date 和一个 Picker 项目。使用这些值,我想更新 CustomPin 属性,正如您在我在下面放置的 C# 方法中插入的 PseudoCode 中看到的那样。我的难点在于该示例基于 "Command" 操作,因此经过几天搜索后我没有找到任何解决方案。谢谢

这里是弹窗WindowXaml

<?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>

这是我的 Popup Window 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);
                 });
           }
        }

弹出 Window C# 已按照建议修改。在 button_Onclicked 我调用了两次,因为我有 2 个参数。这是它的代码

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();
        }
    }
}