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

Binding Enum and using Preferences to save settings

$
0
0

Hi all,

I have come across another roadblock in my app, and would love to have your assistance.

I have my enum like this:

    public enum FuturesTakeProfitMode
    {
        ProfitByTarget,
        ProfitByPriceRange,
        ProfitByAny
    }

Using Xamarin Preferences, I have the following in the SettingsViewModel.cs:

        public List<string> ProftTakeModes
        {
            get
            {
                return Enum.GetNames(typeof(FuturesTakeProfitMode)).Select(x => x.SplitCamelCase()).ToList();
            }
        }

        public override FuturesTakeProfitMode TakeProfitMode
        {
            get => (FuturesTakeProfitMode)Preferences.Get(nameof(TakeProfitMode), (int)FuturesTakeProfitMode.ProfitByAny);
            set => SetPreference(nameof(TakeProfitMode), (int)value);
        }

In my XAML, I have:

            <StackLayout>
                <Label HorizontalOptions="Start">Profit take mode</Label>
                <Picker ItemsSource="{Binding ProftTakeModes}" SelectedIndex="{Binding TakeProfitMode, Converter={StaticResource IntEnum}}"></Picker>
            </StackLayout>

I get the following error when it is run:

System.Reflection.TargetInvocationException: 'Exception has been thrown by the target of an invocation.'

Without binding SelectedIndex, everything works, so I know something is wrong with the binding SelectedIndex.

            <StackLayout>
                <Label HorizontalOptions="Start">Profit take mode</Label>
                <Picker ItemsSource="{Binding ProftTakeModes}"></Picker>
            </StackLayout>

I was hoping you could help me figure out what the issue is please.

Thanks,
Michael


Viewing all articles
Browse latest Browse all 204402

Trending Articles



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