Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

Slider Empty String Value

$
0
0

I have a slider that I am trying to bind to my viewmodel and submit to an API.
I can see the Value updating in the display label but when I submit the page the slider comes back
as an empty string. My setup is slightly different than the slider demos.

ViewModel Code

    public class CreateCampaignViewModel:BaseViewModel
    {
        private readonly INavigation _navigation;
        private readonly int _orgId;
        public CreateCampaignViewModel(INavigation navigation, int orgId)
        {
            _navigation = navigation;
            _orgId = orgId;
        }

        public const string CampaignNamePropertyName = "CampaignName";
        private string _campaignName = string.Empty;
        public string CampaignName
        {
            get { return _campaignName; }
            set { SetProperty(ref _campaignName, value, CampaignNamePropertyName); }
        }

        public const string CampaignDescriptionPropertyName = "CampaignDescription";
        private string _campaignDescription = string.Empty;
        public string CampaignDescription
        {
            get { return _campaignDescription; }
            set { SetProperty(ref _campaignDescription, value, CampaignDescriptionPropertyName); }
        }

        public const string CampaignTargetPropertyName = "CampaignTarget";
        private string _campaignTarget = string.Empty;
        public string CampaignTarget
        {
            get { return _campaignTarget; }
            set { SetProperty(ref _campaignTarget, value, CampaignTargetPropertyName); }
        }

        public const string CampaignTargetDatePropertyName = "CampaignTargetDate";
        private DateTime _campaignTargetDate = DateTime.Now;
        public DateTime CampaignTargetDate
        {
            get { return _campaignTargetDate; }
            set {SetProperty(ref _campaignTargetDate, value, CampaignTargetDatePropertyName);}
        }

        private Command _addCommand;
        public const string AddCommandPropertyName = "AddCommand";
        public Command AddCommand
        {
            get
            {
                return _addCommand ?? (_addCommand = new Command(async () => await ExecuteAddCommand()));
            }
        }

        protected async Task ExecuteAddCommand()
        {
            try
            {
                //Call Api

                await _navigation.PopAsync();
            }
            catch (Exception ex)
            {
                var page = new ContentPage();
                var result = page.DisplayAlert("Error", ex.Message, "OK", "Cancel");
            }
        }
    }

Page Code

            public class CreateCampaignPage:ContentPage
            {
                private readonly Label _campaignTargetLabel;
                private const double _minCampaignTarget = 100;

                public CreateCampaignPage(int orgId)
                {
                    var viewModel = new CreateCampaignViewModel(Navigation, orgId);
                    BindingContext = viewModel;

                    var layout = new StackLayout();

                    var campaignName = new Entry{ Placeholder = "Name" };
                    campaignName.SetBinding(Entry.TextProperty, CreateCampaignViewModel.CampaignNamePropertyName);
                    layout.Children.Add(campaignName);

                    var campaignDescription = new Entry {Placeholder = "Description"};
                    campaignDescription.SetBinding(Entry.TextProperty, CreateCampaignViewModel.CampaignDescriptionPropertyName);
                    layout.Children.Add(campaignDescription);

                    _campaignTargetLabel = new Label
                    {
                        Text = String.Format("Target Amount ${0}", _minCampaignTarget)
                    };
                    layout.Children.Add(_campaignTargetLabel);

                    var campaignTarget = new Slider
                    {
                        Maximum = 5000,
                        Minimum = _minCampaignTarget
                    };
                    campaignTarget.SetBinding(Slider.ValueProperty, CreateCampaignViewModel.CampaignTargetPropertyName);
                    campaignTarget.ValueChanged += async (sender, e) =>
                    {
                        _campaignTargetLabel.Text = String.Format("Target Amount ${0}", e.NewValue);
                    };
                    layout.Children.Add(campaignTarget);

                    var targetDateLabel = new Label
                    {
                        Text = "Campaign Target Date"
                    };
                    layout.Children.Add(targetDateLabel);

                    var campaignTargetDate = new DatePicker
                    {
                        Format = "D"
                    };
                    campaignTargetDate.SetBinding(DatePicker.DateProperty, CreateCampaignViewModel.CampaignTargetDatePropertyName);
                    layout.Children.Add(campaignTargetDate);

                    var button = new Button { Text = "Create Campaign", BackgroundColor = Helpers.Color.Blue.ToFormsColor() };
                    button.SetBinding(Button.CommandProperty, CreateCircleViewModel.AddCommandPropertyName);
                    layout.Children.Add(button);

                    Content = layout;
                }
            }

Viewing all articles
Browse latest Browse all 58056

Trending Articles