【发布时间】:2021-02-18 01:48:11
【问题描述】:
我正在尝试使用 Binding 制作自定义控件,但我的 ImageSource 绑定不起作用。这是我的代码:
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:ContentView { 公共静态只读 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:
<?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:
public partial class MainPage : ContentPage
{
public MainPage()
{
BindingContext = new ViewModel();
InitializeComponent();
}
}
ViewModel 现在是空的,但将来我会用它来控制其他东西。我的想法是使用 Icon="icon.png" 在 MainPage 中设置图标。知道为什么不起作用吗?
【问题讨论】:
标签: c# xamarin.forms