简体   繁体   English

如何在Xamarin.Forms中设置选择器的选定项?

[英]How to set picker's selected item in Xamarin.Forms?

I have a picker with that gets the customer outlet. 我有一个选择器,可以获得客户。 I want to update the what I saved from my database. 我想更新我从数据库中保存的内容。

Here is my app logic: 这是我的app逻辑:
1. The user will select a customer outlet 1.用户将选择一个客户插座
2. The user will save the selected outlet 2.用户将保存所选插座
3. The user will have an option to update what the user selected 4. Upon loading of the page the customer outlet on the picker will be loaded and the what the user saved on will be the item. 3.用户可以选择更新用户选择的内容4.加载页面后,将加载选取器上的客户插座,用户保存的内容将成为该项目。

How can I achieve this? 我怎样才能做到这一点?

Here are my codes: 这是我的代码:

Here is how I load all of outlet: 这是我如何加载所有插座:

var db = DependencyService.Get<ISQLiteDB>();
var conn = db.GetConnection();

var getCode = conn.QueryAsync<ContactsTable>("SELECT * FROM tblContacts LEFT OUTER JOIN tblCustomerMember ON tblCustomerMember.CustomerID = tblContacts.ContactID WHERE CustomerID = ? AND tblCustomerMember.Deleted !='1'", lblCustomer.Text);
var resultCount = getCode.Result.Count;
if (resultCount > 0)
{
   var result = getCode.Result;
   pckOutlet.ItemsSource = result;
}
else
{
    pckOutlet.IsEnabled = false;
}

And here is my XAML: 这是我的XAML:

<Picker x:Name="pckOutlet" Focused="PckOutlet_Focused" SelectedIndexChanged="PckOutlet_SelectedIndexChanged" ItemDisplayBinding="{Binding DisplayText}" StyleClass="formcontrol" IsEnabled="False">
     <Picker.FontFamily>
          <OnPlatform x:TypeArguments="x:String">
               <On Platform="Android" Value="SFProDisplay-Regular.ttf#SFProDisplay-Regular"/>
              </OnPlatform>
      </Picker.FontFamily>
</Picker>

You need to set the SelectedItem property to the previously selected item. 您需要将SelectedItem属性设置为先前选择的项目。

if (resultCount > 0)
{
   var result = getCode.Result;
   pckOutlet.ItemsSource = result;
   pckOutlet.SelectedItem = result.First(outlet => outlet.Id == *PREVIOUSLY_SELECTED_ID*)
}

You may need to store the previously selected ID somewhere where it can survive app resets. 您可能需要将以前选择的ID存储在应用程序重置的位置。 Xamarin.Essentials.SecureStorage or Xamarin.Essentials.Preferences can be used for this and is fairly straight forward to use. Xamarin.Essentials.SecureStorage或Xamarin.Essentials.Preferences可用于此,并且非常直接使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM