【问题标题】:Display the background image as tiles at Xamarin.Forms在 Xamarin.Forms 中将背景图像显示为磁贴
【发布时间】:2022-02-07 16:22:45
【问题描述】:

我正在使用 Xamarin.Forms 开发适用于 Android 的应用程序。 我正在使用 Xamarin。 我想将背景显示为瓷砖。 我准备了一个 100x100 像素的图像。 \Android\MyApp\MyApp.Android\Resources\drawable\background.jpg

<?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:d="http://xamarin.com/schemas/2014/forms/design"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:viewmodels="clr-namespace:MyApp.ViewModels"
    x:DataType="viewmodels:LoginViewModel"
    mc:Ignorable="d"
    x:Class="MyApp.Views.LoginPage"
    Shell.NavBarIsVisible="False"
    BackgroundImage="background.jpg"
>

这样,background.jpg 会被放大显示。 我想在图块中重复显示它。

【问题讨论】:

    标签: xamarin.forms background-image


    【解决方案1】:
    You can display it repeatedly in tiles through below steps.
    1.Create a drawable xml file on your Android project:
        <?xml version="1.0" encoding="utf-8" ?>
        <bitmap  xmlns:android="http://schemas.android.com/apk/res/android" 
                 android:tileMode="repeat"
                 android:src="@drawable/clock" >
        </bitmap>
    2.After that create below two effect classes on your shared project:
        //base class
        namespace AppTiles
    {
        public class BaseEffect : RoutingEffect
        {
            public const string EffectNamespace = "AppTiles";
    
            public BaseEffect(String effectId) : base($"{EffectNamespace}. 
     {effectId}")
            {
            }
        }
    }
    //effect class
        namespace AppTiles
    {
        public class CoverEffect : BaseEffect
        {
            public CoverEffect() : base(nameof(CoverEffect))
            {
            }
        }
    }
    3.Create a effect class in your Android project
    [assembly: ResolutionGroupName("AppTiles") ]
    [assembly: ExportEffect(typeof(AppTiles.Droid.CoverEffect), 
    nameof(CoverEffect))]
    
    
    namespace AppTiles.Droid
    {
        public class CoverEffect : PlatformEffect
        {
            protected override void OnAttached()
            {
                UpdateBackground();
            }
    
            protected override void OnDetached()
            {
            }
    
            private void UpdateBackground()
            {
                Android.Views.View mView = Container as Android.Views.View;
    
                if (mView != null)
                {
                    mView.Background = ContextCompat.GetDrawable(mView.Context, Resource.Drawable.XMLFile1);
                }
            }
        }
    }
    4.Add below xmal in shared main page code behind:
      <StackLayout Orientation="Vertical" Spacing="0">
    
            <StackLayout.Effects>
                <effects:CoverEffect/>
            </StackLayout.Effects>
    
        </StackLayout>
    
    Below is the screenshot:
    

    【讨论】:

    • 感谢您的步骤。但是,它没有按预期工作。我无法让瓷砖工作,它停留在放大的视图中。如果你能在 GitHub 上给我一个完整的项目,我将不胜感激。我明天会报告我采取的步骤。
    • 感谢您的回复!请您帮忙确认一下您这边的图片尺寸是否为 100*100,因为我的图片尺寸为 96*96,并且可以正常工作。另外,由于一些我这边有限制,能否请您在 Github 上提供您的项目,以便我检查您的代码?谢谢!
    • 1.在你的Android项目上创建一个可绘制的xml文件:我创建了MyApp\MyApp\MyApp.Android\Resources\drawable\drawable.xml schemas.android.com/apk/res/android" android:tileMode="repeat" android:src="@drawable/background" >
    • 对不起。我的意思是你需要提供 Github 项目链接。
    • 我检查了你的项目,找到了根本原因,你只需要更改这行代码 mView.Background = ContextCompat.GetDrawable(mView.Context, Resource.Drawable.carbon_fiber); to mView.Background = ContextCompat.GetDrawable(mView.Context, Resource.Drawable.drawable);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-22
    • 2017-01-31
    • 2021-02-26
    • 2011-02-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多