首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Xamarin.Forms上将背景图像显示为tiles

在Xamarin.Forms上将背景图像显示为tiles
EN

Stack Overflow用户
提问于 2022-02-06 08:51:38
回答 1查看 175关注 0票数 0

我正在使用Xamarin.Forms为Android开发一个应用程序。我用的是扎马林。我想以瓷砖的形式显示背景。我准备了一张100×100像素的图像。\Android\MyApp\MyApp.Android\Resources\drawable\background.jpg

代码语言:javascript
复制
<?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就会被放大和显示。我想在瓷砖中反复显示它。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-02-07 08:22:45

代码语言:javascript
复制
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:

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71005555

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档