2

Thanks Advance. I learning to dev a uwp program. I get those code in internet

I have a login view:

    <TextBox x:Name="txtUserID" Grid.Row="0" Grid.Column="1" Width="150" Text="{x:Bind VM.UserId,Mode=TwoWay}" />
    <TextBlock x:Name="txbUserName" Grid.Row="1" Grid.Column="1" Width="150" Text="{x:Bind VM.UserName,Mode=OneWay}"  />
    <Button x:Name="btnLogin" Content="{x:Bind VM.log, Mode=OneWay}" Grid.Row="2" Grid.ColumnSpan="2" Width="150" HorizontalAlignment="Center"
            Command="{x:Bind VM.LoginCommand,Mode=OneWay}"
            CommandParameter="{Binding Text,ElementName=txtUserID,Mode=OneWay}"
            />

Then I have a command for button:

public DelegateCommand<string> LoginCommand
    {
        get
        {
            return _loginCommand
                ?? (_loginCommand = new DelegateCommand<string>(
                    s =>
                    {
                        UserName = s;
                        log = "new";
                    },
                    s => !string.IsNullOrEmpty(s)
                    ));
        }
    }

Of course, this program works fine, but I don’t quite understand it.

Except for S, all variables are declared, but S does not.

But the strange thing is that S does not declare, but it can correctly get the parameters carried by the button.

And I renamed S and the effect is still in effect.

I don't quite understand this, I hope someone can help me.

haihan jin
  • 125
  • 8
  • 1
    `s` is a parameter to your delegate, its type is inferred by the requirements for the delegate, most likely `string` since this is what `DelegateCommand` probably requires. Its value will be provided by the caller when your delegate is actually called. – Lasse V. Karlsen Aug 28 '18 at 10:37
  • 1
    You can provide the type-parameter, but usually it´s implicitely infered from the context and thus can be omiited. In your case it´s pretty sure a `string`. – MakePeaceGreatAgain Aug 28 '18 at 10:37
  • 2
    [Lambda Expressions (C# Programming Guide)](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/statements-expressions-operators/lambda-expressions) – TheGeneral Aug 28 '18 at 10:38
  • Thanks everyone and Now I get it...I guess..@LasseVågsætherKarlsen @HimBromBeere I can think that when I click on the button, I call a function similar to LoginCommand(String s), and s as an implicit object? And this s can be called s, it can also be called other names. – haihan jin Aug 28 '18 at 11:04
  • Yeap, it´s just the name of the parameter for the anoynmous method behind `=>`. So in your case that method expects a `string` and doesn´t return anything. How you call that argument is up to you. – MakePeaceGreatAgain Aug 28 '18 at 11:23

0 Answers0