【问题标题】:RadioGroup in Xamarin Forms Native ViewsXamarin 中的 RadioGroup 形成本机视图
【发布时间】:2018-01-22 11:18:52
【问题描述】:

我有以下 XAML 行,它们利用 Xamarin 表单中的本机视图:

        <androidWidget:RadioGroup x:Arguments="{x:Static formsAndroid:Forms.Context}">
            <androidWidget:RadioButton x:Arguments="{x:Static formsAndroid:Forms.Context}" Click="RadioButton1Clicked"/>
            <androidWidget:RadioButton x:Arguments="{x:Static formsAndroid:Forms.Context}" Click="RadioButton2Clicked"/>
            <androidWidget:RadioButton x:Arguments="{x:Static formsAndroid:Forms.Context}" Click="RadioButton3Clicked"/>
            <androidWidget:RadioButton x:Arguments="{x:Static formsAndroid:Forms.Context}" Click="RadioButton4Clicked"/>
        </androidWidget:RadioGroup>

但是,会抛出以下异常:“Can not set the content of androidWidget:RadioGroup as it doesn't have a ContentPropertyAttribute”

只是想知道是否有人可以帮助我找到与 RadioButtons 相关的 RadioGroup 的正确 XAML 布局?

干杯

【问题讨论】:

    标签: c# xaml xamarin.forms xamarin.android


    【解决方案1】:

    但是,会抛出以下异常:“Can not set the content of androidWidget:RadioGroup as it doesn't have a ContentPropertyAttribute”

    并非所有本机视图都可以直接在 xaml 中使用。 RadioGroup 就是其中之一。

    要在您的 Xaml 中使用它,您需要按照以下步骤操作:

    1. YourProject.Droid 中创建一个自定义的RadioGroup 控件:

      public class MyRadioGroup:RadioGroup
      {
          //Every native control in xaml will be wrapped in NativeViewWrapper, so we want to pass a NativeViewWrapper list here
          IList<NativeViewWrapper> items;
          public IList<NativeViewWrapper> ItemsSource
          {
              get {
                  items.Clear();
                  for (int i = 0; i < this.ChildCount; i++)
                  {
                      items.Add(new NativeViewWrapper(this.GetChildAt(i)));
                  }
                  return items;
              }
              set {
                  //xaml compiler will call this setter
                  if (items != value)
                  {
                      items = value;
                      this.RemoveAllViews();
                      foreach (NativeViewWrapper wrapper in items)
                      {
                          this.AddView(wrapper.NativeView);
                      }
                  }
              }
          }
          public MyRadioGroup(Context context) : base(context)
          {
              items = new List<NativeViewWrapper>();
          }
      }
      
    2. 在你的 pcl 库中添加必要的命名空间:

      <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
              ...
              xmlns:androidWrapper="clr-namespace:Xamarin.Forms.Platform.Android;assembly=Xamarin.Forms.Platform.Android;targetPlatform=Android"
              xmlns:androidWidget="clr-namespace:Android.Widget;assembly=Mono.Android;targetPlatform=Android"
              xmlns:androidLocal="clr-namespace:NativeSwitch.Droid;assembly=NativeSwitch.Droid;targetPlatform=Android"
              xmlns:formsAndroid="clr-namespace:Xamarin.Forms;assembly=Xamarin.Forms.Platform.Android;targetPlatform=Android"
      xmlns:local="clr-namespace:NativeSwitch"
      ...">
      
    3. 在您的 xaml 页面中引用自定义 MyRadioGroup

      <StackLayout Margin="20">
          <androidLocal:MyRadioGroup x:Arguments="{x:Static formsAndroid:Forms.Context}" >
              <androidLocal:MyRadioGroup.ItemsSource>
                  <x:Array Type="{x:Type androidWrapper:NativeViewWrapper}">
                      <androidWidget:RadioButton x:Arguments="{x:Static formsAndroid:Forms.Context}" Text="Scale1" />
                      <androidWidget:RadioButton x:Arguments="{x:Static formsAndroid:Forms.Context}" Text="Scale2" />
                      <androidWidget:RadioButton x:Arguments="{x:Static formsAndroid:Forms.Context}" Text="Scale3" />
                      <androidWidget:RadioButton x:Arguments="{x:Static formsAndroid:Forms.Context}" Text="Scale4" />
                  </x:Array>
              </androidLocal:MyRadioGroup.ItemsSource>
          </androidLocal:MyRadioGroup>
      </StackLayout>
      

    您可以找到完整的演示here

    官方类似教程请参考Subclassing Native Views

    关于在 Xaml 中传递参数,请参考Passing Arguments in Xaml

    【讨论】:

    • 感谢您的回答。我按照您的指示进行操作,但现在出现以下异常:Xamarin.Forms.Xaml.XamlParseException:位置 13:18。类型 androidLocal:MyRadioGroup not found in xmlns clr-namespace:MyProject.Droid;assembly=MyProject.Droid;targetPlatform=Android 你对如何解决这个问题有什么建议吗?
    • 请将“MyProject”替换为您的项目名称。例如,如果您的项目名称是NativeViewSample,那么您需要将MyProject.Droid 更改为NativeViewSample.Droid
    • 我试过了,但仍然抛出异常,即使在清理和重建解决方案之后也是如此。只是想知道 MyProjectName.Droid 命名空间没有出现在 PCL 项目的 Intellisense 中的事实是否相关?
    • 没有对该命名空间定义的 Intellisense 支持。您必须自己检查命名空间的拼写。您可以尝试在您的模拟器/设备中卸载并重新安装该应用程序。如果问题仍然存在,您可以通过在线 repo 分享您的项目。或者与我发布的演示进行比较以找出错误
    • 设法让代码运行而不会在我的项目中引发异常。问题围绕着我的项目程序集名称是 MyProjectName.Android 而不是 MyProjectName.Droid。当我将其更改为 .Droid 时,它就像一个魅力。感谢您的建议和代码@Elvis Xia
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-26
    • 1970-01-01
    • 2017-09-24
    • 2020-06-13
    • 2022-01-20
    • 1970-01-01
    相关资源
    最近更新 更多