簡體   English   中英

在 xamarin-forms 的 xaml 內容頁面中嵌入和數據綁定 android spinner

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

我正在嘗試在 xamarin 表單內容頁面中嵌入本機 android 微調器。 我很高興從開發人員文檔中嵌入一個復選框,但我想添加一個微調器。 從文檔中,我應該能夠正常地將數據綁定到任何本機控件。 因此,我將字符串的 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>

這是我的 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"
        };
    }
}

由此您可以看出它是由棱鏡驅動的 xamarin 形式應用程序。 我注冊了我的頁面以正確導航:

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>();
    }
}

我確保從 AssemblyInfo.cs 中禁用了 XamlC,因為在嵌入本機控件和視圖時無法使用 XamlC。 當我對文本名稱的 observablecollection 進行數據綁定時,我的應用程序崩潰。 所以我想知道是否有人這樣做過並且有什么快樂嗎? 我查看了 xamarin.android 文檔中的微調器,說創建了一個 xml 支持的字符串數組,並且是微調器的數據源。 我使用 observablecollection 做了類似的事情,但是如果我運行該應用程序,它就會崩潰。 如果我在沒有數據綁定的情況下運行應用程序,它會工作並顯示帶有微調器但沒有數據的應用程序。

任何人都可以闡明如何完成這項工作嗎? 盡管我仍在挖掘,但到目前為止還沒有查看文檔?

謝謝

您可以參考Subclassing Native Views ,似乎像Spinner這樣的控件不適合在 XAML 中實例化。 ItemsSource屬性應該在原生Spinner的子類中創建

要查看官方演示,您可以參考Subclassed Native Views

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM