Xamarin.Forms v3 中的 MasterDetail + 从右到左
MasterDetail + Right-to-Left in Xamarin.Forms v3
我正在使用从右到左的新表单功能,除了 MasterDetail 汉堡包菜单图标外,它工作正常。它留在左侧,我需要在更改本地化时将其移动到右侧。任何想法或有人可以帮助我使用自定义渲染器吗?
并非不可能,但需要一些肮脏的编码:
please check the solution here
回顾一下:
要在 IOS 上强制布局 RTL 和 LTR:
1- 创建这个 class/service
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;
}
希望这个帮助ps!所有这一切都是为了那个汉堡图标!
干杯,
拉比
要强制 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;
}
}
第一步设置主方向流
<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 以及操作栏。
我正在使用从右到左的新表单功能,除了 MasterDetail 汉堡包菜单图标外,它工作正常。它留在左侧,我需要在更改本地化时将其移动到右侧。任何想法或有人可以帮助我使用自定义渲染器吗?
并非不可能,但需要一些肮脏的编码:
please check the solution here
回顾一下: 要在 IOS 上强制布局 RTL 和 LTR:
1- 创建这个 class/service
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;
}
希望这个帮助ps!所有这一切都是为了那个汉堡图标!
干杯, 拉比
要强制 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;
}
}
第一步设置主方向流
<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 以及操作栏。