本文介绍了绑定无法使用自定义控件工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Binding 制作自定义控件,但我对 ImageSource 的 Binding 不起作用.这是我的代码:

I'm trying to make a custom control with Binding and my Binding of an ImageSource isn't working. That's my code:

自定义控件.xaml:

Customcontrol.xaml:

<?xml version="1.0" encoding="UTF-8" ?>
<ContentView
    x:Class="Custom.CustomEntry"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
    <ContentView.Content>
        <StackLayout Orientation="Vertical">

            <ImageButton
                BackgroundColor="Transparent"
                Clicked="Open"
                Source="{Binding Icon}" />



            <Frame
                x:Name="frameemojis"
                BackgroundColor="red"
                IsVisible="False">
                <Label Text="Hello" />
            </Frame>
        </StackLayout>
    </ContentView.Content>
</ContentView>

CustomControl.xaml.cs:

CustomControl.xaml.cs:

公共局部类CustomControl:ContentView{公共静态只读 BindableProperty IconProperty =BindableProperty.Create(图标", typeof(ImageSource), typeof(CustomControl));

public partial class CustomControl: ContentView{public static readonly BindableProperty IconProperty =BindableProperty.Create("Icon", typeof(ImageSource), typeof(CustomControl));

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



    bool state = true;
    void Open(object o, System.EventArgs e)
    {

        if (state)
        {
            state = false;
            frameemojis.IsVisible = true;
        }
        else
        {
            state = true;
            frameemojis.IsVisible = false;
        }

    }



    public CustomControl()
    {
        InitializeComponent();
    }
}

}

MainPage.xaml:

MainPage.xaml:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
    x:Class="App23.MainPage"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:fav="clr-namespace:Custom;assembly=Custom">

    <StackLayout>
        <fav:CustomControl Icon="icon.png" WidthRequest="200" />
    </StackLayout>

</ContentPage>

MainPage.xaml.cs:

MainPage.xaml.cs:

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        BindingContext = new ViewModel();
        InitializeComponent();
    }
}

ViewModel 现在是空的,但将来我会用它来控制其他东西.我的想法是在 MainPage 中使用 Icon="icon.png" 设置图标.知道为什么不起作用吗?

The ViewModel now is empty but in the future I will use it to control other things. My idea is to set the icon in the MainPage with Icon="icon.png". Any idea why isn't working?

推荐答案

您可以像下面这样修改自定义视图的代码.

You could modify the code of Custom View like following .

<?xml version="1.0" encoding="UTF-8" ?>
<ContentView
    x:Class="Custom.CustomEntry"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
     x:Name="CustomView"   //set name of custom view
    >
    <ContentView.Content>
        <StackLayout Orientation="Vertical">

            <ImageButton
                BackgroundColor="Transparent"
                Clicked="Open"
                Source="{Binding Source={x:Reference CustomView},Path=Icon}" />



            <Frame
                x:Name="frameemojis"
                BackgroundColor="red"
                IsVisible="False">
                <Label Text="Hello" />
            </Frame>
        </StackLayout>
    </ContentView.Content>
</ContentView>

这篇关于绑定无法使用自定义控件工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 04:05