简体   繁体   English

在 xamarin-forms 的 xaml 内容页面中嵌入和数据绑定 android spinner

[英]Embedding and data-binding android spinner in xaml content page in xamarin-forms

I am trying to embed a native android spinner in a xamarin forms content page.我正在尝试在 xamarin 表单内容页面中嵌入本机 android 微调器。 I have had some joy embedding a check-box from the developer documentation but I want to add a spinner.我很高兴从开发人员文档中嵌入一个复选框,但我想添加一个微调器。 From the documentation, I should be able to data-bind normally to any native control.从文档中,我应该能够正常地将数据绑定到任何本机控件。 So I data-bound an ObservableCollection of strings to a native spinner like so:因此,我将字符串的 ObservableCollection 数据绑定到本机微调器,如下所示:

<?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:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
         prism:ViewModelLocator.AutowireViewModel="True"
         xmlns:android="clr-
namespace:Android.Widget;assembly=Mono.Android;targetPlatform=Android"
         xmlns:androidForms="clr-namespace:Xamarin.Forms;
assembly=Xamarin.Forms.Platform.Android;targetPlatform=Android"
         x:Class="CashFlowBuddie.Views.SelectPage"
         Title="{Binding Title}">
<ContentView HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand">
    <Label Text="Hi there from new app" FontSize="Large" FontAttributes="Bold" TextColor="Aquamarine"/>
    <android:Spinner x:Arguments="{x:Static androidForms:Forms.Context}" ItemsSource="{Binding TextNames}" />
</ContentView>
</ContentPage>

This is my xaml page's viewmodel:这是我的 xaml 页面的视图模型:

public class SelectPageViewModel : ViewModelBase
{
    private ObservableCollection<string> _text;
    public ObservableCollection<string> TextNames
    {
        get { return _text; }
        set { SetProperty(ref _text, value); }
    }
    public SelectPageViewModel(INavigationService navigationService, IPageDialogService dialogService):base(navigationService,dialogService)
    {
        Title = "Select Option Page";
        TextNames = new ObservableCollection<string>
        {
            "First",
            "Second",
            "Third"
        };
    }
}

From this you can tell that its a prism powered xamarin forms app.由此您可以看出它是由棱镜驱动的 xamarin 形式应用程序。 And I register my page for navigation properly:我注册了我的页面以正确导航:

public partial class App : PrismApplication
{
    public App(IPlatformInitializer initializer = null) : base(initializer) { }

    protected override void OnInitialized()
    {
        InitializeComponent();

        NavigationService.NavigateAsync("NavigationPage/MainPage/SelectPage");
    }

    protected override void RegisterTypes()
    {
        Container.RegisterTypeForNavigation<MainPage>();
        Container.RegisterTypeForNavigation<MainNavigation>("NavigationPage");
        Container.RegisterTypeForNavigation<SelectPage>();
    }
}

I made sure that I disabled XamlC from AssemblyInfo.cs since I cannot use XamlC when embedding native controls and views.我确保从 AssemblyInfo.cs 中禁用了 XamlC,因为在嵌入本机控件和视图时无法使用 XamlC。 My app crashes when I do the data-binding to the observablecollection of textnames.当我对文本名称的 observablecollection 进行数据绑定时,我的应用程序崩溃。 So I would like to know if someone has done this and had any joy?所以我想知道是否有人这样做过并且有什么快乐吗? I looked at the spinner in the xamarin.android docs says a xml backed string array is created and is the source of data for the spinner.我查看了 xamarin.android 文档中的微调器,说创建了一个 xml 支持的字符串数组,并且是微调器的数据源。 I did something similar using an observablecollection but if I run the app it just crashes.我使用 observablecollection 做了类似的事情,但是如果我运行该应用程序,它就会崩溃。 If I run the app without the data binding, it works and shows the app with the spinner but no data.如果我在没有数据绑定的情况下运行应用程序,它会工作并显示带有微调器但没有数据的应用程序。

Can anyone shed any light on how to get this done?任何人都可以阐明如何完成这项工作吗? looked at docs and nothing so far though I am still digging?尽管我仍在挖掘,但到目前为止还没有查看文档?

Thanks谢谢

You may refer to Subclassing Native Views , seems like for control like Spinner is not suitable for instantiating in XAML.您可以参考Subclassing Native Views ,似乎像Spinner这样的控件不适合在 XAML 中实例化。 The ItemsSource property should be created in the subclass of native Spinner ItemsSource属性应该在原生Spinner的子类中创建

To check the official demo, you may refer to Subclassed Native Views .要查看官方演示,您可以参考Subclassed Native Views

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

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