C# Xamarin表单中的自定义控件不';行不通

C# Xamarin表单中的自定义控件不';行不通,c#,xamarin.forms,C#,Xamarin.forms,我正在尝试创建一个ImageButton的自定义控件,我可以为它指定一个属性,该属性将为它指定一种颜色,另一个属性将为它指定一个带有源的图像。我试着这样做,但我给它的图像和颜色不适合我。这是我的代码: View1.xaml.cs: public partial class View1 : ContentView { public static readonly BindableProperty ImageSourceProperty = BindablePropert

我正在尝试创建一个ImageButton的自定义控件,我可以为它指定一个属性,该属性将为它指定一种颜色,另一个属性将为它指定一个带有源的图像。我试着这样做,但我给它的图像和颜色不适合我。这是我的代码:

View1.xaml.cs:

 public partial class View1 : ContentView
{

    public static readonly BindableProperty ImageSourceProperty =
        BindableProperty.Create("Source", typeof(ImageSource),typeof(ImageButton),default(ImageSource));

    public ImageSource Source { 
        get { return (ImageSource)GetValue(ImageSourceProperty); }
        set { SetValue(ImageSourceProperty, value); } 
    }

    //============================================

    public static readonly BindableProperty ButtonColorProperty =
        BindableProperty.Create("ButtonColor", typeof(Color), typeof(ImageButton),  Color.White);

    public Color ButtonColor
    {
        get { return (Color)GetValue(ButtonColorProperty); }
        set { SetValue(ButtonColorProperty, value); }
    }


    public View1()
    {
        InitializeComponent();

    }
}
View1.xaml:


您必须如下设置ImageButton的值:


您必须如下设置ImageButton的值:


您可以尝试进行如下更改:

自定义控件视图1

[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class View1 : ContentView
{
    public static readonly BindableProperty ImageSourceProperty =
BindableProperty.Create("ImageSource", typeof(ImageSource), typeof(View1), null);

    public ImageSource ImageSource
    {
        get { return (ImageSource)GetValue(ImageSourceProperty); }
        set { SetValue(ImageSourceProperty, value); }
    }



    public static readonly BindableProperty ButtonColorProperty =
        BindableProperty.Create("ButtonColor", typeof(Color), typeof(View1), null);

    public Color ButtonColor
    {
        get { return (Color)GetValue(ButtonColorProperty); }
        set { SetValue(ButtonColorProperty, value); }
    }

    public View1()
    {
        InitializeComponent();
        BindingContext = this;
    }
}  
<?xml version="1.0" encoding="UTF-8"?>
<ContentView 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="EntryCa.View1">
  <ContentView.Content>
    <StackLayout>
        <ImageButton x:Name="image"  CornerRadius="20" WidthRequest="70" HeightRequest="70"  Source="{Binding ImageSource}"  BackgroundColor="{Binding ButtonColor}" />
    </StackLayout>
  </ContentView.Content>
</ContentView>
视图1.xaml

[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class View1 : ContentView
{
    public static readonly BindableProperty ImageSourceProperty =
BindableProperty.Create("ImageSource", typeof(ImageSource), typeof(View1), null);

    public ImageSource ImageSource
    {
        get { return (ImageSource)GetValue(ImageSourceProperty); }
        set { SetValue(ImageSourceProperty, value); }
    }



    public static readonly BindableProperty ButtonColorProperty =
        BindableProperty.Create("ButtonColor", typeof(Color), typeof(View1), null);

    public Color ButtonColor
    {
        get { return (Color)GetValue(ButtonColorProperty); }
        set { SetValue(ButtonColorProperty, value); }
    }

    public View1()
    {
        InitializeComponent();
        BindingContext = this;
    }
}  
<?xml version="1.0" encoding="UTF-8"?>
<ContentView 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="EntryCa.View1">
  <ContentView.Content>
    <StackLayout>
        <ImageButton x:Name="image"  CornerRadius="20" WidthRequest="70" HeightRequest="70"  Source="{Binding ImageSource}"  BackgroundColor="{Binding ButtonColor}" />
    </StackLayout>
  </ContentView.Content>
</ContentView>

像这样使用它:

<local:View1   ImageSource="dieciseis.png" ButtonColor="Green"></local:View1>

您可以尝试进行如下更改:

自定义控件视图1

[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class View1 : ContentView
{
    public static readonly BindableProperty ImageSourceProperty =
BindableProperty.Create("ImageSource", typeof(ImageSource), typeof(View1), null);

    public ImageSource ImageSource
    {
        get { return (ImageSource)GetValue(ImageSourceProperty); }
        set { SetValue(ImageSourceProperty, value); }
    }



    public static readonly BindableProperty ButtonColorProperty =
        BindableProperty.Create("ButtonColor", typeof(Color), typeof(View1), null);

    public Color ButtonColor
    {
        get { return (Color)GetValue(ButtonColorProperty); }
        set { SetValue(ButtonColorProperty, value); }
    }

    public View1()
    {
        InitializeComponent();
        BindingContext = this;
    }
}  
<?xml version="1.0" encoding="UTF-8"?>
<ContentView 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="EntryCa.View1">
  <ContentView.Content>
    <StackLayout>
        <ImageButton x:Name="image"  CornerRadius="20" WidthRequest="70" HeightRequest="70"  Source="{Binding ImageSource}"  BackgroundColor="{Binding ButtonColor}" />
    </StackLayout>
  </ContentView.Content>
</ContentView>
视图1.xaml

[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class View1 : ContentView
{
    public static readonly BindableProperty ImageSourceProperty =
BindableProperty.Create("ImageSource", typeof(ImageSource), typeof(View1), null);

    public ImageSource ImageSource
    {
        get { return (ImageSource)GetValue(ImageSourceProperty); }
        set { SetValue(ImageSourceProperty, value); }
    }



    public static readonly BindableProperty ButtonColorProperty =
        BindableProperty.Create("ButtonColor", typeof(Color), typeof(View1), null);

    public Color ButtonColor
    {
        get { return (Color)GetValue(ButtonColorProperty); }
        set { SetValue(ButtonColorProperty, value); }
    }

    public View1()
    {
        InitializeComponent();
        BindingContext = this;
    }
}  
<?xml version="1.0" encoding="UTF-8"?>
<ContentView 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="EntryCa.View1">
  <ContentView.Content>
    <StackLayout>
        <ImageButton x:Name="image"  CornerRadius="20" WidthRequest="70" HeightRequest="70"  Source="{Binding ImageSource}"  BackgroundColor="{Binding ButtonColor}" />
    </StackLayout>
  </ContentView.Content>
</ContentView>

像这样使用它:

<local:View1   ImageSource="dieciseis.png" ButtonColor="Green"></local:View1>


您没有做任何事情来绑定您的两个属性您的UI元素有一个名为ImageButton的现有控件,它可以完成这项工作!!如何将这两个属性绑定在一起?我知道有一个名为ImageButton的现有控件,但我只是举个例子。正如@FreakyAli指出的,您可以将ImageButton子类化,避免创建自定义可绑定属性。我知道我可以使用ImageButton,这只是一个尝试自定义控件的示例。如何绑定属性以使其正常工作?您没有做任何事情来绑定两个属性您的UI元素现有的名为ImageButton的控件正是这样做的!!如何将这两个属性绑定在一起?我知道有一个名为ImageButton的现有控件,但我只是举个例子。正如@FreakyAli指出的,您可以将ImageButton子类化,避免创建自定义可绑定属性。我知道我可以使用ImageButton,这只是一个尝试自定义控件的示例。如何连接属性以使其正常工作?