【问题标题】:Is it possible to apply converter implicitly in Xamarin forms是否可以在 Xamarin 表单中隐式应用转换器
【发布时间】:2019-01-20 05:30:03
【问题描述】:

我有一个项目,它的资源文件夹中包含一堆图像。 但是现在我想通过将图像下载到存储中然后使用它来使用它们。但要做到这一点,我必须将转换器应用于所有图像。 是否可以将转换器隐式应用于所有图像。或者任何其他适合这里的方法?

public class ImageFileToImageSourceConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (!(value is string name)) return null;
          return ImageSource.FromFile(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), name));
    }

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

}

【问题讨论】:

    标签: c# visual-studio xaml xamarin xamarin.forms


    【解决方案1】:

    我不这么认为。

    您可以做的事情是创建自己的控件继承,始终在其中应用转换器并在其上创建可绑定的属性,以提供常规属性。

    【讨论】:

      【解决方案2】:

      是的,实际上你可以。在您的 App.xaml 中设置样式(或在代码中取决于它的位置或其他 ResourceDictionary)将样式设置为所有想要使用转换器作为源的图像(我假设这就是您想要做的)。

      【解决方案3】:

      我最终是这样使用的

        public class StorageImage : Image
      {
          public static string FolderName = "assest";
      
          public new object Source
          {
              get
              {
                  return (ImageSource)base.Source;
              }
              set
              {
                  base.Source = (ImageSource)ConvertFromInvariantString(value as string);
              }
          }
      
          public static object ConvertFromInvariantString(string value)
          {
              try
              {
                  if (string.IsNullOrEmpty(value)) return null;
                  return ImageSource.FromFile(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), FolderName, value));
              }
              catch (Exception ee)
              {
                  return null;
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-09-27
        • 1970-01-01
        • 2022-01-09
        • 1970-01-01
        • 2016-08-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多