如何通过命令传递绑定 属性 来修改它

How to pass the bound property with command to modify it

我有一个包含多个标签的页面,每个标签都绑定了不同的 属性,当点击时打开提示以修改它们的值。我不知道如何用命令传递 属性,所以我可以修改它并对所有标签使用相同的命令。

在内容页面中:

                <Label x:Name="lblLevel" Text="{Binding Level}" FontSize="Large">
                    <Label.GestureRecognizers>
                        <TapGestureRecognizer
                            Command="{Binding InputPopup}"
                            CommandParameter="{Binding Source={x:Reference lblLevel}, Path=Text}" />
                    </Label.GestureRecognizers>
                </Label> 

命令:

    public ICommand InputPopup => new Command(
        async () => {
            PromptResult pResult = await UserDialogs.Instance.PromptAsync(new PromptConfig
            {
                InputType = InputType.Name,
                OkText = "Confirm",
                Title = "New value",
                CancelText = "Cancel",
                MaxLength = 1,
            });
            if (pResult.Ok && !string.IsNullOrWhiteSpace(pResult.Text))
            {
//Todo: PropertyX (Level) = pResult.Text
            }

        }
    );

谢谢

只需将 CommandParameter 绑定到与 Label

相同的 属性
Command="{Binding InputPopup}"
CommandParameter="{Binding Level}"  />

所以在尝试了一些我无法开始工作的不同事情之后,我最终使用了一个开关,它不是很漂亮,但至少它可以工作。

命令(在 FreshMVVM ViewModel 中):

public ICommand EditValuePopupCommand => new Command<string>(
    async (param) =>
    {
        PromptResult pResult = await UserDialogs.Instance.PromptAsync(new PromptConfig
        {
            InputType = InputType.Name,
            OkText = "Confirm",
            Title = $"Edit {param}",
            CancelText = "Cancel",
            MaxLength = 2,
        });
        if (pResult.Ok && !string.IsNullOrWhiteSpace(pResult.Text))
        {
            switch (param)
            {
                case "Level": Level = int.Parse(pResult.Text); break;
                case "Strength": Strength = "Str: " + pResult.Text; break;
                case "Dexterity": Dexterity = "Dex: " + pResult.Text; break;
                case "Constitution": Constitution = "Con: " + pResult.Text; break;
                case "Wisdom": Wisdom = "Wis: " + pResult.Text; break;
                case "Intelligence": Intelligence = "Int: " + pResult.Text; break;
                case "Charisma": Charisma = "Cha: " + pResult.Text; break;
                case "AttackModifier": AttackModifier = "Attack: " + pResult.Text; break;
                case "SpellAttackModifier": SpellAttackModifier = "Spell Attack: " + pResult.Text; break;
                case "SecondaryAttackModifier": SecondaryAttackModifier = "Second Attack: " + pResult.Text; break;
                case "SaveDC": SaveDC = "Second Attack: " + pResult.Text; break;

                default:
                    Console.WriteLine("Default case");
                    break;
            }
        }
    }
);

XAML:

<StackLayout Orientation="Horizontal" HorizontalOptions="CenterAndExpand">
                    <Frame Style="{StaticResource DefaultFrameStyle}">
                        <Label x:Name="lblStrength" Text="{Binding Strength}">
                            <Label.GestureRecognizers>
                                <TapGestureRecognizer
                                    Command="{Binding EditValuePopupCommand}"
                                    CommandParameter="Strength" />
                            </Label.GestureRecognizers>
                        </Label>
                    </Frame>

                    <Frame Style="{StaticResource DefaultFrameStyle}">
                        <Label x:Name="lblDexterity" Text="{Binding Dexterity}">
                            <Label.GestureRecognizers>
                                <TapGestureRecognizer
                                    Command="{Binding EditValuePopupCommand}"
                                    CommandParameter="Dexterity" />
                            </Label.GestureRecognizers>
                        </Label>
                    </Frame>