【发布时间】:2017-09-23 23:47:29
【问题描述】:
我正在尝试在 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。当我对 textnames 的 observablecollection 进行数据绑定时,我的应用程序崩溃了。所以我想知道是否有人这样做并且有什么快乐?我查看了 xamarin.android 文档中的微调器,说创建了一个 xml 支持的字符串数组,并且是微调器的数据源。我使用 observablecollection 做了类似的事情,但如果我运行应用程序,它就会崩溃。如果我在没有数据绑定的情况下运行应用程序,它会工作并显示带有微调器但没有数据的应用程序。
任何人都可以阐明如何完成这项工作吗?虽然我还在挖掘,但看了文档,但到目前为止什么都没有?
谢谢
【问题讨论】:
标签: c# android xaml xamarin.forms