You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Xamarin应用点击Alert的OK后Picker列表意外弹出的问题解决

Fix: Picker List Pops Up Unexpectedly After Closing Success Alert in Xamarin.Forms

I see the issue here - when you close the DisplayAlert by tapping "OK", the focus automatically returns to your URLPicker, and your custom Picker control is triggering its dropdown list as soon as it regains focus. That's why you're seeing the list pop up unexpectedly after confirming the URL change.

Here are two straightforward fixes to resolve this:

Solution 1: Disable Picker Temporarily Before Showing Alert

This method prevents the Picker from responding to focus changes while the alert is active, then re-enables it once the alert is closed:

Updated C# Code

public partial class ChangeServerUrl : ContentView
{
  List<string> urlItems = new List<string>() { "http://divcraft-001-site4.com/", "https://lcdpbe.azurewebs.net", "http://192.168.0.14:8077" };
  private bool handleSelect = false;

  public ChangeServerUrl ()
  {
    InitializeComponent ();
    URLPicker.ItemsSource = urlItems;
    URLPicker.SelectedItem = Preferences.Get(Constants.PreferenceKeys.SERVER_BASE, Constants.PreferenceKeys.SERVER_BASE);
  }

  private async void URLPicker_SelectedIndexChanged(object sender, EventArgs e)
  {
    if(handleSelect == true)
    {
        // Disable the Picker to prevent focus-triggered dropdown
        URLPicker.IsEnabled = false;

        // Save the selected URL and update your service
        var selectedUrl = URLPicker.SelectedItem.ToString();
        Preferences.Set(Constants.PreferenceKeys.SERVER_BASE, selectedUrl);
        RestLayer.RestService.Instance.ChangeClientBase(selectedUrl);

        // Wait for the alert to be dismissed
        await App.Current.MainPage.DisplayAlert("", AppResources.Server_Base_Changed_Message, AppResources.OK);

        // Re-enable the Picker for future use
        URLPicker.IsEnabled = true;
    }
    else
    {
        handleSelect = true;
    }
  }
}

Solution 2: Redirect Focus to Another Control

If you prefer not to disable the Picker, you can shift focus to a non-interactive control (like your header Label) after the alert closes:

Step 1: Add x:Name to Your Label in XAML

<ContentView.Content>
  <Grid HorizontalOptions="FillAndExpand" RowSpacing="10">
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
      <RowDefinition Height="Auto" />
      <RowDefinition Height="35" />
    </Grid.RowDefinitions>
    <!-- Add x:Name to the Label -->
    <Label x:Name="ServerUrlLabel" Grid.Row="0" Text="{translate:Translate Change_Server_URL}" FontSize="Small"/>
    <Frame HeightRequest="35" Grid.Row="1" Style="{StaticResource EntryFramStyle}">
      <Grid HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
        <Grid.ColumnDefinitions>
          <ColumnDefinition Width="*"/>
          <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        <rendres:CustomPicker Grid.Column="0" HasBorder="False" x:Name="URLPicker" SelectedIndexChanged="URLPicker_SelectedIndexChanged" Margin="5,0,5,0" VerticalOptions="Center" >
        </rendres:CustomPicker>
      </Grid>
    </Frame>
  </Grid>
</ContentView.Content>

Step 2: Update the C# Event Handler

private async void URLPicker_SelectedIndexChanged(object sender, EventArgs e)
{
    if(handleSelect == true)
    {
        var selectedUrl = URLPicker.SelectedItem.ToString();
        Preferences.Set(Constants.PreferenceKeys.SERVER_BASE, selectedUrl);
        RestLayer.RestService.Instance.ChangeClientBase(selectedUrl);

        await App.Current.MainPage.DisplayAlert("", AppResources.Server_Base_Changed_Message, AppResources.OK);

        // Redirect focus to the Label to avoid Picker gaining focus
        ServerUrlLabel.Focus();
    }
    else
    {
        handleSelect = true;
    }
}

Both solutions will stop the Picker list from popping up after the alert is closed, while keeping your success message intact. The first solution is generally more reliable, especially if your custom Picker has specific focus behaviors.

内容的提问来源于stack exchange,提问作者sver

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 08:37:11