Windows 普遍支持从右到左
Windows universal Right to Left support
有必要支持从右到左的风格(文本和layout-s)。我了解当您在所有 child 控件中设置 parent Grid
的属性 FlowDirection = "RightToLeft"
时,它会继承。
问题是 - 是否有任何默认设置,这将改变我们在应用程序中所需的一切?或者我应该设置每个 parent 贪婪 FlowDirection
由某个旗帜国王设置这个旗帜为 FlowDirection = "RightToLeft"
如果我们,例如在阿拉伯国家?
如果您要支持任何从右到左的语言,则也需要有从右到左的布局。您不需要更改所有元素的 FlowDirection 属性,因为它由子元素继承。
MSDN:
An object inherits the FlowDirection value from its parent in the object tree. Any element can override the value it gets from its parent. If not specified, the default FlowDirection is LeftToRight
所以通常你需要为 Window 的根 element/frame 设置一次 属性。
但是,FontIcon and Image 等某些元素不会自动镜像。 FontIcon 有一个 MirroredWhenRightToLeft 属性:
You can set the MirroredWhenRightToLeft property to have the glyph appear mirrored when the FlowDirection is RightToLeft. You typically use this property when a FontIcon is used to display an icon as part of a control template and the icon needs to be mirrored along with the rest of the control
对于Image,需要通过transforms来翻转图像。
编辑:
您可以在创建主 frame/page 的应用程序 class 中设置 属性:
// Part of the App.xaml.cs in default UWP project template:
protected override void OnLaunched(LaunchActivatedEventArgs e) {
#if DEBUG
if (System.Diagnostics.Debugger.IsAttached) {
this.DebugSettings.EnableFrameRateCounter = true;
}
#endif
Frame rootFrame = Window.Current.Content as Frame;
// Do not repeat app initialization when the Window already has content,
// just ensure that the window is active
if (rootFrame == null) {
// Create a Frame to act as the navigation context and navigate to the first page
rootFrame = new Frame();
rootFrame.NavigationFailed += OnNavigationFailed;
if (e.PreviousExecutionState == ApplicationExecutionState.Terminated) {
//TODO: Load state from previously suspended application
}
//**********************
// Set flow direction
// *********************
if (System.Globalization.CultureInfo.CurrentCulture.TextInfo.IsRightToLeft) {
rootFrame.FlowDirection = FlowDirection.RightToLeft;
}
// Place the frame in the current Window
Window.Current.Content = rootFrame;
}
...
...
如果你不想使用code behind(我觉得用在这种场景下没问题),你可以实现IValueConverter(不推荐):
public class RightToLeftConverter : IValueConverter {
public object Convert(object value, Type targetType,
object parameter, string language) {
if (System.Globalization.CultureInfo.CurrentCulture.TextInfo.IsRightToLeft) {
return FlowDirection.RightToLeft;
}
return FlowDirection.LeftToRight;
}
public object ConvertBack(object value, Type targetType,
object parameter, string language)
{
throw new NotImplementedException();
}
}
并在XAML中使用它:
<Page
...
...
FlowDirection="{Binding Converter={StaticResource RightToLeftConverter}}">