【问题标题】:Drawable not appearing on Button Xamarin Forms AndroidDrawable 未出现在 Button Xamarin Forms Android 上
【发布时间】:2020-03-09 10:28:22
【问题描述】:

Xamarin.Forms - Android。 我在 Android 平台上使用 Button Renderer 将图像添加到 Android.Widget.Button。 Image 文件存储在 Assets 文件夹中,而不是通常的 Drawable 文件夹中。 我在 XAML 中正常设置按钮的 ImageSource,然后在渲染器中获取图像位置并在资产文件夹中搜索它。 可绘制对象已正确创建并通过检查匹配的宽度和高度等来确认,但它只是没有显示在按钮本身上。 图像文件具有 AndroidAsset 的构建操作,但我也尝试了 AndroidResource,但它仍然无法正常工作。

FileImageSource fis = (FileImageSource)Element.ImageSource;
Stream stream = context.Assets.Open("images/" + fis.File);
Drawable d = Drawable.CreateFromStream(stream, null);
stream.Close();
Control.SetCompoundDrawablesWithIntrinsicBounds(null, d, null, null);

【问题讨论】:

    标签: c# android xamarin xamarin.forms xamarin.android


    【解决方案1】:

    您可以像下面的代码一样创建自定义渲染器。

    [assembly: ExportRenderer(typeof(Xamarin.Forms.Button), typeof(CustomButton.Droid.CustomButton))]
    namespace CustomButton.Droid
    {
       public class CustomButton:ButtonRenderer
        {
            Context mcontext;
            public CustomButton(Context context) : base(context)
            {
                mcontext = context;
            }
            protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e)
            {
                base.OnElementChanged(e);
    
                AssetManager assets = mcontext.Assets;
    
                Stream input = assets.Open("main.png");
    
                var mydraw=Drawable.CreateFromStream(input, null);
                Control.Background = mydraw;
            }
        }
    }
    

    在 xamarin 表单中使用它。

       <StackLayout>
        <!-- Place new controls here -->
        <Label Text="Welcome to Xamarin.Forms!" 
           HorizontalOptions="Center"
           VerticalOptions="CenterAndExpand" />
        <Button Text="Button"/>
    </StackLayout>
    

    这是运行截图。

    更新

    如果我使用Control.SetCompoundDrawablesWithIntrinsicBounds(null, mydraw, null, null);,它可以在文本上方显示图像。

     protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e)
            {
                base.OnElementChanged(e);
    
                AssetManager assets = mcontext.Assets;
    
                Stream input = assets.Open("person.jpg");
    
                var mydraw = Drawable.CreateFromStream(input, null);
                Control.SetCompoundDrawablesWithIntrinsicBounds(null, mydraw, null, null);
            }
    

    这是运行截图。

    【讨论】:

    • 不想将背景设置为图像,因为我想为此使用可绘制的波纹背景。该图像是位于波纹背景顶部的图标。我通过替换自定义按钮视图上的默认 ImageSource 属性来修复它,这允许我使用上面的代码。我想问题是 XF 试图添加图像但未能在 drawables 文件夹中找到该文件,然后不允许我在该点之后设置 drawable。因此,通过替换 ImageSource 属性,XF 最初并没有尝试设置图像,我自己可以毫无问题地设置它。谢谢!
    • @RussellCorbin 我更新了我的答案,我设置了Control.SetCompoundDrawablesWithIntrinsicBounds(null, mydraw, null, null); 它可以在文本上方显示图像。如果回复有帮助,请将其标记为答案,它将帮助其他有类似问题的人。
    • @RussellCorbin 这个问题有更新吗?如果回复有帮助,请将其标记为答案,这将对有类似问题的其他人有所帮助
    • 你基本上只是复制了我的代码。问题是在 xaml 中设置图像源时。这会导致您的代码无法正常工作。
    • 我已经发布了答案。使用了我的原始渲染器代码,但我创建了一个自定义按钮视图并将 ImageSource 属性替换为字符串。
    【解决方案2】:
    public class ButtonView : Button {
    
        public new static readonly BindableProperty ImageSourceProperty = BindableProperty.Create(nameof(ImageSource), typeof(string), typeof(ButtonView), null);
            public new string ImageSource {
                get { return (string)GetValue(ImageSourceProperty); }
                set { SetValue(ImageSourceProperty, value); }
            }
        }
    }
    

    然后在渲染器上:

    protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) {
        base.OnElementPropertyChanged(sender, e);
        if(e.PropertyName == ButtonView.ImageSourceProperty.PropertyName) {
            SetImage();
        }
    }
    
    private void SetImage() {
        if (Element is ButtonView btn){
            Stream stream = context.Assets.Open("images/" + btn.ImageSource);
            Drawable d = Drawable.CreateFromStream(stream, null);
            stream.Close();
            Control.SetCompoundDrawablesWithIntrinsicBounds(null, d, null, null);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-09-28
      • 2020-01-15
      • 1970-01-01
      • 1970-01-01
      • 2019-02-10
      • 1970-01-01
      • 2021-04-20
      • 2019-10-31
      • 1970-01-01
      相关资源
      最近更新 更多