UWP / XAML:使Grid内的TextBlock自动缩小

时间:2018-04-11 23:11:33

标签: uwp uwp-xaml

我有一个包含几个TextBlocks的网格:

// From https://github.com/serilog-web/owin/blob/master/src/SerilogWeb.Owin/Owin/LoggerFactory.cs

// Copyright 2015 SerilogWeb, Serilog Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using System.Diagnostics;
using System.Linq;
using Microsoft.Owin.Logging;
using Serilog;
using Serilog.Core;
using Serilog.Events;
using ILogger = Serilog.ILogger;

namespace SerilogWeb.Owin
{
    /// <summary>
    /// Implementation of Microsoft.Owin.Logger.ILoggerFactory.
    /// </summary>
    public class SerilogOwinFactory : ILoggerFactory
    {
        readonly Func<ILogger> _getLogger;
        readonly Func<TraceEventType, LogEventLevel> _getLogEventLevel;

        /// <summary>
        /// Create a logger factory.
        /// </summary>
        /// <param name="logger">The logger; if not provided the global <see cref="Serilog.Log.Logger"/> will be used.</param>
        /// <param name="getLogEventLevel"></param>
        public SerilogOwinFactory(ILogger logger = null, Func<TraceEventType, LogEventLevel> getLogEventLevel = null)
        {
            _getLogger = logger == null ? (Func<ILogger>)(() => Log.Logger) : (() => logger);
            _getLogEventLevel = getLogEventLevel ?? ToLogEventLevel;
        }

        /// <summary>
        /// Creates a new ILogger instance of the given name.
        /// </summary>
        /// <param name="name">The logger context name.</param>
        /// <returns>A logger instance.</returns>
        public Microsoft.Owin.Logging.ILogger Create(string name)
        {
            return new Logger(_getLogger().ForContext(Constants.SourceContextPropertyName, name), _getLogEventLevel);
        }

        static LogEventLevel ToLogEventLevel(TraceEventType traceEventType)
        {
            switch (traceEventType)
            {
                case TraceEventType.Critical:
                    return LogEventLevel.Fatal;
                case TraceEventType.Error:
                    return LogEventLevel.Error;
                case TraceEventType.Warning:
                    return LogEventLevel.Warning;
                case TraceEventType.Information:
                    return LogEventLevel.Information;
                case TraceEventType.Verbose:
                    return LogEventLevel.Verbose;
                case TraceEventType.Start:
                    return LogEventLevel.Debug;
                case TraceEventType.Stop:
                    return LogEventLevel.Debug;
                case TraceEventType.Suspend:
                    return LogEventLevel.Debug;
                case TraceEventType.Resume:
                    return LogEventLevel.Debug;
                case TraceEventType.Transfer:
                    return LogEventLevel.Debug;
                default:
                    throw new ArgumentOutOfRangeException("traceEventType");
            }
        }

        class Logger : Microsoft.Owin.Logging.ILogger
        {
            readonly ILogger _logger;
            readonly Func<TraceEventType, LogEventLevel> _getLogEventLevel;
            static readonly Exception _exceptionPlaceHolder = new Exception("(Exception enclosed)");

            internal Logger(ILogger logger, Func<TraceEventType, LogEventLevel> getLogEventLevel)
            {
                _logger = logger;
                _getLogEventLevel = getLogEventLevel;
            }

            public bool WriteCore(TraceEventType eventType, int eventId, object state, Exception exception, Func<object, Exception, string> formatter)
            {
                var level = _getLogEventLevel(eventType);

                // According to docs http://katanaproject.codeplex.com/SourceControl/latest#src/Microsoft.Owin/Logging/ILogger.cs
                // "To check IsEnabled call WriteCore with only TraceEventType and check the return value, no event will be written."
                if (state == null)
                    return _logger.IsEnabled(level);
                if (!_logger.IsEnabled(level))
                    return false;
                var formattedMessage = formatter(state, null); // Omit exception as we're including it in the LogEvent
                var template = new Serilog.Events.MessageTemplate(new[] { new Serilog.Parsing.TextToken(formattedMessage) });
                var logEvent = new Serilog.Events.LogEvent(DateTimeOffset.Now, level, exception, template, Enumerable.Empty<Serilog.Events.LogEventProperty>());
                _logger.ForContext("eventId", eventId).Write(logEvent);
                return true;
            }
        }
    }
}

目前,这表现出两种不良特性:

  1. 当文本太长时,会使用省略号截断。 (我看到这种行为是由TextTrimming属性指定的。我应该将它设置为&#34;无&#34;以获得自动缩小的行为?)
  2. 当网格变得太窄(比如缩小窗口)时,它会剪切文本的边缘而不是缩小它。
  3. 我希望文本在长度过长时缩小以适应,并且如果空间变窄则缩小。似乎应该有一个简单的方法来做到这一点,但到目前为止我的谷歌搜索已经提出使用Grid(已经这样做)和Viewbox(在我的测试中没有做任何事情)的建议。

    在UWP开发方面,我是个孩子,我把大部分时间花在iOS上,所以请原谅这个愚蠢的问题。

1 个答案:

答案 0 :(得分:1)

  

当文本太长时,它会被省略号截断。 (我看到这种行为是由TextTrimming属性指定的。我应该将它设置为“None”以获得自动缩小的行为吗?)

TextTrimming属性用于获取或设置内容溢出内容区域时要使用的文本修剪行为。

  

CharacterEllipsis介绍于。文本在字符边界处被修剪。绘制省略号(...)代替剩余文本。

     

Clip   介绍。文本在像素级别进行修剪,在视觉上剪切多余的字形。

     

None   文字未被修剪。

     

WordEllipsis   文本在单词边界处被修剪。绘制省略号(...)代替剩余文本。

如果您设置None,则不会裁剪文字。并且TextWrapping用于换行长词。但是,它可以用于缩小文本。目前,有一种解决方法可以将Textblock插入ViewboxTextBolck将缩放整个内容以适合父级。

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Grid.RowDefinitions>
        <RowDefinition Height="1*"/>
        <RowDefinition Height="auto"/>
    </Grid.RowDefinitions>
    <Viewbox Grid.Row="0" Stretch="Uniform"  Width="200" Height="50">
        <TextBlock  Text="I would like the text "   />
    </Viewbox>

    <Viewbox Grid.Row="1" Stretch="Uniform"  Width="200" Height="50">
        <TextBlock  Text="I'm a baby when it comes to UWP development, ."   />
    </Viewbox>
</Grid>

enter image description here