如何记住选择器中的选择,我使用的是montemagno的xamarin.plugins.settings,我不知道如何获取正确的语法

时间:2019-06-16 15:27:38

标签: xamarin xamarin.forms

将保存值或字符串,但是未保存主页本身选择器上的选择。当我重新打开应用程序时,选择器为空。我不知道如何使用选择器的语法。
npicker1 = nlocation1;

xaml:

  <Picker x:Name="drainlocationPicker1" Title="Drain Location" 
                 Grid.Column="1" SelectedIndexChanged="drain1Handle_SelectedIndexChanged" />

//后面的代码

//选择列表:

 public SettingsPage()
    {
    InitializeComponent();
    nquantity();
    list = new List<string>();
    list.Add("Right1");
    list.Add("Right2");
    list.Add("Right3");
    list.Add("Right4");

    //picker1 example selection
    void n1Handle_SelectedIndexChanged(object sender, System.EventArgs e)
            {
     nlocation1 = (string)nlocationPicker1.SelectedItem;
    //if picker1 is not empty then show n2picker
    if (nlocationPicker1.SelectedItem != null)
    {
    nPicker2.IsVisible = true;
    Picker1.IsEnabled = false;

    nlocationPicker2.Items.Remove((string)nlocationPicker1.SelectedItem);

    nlocationPicker3.Items.Remove((string)nlocationPicker1.SelectedItem);
    }
    //supposed to save picker selection
    void settingsaveButton_Clicked(object sender, System.EventArgs e)
    {


    bool isNameEmpty = string.IsNullOrEmpty(nameEntry.Text);
    if (isNameEmpty == true)
    {
        DisplayAlert("Enter Name", "PLEASE", "OK");
    }
    else if (nlocationPicker1.SelectedIndex == -1)
    {
        DisplayAlert("Error1", "Please select your country.", "Ok");
        nPicker1.Focus();
    }

    else
    {
    Navigation.PushModalAsync(new HomePage());

    //put nlocation1 to nPicker1
    // npicker=nlocation1???
    Settings.n1LocationSettings = nlocation1;
    Settings.n2LocationSettings = nlocation2;
    Settings.n3LocationSettings = nlocation3; 

    }

1 个答案:

答案 0 :(得分:0)

如果要为选择器设置默认值,则可以使用:

1。function sendEmails() { var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Agent Report - Monthly") var startRow = 3; // First row of data to process since there is a header row var emailRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Agent Report - Monthly").getRange("D3"); var emailAddress = emailRange.getValues(); var chartImg = Charts.newLineChart.setRange(12, 8) //This Throws an error var subject = "Agent Report - Monthly"; // This is the subject of the email var greeting_text = 'Hey Please find the below Report '+'</b><br><br>'; var text = greeting_text; text+=SheetConverter.convertRange2html(sheet.getRange(startRow, 3 ,10,8)); text+='<br>'; var options = { htmlBody: text, noReply:true }; MailApp.sendEmail({ to: "xxx@der.com", subject: subject, htmlBody: options, inlineImages: { chartImg: chartImage, } }); }

2。设置索引:ChartImg

3。在xaml中:

npicker.SelectedItem = nlocation1;

注意:

  

可以通过设置选择器来初始化选择器以显示特定项目   SelectedIndex或SelectedItem属性。但是,这些属性   必须在初始化ItemsSource集合之后设置。

文档在这里:picker

我不知道您的项目中的npicker.SelectedIndex = 2;是什么。我为您创建了一个简单的示例。

在xaml中:

<Picker  x:Name="drainlocationPicker1" 
                Title="Drain Location" 
                HorizontalOptions="CenterAndExpand" 
                VerticalOptions="CenterAndExpand"
                ItemsSource="{Binding myList}"
                ItemDisplayBinding="{Binding Title}"
                SelectedItem="{Binding SelectedObject}"        
                />

在后面的代码中:

Settings.Drain1LocationSettings

您可以使用<StackLayout> <!-- Place new controls here --> <Picker x:Name="drainlocationPicker1" Title="Drain Location" SelectedIndexChanged="DrainlocationPicker1_SelectedIndexChanged" VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand"/> </StackLayout> 存储用户选择的值,并在下次将其设置为选择器。

通过使用plugin.setting,您应通过以下方式定义public partial class MainPage : ContentPage { private static ISettings AppSettings => CrossSettings.Current; public static string LastPickValue { get => AppSettings.GetValueOrDefault(nameof(LastPickValue), string.Empty); set => AppSettings.AddOrUpdateValue(nameof(LastPickValue), value); } public MainPage() { InitializeComponent(); List<string> list = new List<string>(); list.Add("Right1"); list.Add("Right2"); list.Add("Right3"); list.Add("Right4"); drainlocationPicker1.ItemsSource = list; //Set the default value drainlocationPicker1.SelectedItem = LastPickValue; } private void DrainlocationPicker1_SelectedIndexChanged(object sender, EventArgs e) { string nlocation1 = (string)drainlocationPicker1.SelectedItem; LastPickValue = nlocation1; } }

LastPickValue

我上传了示例here,您可以检查它,让我知道它是否有效。