Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

Inheriting Android.Widget.Button. How to access custom properties

$
0
0

I'm really having a hard time with this. I'm trying to pass properties such as a string NameSearch to my MvxCommand. I've asked a question on stackoverflow, and @StuartLodge's answer was to use a custom button.

I've implemented this custom button, but I still don't know how to pass the CommandParameter and then access it in the Command.

Here's my custom button

public class FsmButton : Button
{
    public FsmButton ( Context context, IAttributeSet attributeSet ) : base( context, attributeSet )
    {
        Click += ( s, e ) =>
        {
            if ( Command == null ) return;
            if ( !Command.CanExecute( CommandParameter ) ) return;
            Command.Execute( CommandParameter );
        };
    }

    public ICommand Command { get; set; }
    public object CommandParameter { get; set; }
}

And here's my axml implementation.

<FsmButton
    android:text="Search"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/button1"
    local:MvxBind="Click FindUserCommand, CommandParameter=$NameSearch$" />

The problem that I'm currently facing is that the parameter of the Execute(object parameter) method comes back LITERALLY as $NameSearch$.

What am I doing wrong?


Also, for what it's worth, here's my Command Object in it's entirety.

public class FindUserCommand : IFindUserCommand
{
    private readonly IUserService _userService;
    private readonly IUserInteractionService _findUserInteractionService;
    private bool _isExecuting;

    // right now we're injecting the FindUserCommand into the FindUserViewModel
    // so that we can populate the object. this is obviously not desirable.
    public FindUserViewModel FindUserViewModel { get; set; }

    public FindUserCommand(IUserService userService, IUserInteractionService findUserInteractionService)
    {
        _userService = userService;
        _findUserInteractionService = findUserInteractionService;
    }

    public bool CanExecute ( object parameter )
    {
        var findUserViewModel = ( FindUserViewModel )parameter;
        return findUserViewModel.NameSearch.Length > 0 && !_isExecuting;
    }

    public void Execute ( object parameter )
    {
        _isExecuting = true;
        RaiseCanExecuteChanged();

        try
        {
            // this is the hack
            FindUserViewModel.Users = _userService.Find( FindUserViewModel.NameSearch );

            // we'd prefer
            var vm = (FindUserViewModel)parameter;
            vm.Users = _userService.Find(vm.NameSearch);
        }
        catch (Exception exception)
        {
            _findUserInteractionService.NotifyUserOfError(exception);
        }

        _isExecuting = false;
        RaiseCanExecuteChanged();
    }

    public event EventHandler CanExecuteChanged;
    public void RaiseCanExecuteChanged ()
    {
        if ( CanExecuteChanged != null )
            CanExecuteChanged( this, new EventArgs() );
    }

    public void Execute()
    {
        // Intentionally not implemented
        throw new NotImplementedException();
    }

    public bool CanExecute()
    {
        // Intentionally not implemented
        throw new NotImplementedException();
    }
}

Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>