【问题标题】:MasterDetail + Right-to-Left in Xamarin.Forms v3Xamarin.Forms v3 中的 MasterDetail + 从右到左
【发布时间】:2018-10-27 18:52:11
【问题描述】:

我正在使用从右到左的新表单功能,除了 MasterDetail 汉堡菜单图标外,它运行良好。它停留在左侧,我需要在本地化发生更改时将其移动到右侧。有什么想法或有人可以帮助我使用自定义渲染器吗?

【问题讨论】:

  • 从图片来看,masterdetail的菜单好像是RTL。github.com/xamarin/Xamarin.Forms/pull/1222
  • @Ali123 查看顶部导航栏...图标在同一位置(例如箭头)...当它的 rtl 时它应该在右侧
  • 自定义渲染器有没有办法做到这一点?
  • everywere 我发现不可能(现在)希望他们能修复它
  • 现在有可能(我认为他们在最新更新之一中修复了它),只需添加所需的权限:blog.xamarin.com/right-to-left-localization-xamarin-forms 阅读“准备从右到左”部分

标签: xamarin.forms right-to-left master-detail


【解决方案1】:

不是不可能,但需要一些肮脏的编码:

please check the solution here

作为回顾: 在 IOS 上强制布局 RTL 和 LTR:

1- 创建此类/服务

using System;
using System.IO;
using Xamarin.Forms;
using yourproject.iOS;
using yourproject.database;
using ObjCRuntime;
using System.Runtime.InteropServices;
using UIKit;
using System.Diagnostics;

[assembly: Dependency(typeof(PathManager_IOS))]
namespace yourproject.iOS
{
 public class PathManager_IOS : IPathManager
 {
[DllImport(ObjCRuntime.Constants.ObjectiveCLibrary, EntryPoint = "objc_msgSend")]
internal extern static IntPtr IntPtr_objc_msgSend(IntPtr receiver, IntPtr selector, UISemanticContentAttribute arg1);

    public PathManager_IOS()
    {

    }
   public void SetLayoutRTL()
    {
        try
        {
            Selector selector = new Selector("setSemanticContentAttribute:");
            IntPtr_objc_msgSend(UIView.Appearance.Handle, selector.Handle, UISemanticContentAttribute.ForceRightToLeft);
        }
        catch (Exception s)
        {
            Debug.WriteLine("failed to set layout...."+s.Message.ToString());
        }
    }
    public void SetLayoutLTR()
    {
        try
        {
            Selector selector = new Selector("setSemanticContentAttribute:");
            IntPtr_objc_msgSend(UIView.Appearance.Handle, selector.Handle, UISemanticContentAttribute.ForceLeftToRight);
        }
        catch (Exception s)
        {
            Debug.WriteLine("failed to set layout...." + s.Message.ToString());
        }
    }
 }
} 

ps:请将“yourproject”更改为您的项目名称...

在启动时调用它

在 AppDelegate.cs 中

   PathManager_IOS pathManager = new PathManager_IOS();
   if (lang == 3)
    {
      pathManager.SetLayoutRTL();/* RTL */

    }
    if (lang == 1||lang == 2)
    {
       pathManager.SetLayoutLTR();/*   LTR   */
    }
    LoadApplication(new App(m, lang));

从 PCL 共享页面或项目中调用它

/* arabic */
DependencyService.Get<IPathManager>().SetLayoutRTL();

/* English */
DependencyService.Get<IPathManager>().SetLayoutLTR();

不要忘记在语言更改时设置流向

if(lang==3)
                {//arabic 
                    this.FlowDirection = FlowDirection.RightToLeft;
                    this.Master.FlowDirection= FlowDirection.RightToLeft;
                    this.Detail.FlowDirection= FlowDirection.RightToLeft;

                } 

希望这会有所帮助!这一切都是为了那个汉堡包图标!!!

干杯, 拉比

【讨论】:

    【解决方案2】:

    要强制 Android 导航栏成为 RTL,请在 Android 项目上使用 masterDetailPage 渲染器,如下所示:

    public class MyMasterDetailPageRenderer : MasterDetailPageRenderer
    {
        public MyMasterDetailPageRenderer(Context context) : base(context)
        {
    
    
        }
        protected override void OnLayout(bool changed, int l, int t, int r, int b)
        {
            base.OnLayout(changed, l, t, r, b);
            var toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
            toolbar.LayoutDirection = LayoutDirection.Rtl;
    
        }
    
    
    }
    

    【讨论】:

      【解决方案3】:

      第一步是设置主方向流

      <MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms"
                   xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                   x:Class="APPNAME.MainPage"
                        FlowDirection="RightToLeft">
      

      对于 Android,您需要将 android:supportsRtl="true" 添加到 AndroidManifest.xml 中

      <application android:label="APPNAME.Android" android:theme="@style/MainTheme" android:supportsRtl="true"></application>
      

      然后您的母版页是 rtl 以及操作栏。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-11
        • 2022-01-18
        • 2022-01-13
        • 1970-01-01
        相关资源
        最近更新 更多