サイトアイコン 知的好奇心

Xamarinで名前(string)からUIコントロールを取得する方法

Xamarinで名前(string)からUIコントロールを取得する方法をご紹介します。

条件

実装方法

例として、Labelを取得する場合、以下のようにします。

Label resource = NameScopeExtensions.FindByName<Label>(this, "リソースID文字列");

実装例

以下のようなxamlが定義されているものとします。

MainPage.xaml

<?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:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Class="ResourceTest.MainPage">

    <StackLayout>
        <Label Text="テスト1" x:Name="resource1" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" />
        <Label Text="テスト2" x:Name="resource2" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" />
        <Label Text="テスト3" x:Name="resource3" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" />
        <Label Text="テスト4" x:Name="resource4" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" />
        <Label Text="テスト5" x:Name="resource5" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" />
        <Label Text="テスト6" x:Name="resource6" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" />
        <Label Text="テスト7" x:Name="resource7" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" />
        <Label Text="テスト8" x:Name="resource8" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" />
        <Label Text="テスト9" x:Name="resource9" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" />
        <Label Text="テスト10" x:Name="resource10" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" />
    </StackLayout>

</ContentPage>

何もしない場合

以下のように、ひたすら対象のUIコントロールについて値をセットするという記述になります。

MainPage.xaml.cs

using System.ComponentModel;
using Xamarin.Forms;

namespace ResourceTest
{
    // Learn more about making custom code visible in the Xamarin.Forms previewer
    // by visiting https://aka.ms/xamarinforms-previewer
    [DesignTimeVisible(false)]
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();

            resource1.Text = "リソース1";
            resource2.Text = "リソース2";
            resource3.Text = "リソース3";
            resource4.Text = "リソース4";
            resource5.Text = "リソース5";
            resource6.Text = "リソース6";
            resource7.Text = "リソース7";
            resource8.Text = "リソース8";
            resource9.Text = "リソース9";
            resource10.Text = "リソース10";
        }
    }
}

文字列からUIコントロールを取得する場合

for文でいい感じに記述することが出来ます。

MainPage.xaml.cs

using System.ComponentModel;
using Xamarin.Forms;

namespace ResourceTest
{
    // Learn more about making custom code visible in the Xamarin.Forms previewer
    // by visiting https://aka.ms/xamarinforms-previewer
    [DesignTimeVisible(false)]
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();

            for (int i = 0; i < 10; i++)
            {
                string strId = "resource" + (i + 1).ToString();
                Label resource = NameScopeExtensions.FindByName<Label>(this, strId);
                resource.Text = "リソース" + (i + 1).ToString();
            }
        }
    }
}

実行結果

各ラベルに指定した文字列がセットされていることがわかります。

(参考)Xamarin.Androidの場合

Xamarin.Androidで似たようなことを行う場合、以下のようにしてResourceIdを取得することが出来ます。

var resourceId = (int)typeof(Resource.Id).GetField("リソースID文字列").GetValue(null);

参考

How do I set the x:Name property of a UI control that I created in code?

https://forums.xamarin.com/discussion/136632/how-do-i-set-the-x-name-property-of-a-ui-control-that-i-created-in-code

stackoverflow:Return id of resource, if i know name of resource

https://stackoverflow.com/questions/13038895/return-id-of-resource-if-i-know-name-of-resource

モバイルバージョンを終了