Home > Back-end >  ICommand not running task
ICommand not running task

Time:02-02

I am updating a property in Viewmodel-A from Viewmodel-B while in View-B. In the same function that calls for the property update, the view is switched to View-A; The breakpoint I set on private async Task RepopulateFieldValues doesn't get hit.

So when I click on my button in View-B, I see all my relevant breakpoints get hit in Viewmodel-B, my view switches over to View-A, I see my breakpoint hit on the ICommand and thats it. Then, if I switch my view back to any other view, I see the breakpoint on my Tasks getting hit. I know I've got something backwards.

This is my property in Viewmodel-A thats updated via Viewmodel-B:


        private int _ApptSelected;
        public int ApptSelected {
            get { return _ApptSelected; }
            set {
                _ApptSelected = (MainViewModel.ApptID.Count > 0) ? MainViewModel.ApptID[0] : 0;
            }
        }

Here is my ICommand:

public ICommand RepopulateFields_Command => new AsyncRelayCommand(RepopulateFieldValues);

This is the only way I could come up with to trigger the RepopulateFields_Command:

<TextBox x:Name="HackProperty"
                 Text="{Binding ApptSelected,
                        Mode=OneWay,
                        NotifyOnSourceUpdated=True,
                        UpdateSourceTrigger=PropertyChanged}" >
            <behaviors:Interaction.Triggers>
                <behaviors:EventTrigger EventName="TextChanged">
                    <behaviors:InvokeCommandAction Command="{Binding RepopulateFields_Command}" />
                </behaviors:EventTrigger>
            </behaviors:Interaction.Triggers>
        </TextBox>

And this is the "cascading" Tasks Im trying to run:

private async Task RepopulateFieldValues()
        {
            if (ApptSelected > 0)
            {
                Console.WriteLine("ApptSelected: "   ApptSelected);
                await RetrieveEditInfo(ApptSelected);
            }
        }

This is the relevant code within Viewmodel-B (for switching views)

public void EditSelection_Clicked(object obj)
        {
            if(Messaging.AskQuestion("Do you want to edit this appointment?"))
            {
                SelectedVM = AppointmentsViewModel.ApptTabItems[1];
                AppointmentsViewModel.ApptTabItems[1].IsSelected = true;
                ApptSelectedID = ApptIDSelected.ApptID;
                Messaging.ShowAlert("ID: "   ApptSelectedID);
                MainViewModel.ApptID.Clear();
                MainViewModel.ApptID.Add(ApptSelectedID);
            }
        }

CodePudding user response:

The EventTrigger that you are using only works with routed events and TextChanged is not a routed event. This means that your command won't be invoked when you type something into the TextBox.

But if you simply want to fire-and-forget the command, you should be able to execute it directly in the setter of the ApptSelected which is bound to the TextBox:

public int ApptSelected {
    get { return _ApptSelected; }
    set {
         _ApptSelected = (MainViewModel.ApptID.Count > 0) ? MainViewModel.ApptID[0] : 0;
         _ = RepopulateFieldValues();
        }
    }

Then you don't need to bother about the TextChanged event at all.

  • Related