0

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

在内容页面中:

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

        }
    );

谢谢

4

2 回答 2

1

只需将 绑定CommandParameter到与Label

Command="{Binding InputPopup}"
CommandParameter="{Binding Level}"  />
于 2020-08-08T20:49:00.013 回答
0

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

命令(在 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>
于 2020-08-09T20:25:28.803 回答