質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Visual Studio

Microsoft Visual StudioはMicrosoftによる統合開発環境(IDE)です。多種多様なプログラミング言語に対応しています。

XAML

XAML(Extensible Application Markup Language)はWPF、Silverlight、Windows PhoneそしてWindows Store appsでユーザーインターフェースを定義するために使われるXML言語です。

Xamarin

Xamarin(ザマリン)は、iPhoneなどのiOSやAndroidで動作し、C# 言語を用いてアプリを開発できるクロスプラットフォーム開発環境です。Xamarin Studioと C# 言語を用いて、 iOS と Android の両方の開発を行うことができます。

Q&A

解決済

1回答

1358閲覧

Xamarin.Formsでカスタムコントロールを配置したい

micchi

総合スコア36

Visual Studio

Microsoft Visual StudioはMicrosoftによる統合開発環境(IDE)です。多種多様なプログラミング言語に対応しています。

XAML

XAML(Extensible Application Markup Language)はWPF、Silverlight、Windows PhoneそしてWindows Store appsでユーザーインターフェースを定義するために使われるXML言語です。

Xamarin

Xamarin(ザマリン)は、iPhoneなどのiOSやAndroidで動作し、C# 言語を用いてアプリを開発できるクロスプラットフォーム開発環境です。Xamarin Studioと C# 言語を用いて、 iOS と Android の両方の開発を行うことができます。

0グッド

0クリップ

投稿2020/10/12 15:48

Xamarin.Formsでアプリを作り始めたのですがカスタムコントロールを作って
それを部品として使用しようと思ったのですが、配置するところで躓いてしまいました

controls:View1などとやれば配置できるかと思ったのですがはいちできず・・・

配置したいカスタムコントロール(xaml)(コンテンツビューとして作成)

<?xml version="1.0" encoding="UTF-8"?> <ContentView xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="CustomComponent.Custom.View1"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="*" /> </Grid.RowDefinitions> <Label Text="これかな" Grid.Row="0" Grid.Column="0" Grid.RowSpan="2" /> <Stepper Minimum="0" Maximum="10" Increment="1" Grid.Row="0" Grid.Column="1" /> <Stepper Minimum="0" Maximum="10" Increment="1" Grid.Row="1" Grid.Column="1" /> </Grid> </ContentView>

配置したいカスタムコントロール(cs)

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Xamarin.Forms; using Xamarin.Forms.Xaml; namespace CustomComponent.Custom { [XamlCompilation(XamlCompilationOptions.Compile)] public partial class View1 : ContentView { public View1() { InitializeComponent(); } } }

このページに配置したい

<?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="CustomComponent.Views.AboutPage" xmlns:vm="clr-namespace:CustomComponent.ViewModels" Title="{Binding Title}"> <ContentPage.BindingContext> <vm:AboutViewModel /> </ContentPage.BindingContext> <ContentPage.Resources> <ResourceDictionary> <Color x:Key="Accent">#96d1ff</Color> </ResourceDictionary> </ContentPage.Resources> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <StackLayout BackgroundColor="{StaticResource Accent}" VerticalOptions="FillAndExpand" HorizontalOptions="Fill"> <StackLayout Orientation="Horizontal" HorizontalOptions="Center" VerticalOptions="Center"> <ContentView Padding="0,40,0,40" VerticalOptions="FillAndExpand"> <Image Source="xamarin_logo.png" VerticalOptions="Center" HeightRequest="64" /> </ContentView> </StackLayout> </StackLayout> <ScrollView Grid.Row="1"> <StackLayout Orientation="Vertical" Padding="30,24,30,24" Spacing="10"> <Label Text="Start developing now" FontSize="Title"/> <Label Text="Make changes to your XAML file and save to see your UI update in the running app with XAML Hot Reload. Give it a try!" FontSize="16" Padding="0,0,0,0"/> <Label FontSize="16" Padding="0,24,0,0"> <Label.FormattedText> <FormattedString> <FormattedString.Spans> <Span Text="Learn more at "/> <Span Text="https://aka.ms/xamarin-quickstart" FontAttributes="Bold"/> </FormattedString.Spans> </FormattedString> </Label.FormattedText> </Label> <Button Margin="0,10,0,0" Text="Learn more" Command="{Binding OpenWebCommand}" BackgroundColor="{StaticResource Primary}" TextColor="White" /> </StackLayout> </ScrollView> <Stepper Minimum="0" Maximum="10" Increment="1" /> </Grid> </ContentPage>

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

名前空間の宣言が必要です。プレフィックスをcontrolにするなら、xmlns:control="clr-namespace:CustomComponent.Custom"ContentPageに記述します。

<?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="CustomComponent.Views.AboutPage" xmlns:vm="clr-namespace:CustomComponent.ViewModels" xmlns:control="clr-namespace:CustomComponent.Custom" Title="{Binding Title}"> <control:View1 /> </ContentPage>

投稿2020/10/12 19:51

f-miyu

総合スコア1625

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

micchi

2020/10/13 16:22

自分でネームスペースを設定してあげなければならなかったのですね ありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問