【问题标题】:Loading FFImageLoading SVG into ImageButton Source from code?从代码中将 FFImageLoading SVG 加载到 ImageButton Source 中?
【发布时间】:2019-01-17 21:07:25
【问题描述】:

这个post 展示了如何使 SvgCachedImage 像一个按钮一样工作。但是,如何将 SvgCachedImage 加载到 XamarinForm 的 ImageButton Source 中?

我的非工作代码:

<?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:local="clr-namespace:SharedSvgSample"
             x:Class="SharedSvgSample.MainPage"
             xmlns:ffimageloadingsvg="clr-namespace:FFImageLoading.Svg.Forms;assembly=FFImageLoading.Svg.Forms">
    <StackLayout VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand">
        <ImageButton
            x:Name="myButton"
            HeightRequest="200"
            Clicked="myButton_Clicked"
            WidthRequest="200" />
    </StackLayout>
</ContentPage>

代码隐藏:

using FFImageLoading.Svg.Forms;
using System;
using Xamarin.Forms;

namespace SharedSvgSample
{
    public partial class MainPage : ContentPage
    {
        private bool _myButtonValue;
        private SvgImageSource _visibilityOn = null;
        private SvgImageSource _visibilityOff = null;

        public MainPage()
        {
            InitializeComponent();
            _visibilityOn = SvgImageSource.FromResource("SharedSvgSample.Resources.visibility_on.svg");
            _visibilityOn.VectorHeight = 100;
            _visibilityOn.VectorWidth = 100;

            _visibilityOff = SvgImageSource.FromResource("SharedSvgSample.Resources.visibility_off.svg");
            _visibilityOff.VectorHeight = 100;
            _visibilityOff.VectorWidth = 100;

            myButton.Source = _visibilityOff;   
        }

        private void myButton_Clicked(object sender, EventArgs e)
        {
            _myButtonValue = !_myButtonValue;
            myButton.Source = _myButtonValue ? _visibilityOn.ImageSource : _visibilityOff.ImageSource;
        }
    }
}

【问题讨论】:

  • 据我所知,FFImageLoading SVG和Image button是不同的控件,只能设置ImageButton图片源。为什么不能使用 FFImageLoading SVG 和点击手势事件?
  • 我很困惑,这是两个不同的控件,你到底想在这里实现什么
  • @G.hakim 我正在尝试让 ImageButton 加载 SVG 图标。
  • @ДенисЧорный ,使用带有点击手势事件的 FFImageLoading SVG,我失去了 ImageButton 的功能(例如设置圆角边框等)。我正在寻找的解决方法是让 FFImageLoading 在内存中预渲染 ImageButton 可以接受的图像。
  • 好吧,我不知道你能不能做到这一点,我很确定你不能,但祝你好运

标签: xamarin.forms ffimageloading


【解决方案1】:

不幸的是,Xamarin.Forms.Button 仅支持 FileImageSource,因此目前您不能将 SVG 加载到按钮图像中。

但是,您可以只加载 SVG 图像,然后添加 TapGestureRecognizer 来模拟按钮。

【讨论】:

  • 我指的是Xamarin.Forms.ImageButton。我正在研究让 FFImageLoading 将内存中的 svg 图标预渲染为 ImageButton 可以接受的图像的解决方法。
  • @JesonMartajaya 你找到解决方案了吗?
猜你喜欢
  • 1970-01-01
  • 2018-06-15
  • 2022-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-09
相关资源
最近更新 更多