gpt4 book ai didi

c# - 如何在 Xamarin.Forms 中制作可绑定(bind)标记扩展

转载 作者:行者123 更新时间:2023-12-04 15:24:38 26 4
gpt4 key购买 nike

你们都知道,没有内置的类型转换器类可以将字符串转换为 ImageSource 对象。

当我想让我的模型具有表示嵌入图像的 ResouceID 的字符串属性,然后 XAML 的 Image.Source 属性通过数据绑定(bind)绑定(bind)到该属性时,这给我带来了挑战。

最简单的解决方案是在模型中添加另一个 ImageSource 类型的属性,该属性仅获取基于字符串 ResourceID 属性生成的图像源对象,然后将其绑定(bind)到 Image.Source。

它运行良好,但问题是我必须将 Xamarin.Forms 依赖项添加到定义模型的项目中,该模型是 .NET Stardard 类库。

我按项目 Split View和模型/ View 模型,以便模型可以在其他应用程序中重复使用。因此,该模型被设计为只有基本类型,如 int、string 等。最简单的解决方案是使模型类依赖于 Xamarin.Forms,我担心稍后在我用于其他框架时是否会带来一些问题。

为了解决问题,我尝试在共享项目中通过引用MS文档声明一个具有绑定(bind)能力的标记扩展类,但没有成功。

扩展类代码如下。


// This class is added to the shared project, where a page class exsists.
public class BindableImageSourceExtension : BindableObject, IMarkupExtension
{
// This is a unique property of BindableImageSource element
public string ResourceID
{
get { return (string)GetValue(ResouceIDProperty); }
set { SetValue(ResouceIDProperty, value); }
}

public object ProvideValue(IServiceProvider serviceProvider)
{
if(ResourceID == null) return null;

// Images are embeded to a project named BusinessSession that holds Data class.
return ImageSource.FromResource(ResourceID, typeof(BusinessSession).GetTypeInfo().Assembly);
}

public static readonly BindableProperty ResouceIDProperty =
BindableProperty.Create("ResourceID", typeof(string),
typeof(BindableImageSourceExtension),
default(string));
}

有人可以帮忙吗?

最佳答案

无需创建bindableproperty,解决方案在document中列出:

Because there is no built-in type converter from string toResourceImageSource, these types of images cannot be natively loadedby XAML. Instead, a simple custom XAML markup extension can be writtento load images using a Resource ID specified in XAML:

[ContentProperty (nameof(Source))]
public class ImageResourceExtension : IMarkupExtension
{
public string Source { get; set; }

public object ProvideValue (IServiceProvider serviceProvider)
{
if (Source == null)
{
return null;
}

// Do your translation lookup here, using whatever method you require
var imageSource = ImageSource.FromResource(Source, typeof(ImageResourceExtension).GetTypeInfo().Assembly);

return imageSource;
}
}

在 Xaml 中:

 <StackLayout VerticalOptions="Center" HorizontalOptions="Center">
<!-- use a custom Markup Extension -->
<Image Source="{local:ImageResource WorkingWithImages.beach.jpg}" />
</StackLayout>

您还可以创建自己的ValueConverter:

public class stringToImageSourceConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return ImageSource.FromResource(value as string, typeof(MainPage).GetTypeInfo().Assembly);
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return value;
}
}

在 Xaml 中:

<Image Source="{Binding Path, Converter={StaticResource stringToImage}}" VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand" />

我上传了一个示例项目 here .

关于c# - 如何在 Xamarin.Forms 中制作可绑定(bind)标记扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62527852/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com