locked
MVVM change property from Command method does not effect the UI RRS feed

  • Question

  • User211159 posted

    I can change my UI from constructure with no problem. Trying to change values from this command method fails. Does someone have a solution?

    ` public ICommand UseCurrentPositionButtonCommand { get { return new CommandHandler(sender => { UseCurrentPositionButtonCommandMethod(sender); }); } }

        private async void UseCurrentPositionButtonCommandMethod(Object Sender)
        {
            await setPlacemarkAsync();
    
            if (placemark != null)
            {
                CountryTextBoxText = placemark.CountryName;
                StreetTextBoxText = placemark.Thoroughfare;
                HouseNumberTextBoxText = placemark.SubThoroughfare;
                PostalCodeTextBoxText = placemark.PostalCode;
            }
        }`
    

    Thanks Agredo

    Thursday, October 24, 2019 3:55 PM

Answers

  • User211159 posted

    I found the error. This is a Rg.Plugins.Popup error. On my other pages changing properties from comman methods make no Problem.

    Thank you!

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, October 24, 2019 6:20 PM

All replies

  • User89714 posted

    @Agredo

    I assume that CountryTextBoxText, StreetTextBoxText, HouseNumberTextBoxText and PostalCodeTextBoxText are all string properties? Do they raise the PropertyChanged event when their values are changed?

    If not, Google INotifyPropertyChanged and PropertyChanged

    Thursday, October 24, 2019 4:23 PM
  • User211159 posted

    @JohnHardman it all works perfectly when I Change the Properties in Constructor.

    When Change the Properties in this Command Method the Property saves the value but it does not arrive the UI. Chnaging it in the Cosntructor works. I tried to Change the mode. TowWay, OneWay, OneWayFromSource

    ` private string countryTextBox;

        public string CountryTextBoxText
        {
            get { return countryTextBox; }
            set { SetProperty(ref countryTextBox, value); }
        }
    

    `

    Thursday, October 24, 2019 4:31 PM
  • User89714 posted

    @Agredo - Post the XAML/C# for the page, and the full code of the ViewModel.

    Thursday, October 24, 2019 4:58 PM
  • User211159 posted

    `

    <Grid HorizontalOptions="Center" VerticalOptions="Center" BackgroundColor="Orange">
        <ScrollView>
            <StackLayout VerticalOptions="Center" HorizontalOptions="Center" BackgroundColor="WhiteSmoke" Padding="16" WidthRequest="320">
                <StackLayout>
                    <Label Text="No store information found!" FontSize="Medium" HorizontalOptions="Center" VerticalOptions="Center"/>
                    <Label Text="Please insert this information about your store" FontSize="Body" HorizontalOptions="Center" VerticalOptions="Center"/>
                </StackLayout>
    
                <StackLayout>
                    <Label Text="{Binding CountryNameTextBlockText}"/>
                    <UserControl:TextBox Text="{Binding CountryTextBoxText, Mode=OneWay}" />
                </StackLayout>
    
                <StackLayout>
                    <Label Text="{Binding StreetNameTextBlockText}"/>
                    <StackLayout Orientation="Horizontal">
                        <UserControl:TextBox Text="{Binding StreetTextBoxText}" WidthRequest="250" HorizontalOptions="Start"/>
                        <UserControl:TextBox Text="{Binding HouseNumberTextBoxText}" HorizontalOptions="End"/>
                    </StackLayout>
                </StackLayout>
    
                <StackLayout>
                    <Label Text="{Binding PostalCodeTextBlockText}"/>
                    <UserControl:TextBox Text="{Binding PostalCodeTextBoxText}"/>
                </StackLayout>
    
                <StackLayout Orientation="Horizontal" HorizontalOptions="End">
                    <Label Text="{Binding MapVisibilityLabel}" Margin="0,5,0,0"/>
                    <Switch IsToggled="{Binding ShowMapToggleToggled}"/>
                </StackLayout>
    
                <StackLayout Orientation="Horizontal">
                    <Label Text="{Binding UseCurrentPositionTextBlockText}" Margin="0,5,0,0"/>
                    <Button Text="{Binding UseCurrentPositionButtonText}" Command="{Binding UseCurrentPositionButtonCommand}"/>
                </StackLayout>
    
                <map:Map HeightRequest="240"  IsVisible="{Binding ShowMapToggleToggled}"/>
    
                <Button Text="Submit"/>
            </StackLayout>
        </ScrollView>
    </Grid>
    

    `

    `using Retailer.Command; using System; using System.Linq; using System.Threading.Tasks; using System.Windows.Input; using Xamarin.Essentials; using Xamarin.Forms;

    namespace Retailer.ViewModels.RetailerMasterDetail.PopUp { class AddStoreInformationPopupViewModel : BaseViewModel { public INavigation Navigation { get; set; }

        public string StoreName { get; set; }
    
        public String MyProperty { get; set; }
    
        public Placemark currentPlacemark { get; set; }
    
    
        private bool showMapToggleToggled;
        public bool ShowMapToggleToggled
        {
            get { return showMapToggleToggled; }
            set { SetProperty(ref showMapToggleToggled, value); }
        }
    
        private string mapVisibilityLabel = "Show map";
        public string MapVisibilityLabel
        {
            get { return mapVisibilityLabel; }
            set { SetProperty(ref mapVisibilityLabel, value); }
        }
    
        //Country
        private string countryNameTextBlockText = "Country";
        public string CountryNameTextBlockText
        {
            get { return countryNameTextBlockText; }
            set { SetProperty(ref countryNameTextBlockText, value); }
        }
        private string countryTextBox;
        public string CountryTextBoxText
        {
            get { return countryTextBox; }
            set { SetProperty(ref countryTextBox, value); }
        }
    
        //Street
        private string streetNameTextBlockText = "Street Name";
        public string StreetNameTextBlockText
        {
            get { return streetNameTextBlockText; }
            set { SetProperty(ref streetNameTextBlockText, value); }
        }
        private string streetTextBoxText;
        public string StreetTextBoxText
        {
            get { return streetTextBoxText; }
            set { SetProperty(ref streetTextBoxText, value); }
        }
        private string houseNumberTextBoxText;
        public string HouseNumberTextBoxText
        {
            get { return houseNumberTextBoxText; }
            set { SetProperty(ref houseNumberTextBoxText, value); }
        }
    
        //Postal Code
        private string postalCodeTextBlockText = "Postal Code";
        public string PostalCodeTextBlockText
        {
            get { return postalCodeTextBlockText; }
            set { SetProperty(ref postalCodeTextBlockText, value); }
        }
        private string postalCodeTextBoxText;
        public string PostalCodeTextBoxText
        {
            get { return postalCodeTextBoxText; }
            set { SetProperty(ref postalCodeTextBoxText, value); }
        }
    
        //get position textblock
        private string useCurrentPositionTextBlockText = "Use posistion";
        public string UseCurrentPositionTextBlockText
        {
            get { return useCurrentPositionTextBlockText; }
            set { SetProperty(ref useCurrentPositionTextBlockText, value); }
        }
    
        //get position button
        private string useCurrentPositionButtonText = "Get Position";
        public string UseCurrentPositionButtonText
        {
            get { return useCurrentPositionButtonText; }
            set { SetProperty(ref useCurrentPositionButtonText, value); }
        }
    
    
        private Placemark placemark;
    
    
        /// <summary>
        /// ViewModel for AddStoreInformationPopupViewModel
        /// </summary>
        /// <param name="Navigation">
        /// The Navgation for Xamarin.Forms to navigate back to HomeDetailPage
        /// </param>
        /// <param name="placemark">
        /// The current Address data as placemark. s
        /// </param>
        public AddStoreInformationPopupViewModel(INavigation Navigation)
        {
            this.Navigation = Navigation;
    
    
            CountryTextBoxText = "Test";
            StreetTextBoxText = "Test";
            HouseNumberTextBoxText = "Test";
            PostalCodeTextBoxText = "Test";
        }
    
        private async Task setPlacemarkAsync()
        {
            var location = await Geolocation.GetLocationAsync();
    
            var placemarks = await Geocoding.GetPlacemarksAsync(location);
    
            placemark = placemarks?.FirstOrDefault();
    
    
        }
    
        public ICommand UseCurrentPositionButtonCommand
        {
            get
            {
                return new CommandHandler(() => { UseCurrentPositionButtonCommandMethod(); });
            }
        }
    
        private async void UseCurrentPositionButtonCommandMethod()
        {
            await setPlacemarkAsync();
    
            if (placemark != null)
            {
                CountryTextBoxText = placemark.CountryName;
                StreetTextBoxText = placemark.Thoroughfare;
                HouseNumberTextBoxText = placemark.SubThoroughfare;
                PostalCodeTextBoxText = placemark.PostalCode;
            }
        }
    }
    

    } `

    @JohnHardman

    Thursday, October 24, 2019 5:11 PM
  • User89714 posted

    @Agredo

    Before I spend time looking at the code, can you try binding an out-of-the-box Xamarin.Forms View (e.g. a Label) to one of the properties (e.g. CountryTextBoxText). If the Label updates as you would expect, that would suggest an issue in UserControl:TextBox . It would be good to rule that out first.

    Thanks

    Thursday, October 24, 2019 5:36 PM
  • User211159 posted

    I found the error. This is a Rg.Plugins.Popup error. On my other pages changing properties from comman methods make no Problem.

    Thank you!

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, October 24, 2019 6:20 PM
  • User89714 posted

    @Agredo said: I found the error. This is a Rg.Plugins.Popup error. On my other pages changing properties from comman methods make no Problem.

    Thank you!

    I'm curious. What did you have to change? I use Rg.Plugins.Popup but have not had any issues with bindings updating.

    Thursday, October 24, 2019 7:43 PM