我正在研究这种代码
我的 ImageResource 类
[ContentProperty(nameof(Source))]
class ImageResourceExtension : IMarkupExtension
{
public string Source { get; set; }
public object ProvideValue(IServiceProvider serviceProvider)
{
var newImageSource = ImageSource.FromResource(Source, typeof(ImageResourceExtension).GetTypeInfo().Assembly);
return newImageSource;
}
}
我的 ListModel 类
class ListModel
{
public string IconSource { get; set; }
public string IconPath { get; set; }
}
我的 MyViewModel 类
class MyViewModel : INotifyPropertyChanged
{
public List<Models.ListModel> MyListModel { get; }
public Command MyListCommand { get; }
public MyViewModel()
{
MyListModel = new List<Models.ListModel>()
{
new Models.ListModel(){ IconSource="https://example.com/image1",IconPath="MyImage1.jpg"},
new Models.ListModel(){ IconSource="https://example.com/image2",IconPath="MyImage2.jpg"}
};
MyListCommand = new Command(OnListClicked);
}
private async void OnListClicked(object value)
{
string valueString = value as string;
await Launcher.OpenAsync(valueString);
}
}
我的视图类
<?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:images="clr-namespace:MyProject.Images"
xmlns:models="clr-namespace:MyProject.Models"
xmlns:viewModels="clr-namespace:MyProject.ViewModels"
...
<ContentPage.BindingContext>
<viewModels:MyViewModel/>
</ContentPage.BindingContext>
<ContentPage.Content>
<StackLayout BindableLayout.ItemsSource="{Binding MyListModel}">
<BindableLayout.ItemTemplate>
<DataTemplate> <!-- line{setup path} -->
<Image Source="{images:ImageResource Source={Binding Source={RelativeSource AncestorType={x:Type viewModels:MyListModel}}, Path=ImageSource}}">
<Image.GestureRecognizers>
<TapGestureRecognizer
NumberOfTapsRequired="1"
Command="{Binding Source={RelativeSource AncestorType={x:Type viewModels:MyListModel}}, Path=MyListCommand}"
CommandParameter="{Binding ImagePath}"/>
</Image.GestureRecognizers>
</Image>
</DataTemplate>
</BindableLayout.ItemTemplate>
</StackLayout>
</ContentPage.Content>
我的问题是如何为我的图像源设置动态或可绑定属性line: {setup path}
?
如果你能解决这个问题,我真的很感激。谢谢你。
编辑:我忘了添加我的图像Embedded resource
在我的共享代码中。
您可以使用Value Converters来实现此功能。
假设您的图像放置在Images
xamarin 表单项目的文件夹中(例如MyFormDemo
)。
1.创建EmbeddedToImageSourceConverter.cs
public class EmbeddedToImageSourceConverter: IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string ResourceId = value.ToString();
if (String.IsNullOrWhiteSpace(ResourceId))
return null;
return ImageSource.FromResource(ResourceId);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
2.在MyViewModel.cs
,init的构造函数中IconPath
如下(添加MyFormDemo.Images.
):
public MyViewModel()
{
MyListModel = new List<ListModel>()
{
new ListModel(){ IconSource="https://example.com/image1",IconPath="MyFormDemo.Images.cherry.png"},
new ListModel(){ IconSource="https://example.com/image2",IconPath="MyFormDemo.Images.love.png"}
};
MyListCommand = new Command(OnListClicked);
}
INotifyPropertyChanged
3.实现类接口ListModel
:
public class ListModel: INotifyPropertyChanged
{
public string IconSource { get; set; }
//public string IconPath { get; set; }
string _iconPath;
public string IconPath
{
set { SetProperty(ref _iconPath, value); }
get { return _iconPath; }
}
bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
{
if (Object.Equals(storage, value))
return false;
storage = value;
OnPropertyChanged(propertyName);
return true;
}
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
}
4.在您页面的 xaml 中:
<ContentPage.Resources>
<MyFormDemo:EmbeddedToImageSourceConverter x:Key="converter">
</MyFormDemo:EmbeddedToImageSourceConverter>
</ContentPage.Resources>
<ContentPage.BindingContext>
<viewmodel:MyViewModel></viewmodel:MyViewModel>
</ContentPage.BindingContext>
<ContentPage.Content>
<StackLayout BindableLayout.ItemsSource="{Binding MyListModel}">
<BindableLayout.ItemTemplate>
<DataTemplate>
<!-- line{setup path} -->
<Image Source="{Binding IconPath, Converter={StaticResource converter}}" WidthRequest="60" HeightRequest=" 60" BackgroundColor="Gray">
</Image>
</DataTemplate>
</BindableLayout.ItemTemplate>
</StackLayout>
</ContentPage.Content>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句