【问题标题】:I'm looking for a more reliable option in Xamarin我正在 Xamarin 中寻找更可靠的选择
【发布时间】:2019-11-01 20:45:12
【问题描述】:

我不知道它是怎么称呼的。 我需要创建以这种方式工作的东西: 执行按钮,当您单击按钮下的按钮时,您会看到列表,您可以选择一个选项。列表应该是按钮的宽度。

您可以在应用程序中找到它来选择例如国家/地区的语言。

Xamarin 是否内置了一些东西来创建它?或者有人可以告诉我如何实现吗?

【问题讨论】:

  • 你能张贴你想要的截图或模型吗?你的描述真的没有任何意义
  • 听起来你想要一个组合框或下拉列表
  • 我添加了。你能看看吗?
  • 比什么更可靠的选择?我不明白你的标题。
  • 对不起,我不知道标题

标签: c# xamarin


【解决方案1】:

或者您可以在表单中滚动自己的内容,例如:

ImagePickerDropDown.xaml:

<?xml version="1.0" encoding="UTF-8"?>
<ContentView
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    x:Class="ImagePickerDropdownSample.ImagePickerDropdown"
    x:Name="imagePickerDropDown" >
    <ContentView.Content>
        <StackLayout>
            <ImageButton x:Name="mainButton"
                Source="{Binding Source={x:Reference imagePickerDropDown}, Path=SelectedImage}"
                Clicked="ImageClicked" />
            <StackLayout x:Name="stackView"
                BindableLayout.ItemsSource="{Binding Source={x:Reference imagePickerDropDown}, Path=Images}"
                IsVisible="False">
                <BindableLayout.ItemTemplate>
                    <DataTemplate>
                        <StackLayout>
                            <ImageButton Source="{Binding .}" Clicked="ImageSelected"/>
                        </StackLayout>
                    </DataTemplate>
                </BindableLayout.ItemTemplate>
            </StackLayout>
        </StackLayout>
    </ContentView.Content>
</ContentView>

ImagePickerDropDown.xaml.cs:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Xamarin.Forms;

namespace ImagePickerDropdownSample
{
    public partial class ImagePickerDropdown : ContentView
    {
        public ImagePickerDropdown()
        {
            InitializeComponent();
        }

        private void ImageSelected(object sender, EventArgs e)
        {
            var imageSource = (sender as ImageButton).Source;
            SelectedImage = imageSource;
            mainButton.IsEnabled = true;
            stackView.IsVisible = false;
        }

        private void ImageClicked(object sender, EventArgs e)
        {
            mainButton.IsEnabled = false;
            stackView.IsVisible = true;
        }

        public static readonly BindableProperty SelectedImageProperty =
  BindableProperty.Create(nameof(SelectedImage), typeof(ImageSource), typeof(ImagePickerDropdown), null);

        public ImageSource SelectedImage
        {
            get
            {
                return (ImageSource)GetValue(SelectedImageProperty);
            }
            set
            {
                SetValue(SelectedImageProperty, value);
            }
        }

        public static readonly BindableProperty ImagesProperty =
  BindableProperty.Create(nameof(Images), typeof(ObservableCollection<ImageSource>), typeof(ImagePickerDropdown), null);

        public ObservableCollection<ImageSource> Images
        {
            get
            {
                return (ObservableCollection<ImageSource>)GetValue(ImagesProperty);
            }
            set
            {
                SetValue(ImagesProperty, value);
            }
        }
    }
}

使用 XAML:

<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Class="ImagePickerDropdownSample.MainPage"
             xmlns:local="clr-namespace:ImagePickerDropdownSample"
             Padding="0,50,0,0"
             BackgroundColor="Black">
        <StackLayout
            x:Name="mainLayout">
            <Label Text="Welcome to Xamarin.Forms!"
                   HorizontalOptions="Center"
                   VerticalOptions="Start"
                   TextColor="White"/>
            <local:ImagePickerDropdown SelectedImage="{Binding SelectedImage}"
                                       Images="{Binding Images}"
                                       WidthRequest="50"
                                       HorizontalOptions="Center"
                                       BackgroundColor="Black"/>

        </StackLayout>
</ContentPage>

在后面使用它的代码:

using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace ImagePickerDropdownSample
{
    // Learn more about making custom code visible in the Xamarin.Forms previewer
    // by visiting https://aka.ms/xamarinforms-previewer
    [DesignTimeVisible(false)]
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();

            Images = new ObservableCollection<ImageSource>();
            Images.Add(new FileImageSource() { File = "image1.png" });
            Images.Add(new FileImageSource() { File = "image2.png" });
            Images.Add(new FileImageSource() { File = "image3.png" });
            SelectedImage = Images[0];
            BindingContext = this;
        }

        ImageSource _selectedImage;
        public ImageSource SelectedImage
        {
            get
            {
                return _selectedImage;
            }
            set
            {
                if (_selectedImage != value)
                {
                    _selectedImage = value;
                    OnPropertyChanged(nameof(SelectedImage));
                }
            }
        }

        ObservableCollection<ImageSource> _images;
        public ObservableCollection<ImageSource> Images
        {
            get
            {
                return _images;
            }
            set
            {
                if (_images != value)
                {
                    _images = value;
                    OnPropertyChanged(nameof(Images));
                }
            }
        }
    }
}

【讨论】:

    【解决方案2】:

    使用Spinner .. 基本上您需要先创建一个ArrayAdapter,然后将ArrayAdapter 附加到Spinner:

    //we need a List of some type because the ArrayAdapter takes one as param
    var items = new List<string>() {"one", "two", "three"};
    //instantiate the ArrayAdapter with context, your Resource is a layout, items is the List
    var adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleSpinnerItem, items);
    //then instantiate your spinner
    var spinner = FindViewById<Spinner>(Resource.Id.spinner);
    //and attach the adapter to the spinner like this
    spinner.Adapter = adapter;
    

    来自@Aaron He

    Create android spinner dynamically in Xamarin

    【讨论】:

    • 好的,谢谢,这可能是我需要的。我希望它不是原生解决方案;)
    • 在我发布更多图像类型的内容后,您编辑了问题。我很确定您可以使用带有ArrayAdapter / Spinner 的图像以及查看这篇文章:stackoverflow.com/questions/21833181/…
    猜你喜欢
    • 2022-09-30
    • 2012-03-20
    • 1970-01-01
    • 2022-12-09
    • 1970-01-01
    • 2010-12-08
    • 1970-01-01
    • 1970-01-01
    • 2020-05-30
    相关资源
    最近更新 更多