gpt4 book ai didi

c# - 在 xamarin-forms 中的 xaml 内容页面中嵌入和数据绑定(bind) android 微调器

转载 作者:行者123 更新时间:2023-11-30 00:20:31 24 4
gpt4 key购买 nike

我正在尝试在 xamarin 表单内容页面中嵌入原生 android 微调器。我从开发人员文档中嵌入了一个复选框,但我想添加一个微调器,这让我很高兴。从文档中,我应该能够正常地将数据绑定(bind)到任何 native 控件。所以我将字符串的 ObservableCollection 数据绑定(bind)到本地微调器,如下所示:

<?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 页面的 View 模型:

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,因为在嵌入 native 控件和 View 时我无法使用 XamlC。当我对文本名称的可观察集合进行数据绑定(bind)时,我的应用程序崩溃了。所以我想知道是否有人这样做并且有任何快​​乐?我查看了 xamarin.android 文档中的微调器说创建了一个 xml 支持的字符串数组,它是微调器的数据源。我使用 observablecollection 做了类似的事情,但如果我运行该应用程序,它就会崩溃。如果我在没有数据绑定(bind)的情况下运行应用程序,它会工作并显示带有微调器但没有数据的应用程序。

任何人都可以阐明如何完成这项工作吗?尽管我仍在挖掘,但到目前为止还没有查看文档?

谢谢

最佳答案

可以引用Subclassing Native Views ,似乎像 Spinner 这样的控件不适合在 XAML 中实例化。 ItemsSource 属性应该在原生 Spinner

的子类中创建

查看官方demo,可引用Subclassed Native Views .

关于c# - 在 xamarin-forms 中的 xaml 内容页面中嵌入和数据绑定(bind) android 微调器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46385274/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com