jjzjj

c# - 带有标记扩展名的字符串格式

coder 2024-05-28 原文

我正在尝试将 string.Format 用作 WPF 中的一个方便的函数,以便可以在纯 XAML 中组合各种文本部分,而无需代码隐藏中的样板。主要问题是支持函数的参数来自其他嵌套标记扩展(例如 Binding )的情况。

实际上,有一个功能非常接近我需要的功能: MultiBinding 。不幸的是,它只能接受绑定(bind),而不能接受其他动态类型的内容,例如 DynamicResource s。

如果我所有的数据源都是绑定(bind),我可以使用这样的标记:

<TextBlock>
    <TextBlock.Text>
        <MultiBinding Converter="{StaticResource StringFormatConverter}">
            <Binding Path="FormatString"/>
            <Binding Path="Arg0"/>
            <Binding Path="Arg1"/>
            <!-- ... -->
        </MultiBinding>
    </TextBlock.Text>
</TextBlock>

明显实现 StringFormatConveter

我试图实现一个自定义标记扩展,以便语法是这样的:
<TextBlock>
    <TextBlock.Text>
        <l:StringFormat Format="{Binding FormatString}">
            <DynamicResource ResourceKey="ARG0ID"/>
            <Binding Path="Arg1"/>
            <StaticResource ResourceKey="ARG2ID"/>
        </MultiBinding>
    </TextBlock.Text>
</TextBlock>

或者也许只是
<TextBlock Text="{l:StringFormat {Binding FormatString},
                  arg0={DynamicResource ARG0ID},
                  arg1={Binding Arg2},
                  arg2='literal string', ...}"/>

但是对于参数是另一个标记扩展的情况,我坚持 ProvideValue(IServiceProvider serviceProvider) 的实现。

互联网上的大多数示例都非常简单:它们要么根本不使用 serviceProvider,要么查询 IProvideValueTarget ,它(主要)说明了标记扩展的目标是什么依赖属性。在任何情况下,代码都知道应该在 ProvideValue 调用时提供的值。但是, ProvideValue 只会被调用一次( except for templates ,这是一个单独的故事),因此如果实际值不是恒定的(例如 Binding 等),则应使用另一种策略。

我查了一下 Reflector 中 Binding 的实现,它的 ProvideValue 方法实际上返回的不是真正的目标对象,而是 System.Windows.Data.BindingExpression 类的一个实例,它似乎做了所有的实际工作。关于 DynamicResource 也是如此:它只返回 System.Windows.ResourceReferenceExpression 的一个实例,它关心订阅(内部) InheritanceContextChanged 并在适当的时候使该值无效。但是,通过查看代码,我无法理解以下内容:
  • 类型 BindingExpression/ResourceReferenceExpression 的对象未被“按原样”处理,而是被要求提供基础值,这是怎么回事?
  • MultiBindingExpression 如何知道底层绑定(bind)的值发生了变化,因此它也必须使其值失效?

  • 我实际上发现了一个标记扩展库实现,它声称支持连接字符串(它完美地映射到我的用例)( projectcodeconcatenation implementation 依赖于 other code ),但它似乎只支持嵌套的库类型扩展(即,您不能在内部嵌套 Vanilla Binding)。

    有没有办法实现问题顶部提供的语法?它是受支持的场景,还是只能从 WPF 框架内部执行此操作(因为 System.Windows.Expression 具有内部构造函数)?

    实际上,我使用自定义的不可见帮助器 UI 元素实现了所需的语义:
    <l:FormatHelper x:Name="h1" Format="{DynamicResource FORMAT_ID'">
        <l:FormatArgument Value="{Binding Data1}"/>
        <l:FormatArgument Value="{StaticResource Data2}"/>
    </l:FormatHelper>
    <TextBlock Text="{Binding Value, ElementName=h1}"/>
    

    (其中 FormatHelper 跟踪其子项及其依赖项属性更新,并将最新结果存储到 Value 中),但这种语法似乎很难看,我想摆脱可视化树中的辅助项。

    最终目标是促进翻译:像“15 秒直到爆炸”这样的 UI 字符串自然地表示为可本地化的格式“{0}直到爆炸”(进入 ResourceDictionary 并在语言改变时将被替换)和 Binding 到表示时间的 VM 依赖属性。

    更新报告 :我尝试使用我在互联网上可以找到的所有信息自己实现标记扩展。完整的实现在这里( [1][2][3] ),这里是核心部分:
    var result = new MultiBinding()
    {
        Converter = new StringFormatConverter(),
        Mode = BindingMode.OneWay
    };
    
    foreach (var v in values)
    {
        if (v is MarkupExtension)
        {
            var b = v as Binding;
            if (b != null)
            {
                result.Bindings.Add(b);
                continue;
            }
    
            var bb = v as BindingBase;
            if (bb != null)
            {
                targetObjFE.SetBinding(AddBindingTo(targetObjFE, result), bb);
                continue;
            }
        }
    
        if (v is System.Windows.Expression)
        {
            DynamicResourceExtension mex = null;
            // didn't find other way to check for dynamic resource
            try
            {
                // rrc is a new ResourceReferenceExpressionConverter();
                mex = (MarkupExtension)rrc.ConvertTo(v, typeof(MarkupExtension))
                    as DynamicResourceExtension;
            }
            catch (Exception)
            {
            }
            if (mex != null)
            {
                targetObjFE.SetResourceReference(
                        AddBindingTo(targetObjFE, result),
                        mex.ResourceKey);
                continue;
            }
        }
    
        // fallback
        result.Bindings.Add(
            new Binding() { Mode = BindingMode.OneWay, Source = v });
    }
    
    return result.ProvideValue(serviceProvider);
    

    这似乎适用于嵌套绑定(bind)和动态资源,但在尝试将其嵌套在自身中时失败了,因为在这种情况下,从 targetObj 获得的 IProvideValueTargetnull 。我试图通过将嵌套绑定(bind)合并到外部绑定(bind)( [1a][2a] )来解决这个问题(将多绑定(bind)溢出添加到外部绑定(bind)中),这可能适用于嵌套的多绑定(bind)和格式扩展,但仍然无法使用嵌套的动态资源。

    有趣的是,当嵌套不同类型的标记扩展时,我在外部扩展中得到 Binding s 和 MultiBinding s,但是 ResourceReferenceExpression 而不是 DynamicResourceExtension 。我想知道为什么它不一致(以及如何从 Binding 重建 BindingExpression )。

    更新报告 :不幸的是,答案中给出的想法并没有带来问题的解决方案。也许这证明了标记扩展虽然是非常强大和通用的工具,但需要 WPF 团队更多的关注。

    无论如何,我感谢任何参与讨论的人。提出的部分解决方案足够复杂,值得更多的赞成。

    更新报告 :标记扩展似乎没有好的解决方案,或者至少创建标记扩展所需的 WPF 知识水平太深而不实用。

    然而,@adabyron 有一个改进的想法,这有助于隐藏宿主项目中的辅助元素(然而,这样做的代价是对宿主进行子类化)。我会尝试看看是否有可能摆脱子类化(使用劫持主机的 LogicalChildren 并在我的脑海中添加辅助元素的行为,灵感来自同一答案的旧版本)。

    最佳答案

    看看以下是否适合你。我拿了test case您在评论中提供并稍微扩展以更好地说明该机制。我想关键是通过使用 DependencyProperties 来保持灵活性。在嵌套容器中。



    编辑 :我已经用 TextBlock 的子类替换了混合行为。这为 DataContext 和 DynamicResources 添加了更简单的链接。

    在旁注中,您的项目使用的方式 DynamicResources引入条件不是我推荐的。而是尝试使用 ViewModel 来建立条件,和/或使用触发器。

    Xml:

    <UserControl x:Class="WpfApplication1.Controls.ExpiryView" xmlns:system="clr-namespace:System;assembly=mscorlib" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
                     xmlns:props="clr-namespace:WpfApplication1.Properties" xmlns:models="clr-namespace:WpfApplication1.Models"
                     xmlns:h="clr-namespace:WpfApplication1.Helpers" xmlns:c="clr-namespace:WpfApplication1.CustomControls"
                     Background="#FCF197" FontFamily="Segoe UI"
                     TextOptions.TextFormattingMode="Display">    <!-- please notice the effect of this on font fuzzyness -->
    
        <UserControl.DataContext>
            <models:ExpiryViewModel />
        </UserControl.DataContext>
        <UserControl.Resources>
            <system:String x:Key="ShortOrLongDateFormat">{0:d}</system:String>
        </UserControl.Resources>
        <Grid>
            <StackPanel>
                <c:TextBlockComplex VerticalAlignment="Center" HorizontalAlignment="Center">
                    <c:TextBlockComplex.Content>
                        <h:StringFormatContainer StringFormat="{x:Static props:Resources.ExpiryDate}">
                            <h:StringFormatContainer.Values>
                                <h:StringFormatContainer Value="{Binding ExpiryDate}" StringFormat="{DynamicResource ShortOrLongDateFormat}" />
                                <h:StringFormatContainer Value="{Binding SecondsToExpiry}" />
                            </h:StringFormatContainer.Values>
                        </h:StringFormatContainer>
                    </c:TextBlockComplex.Content>
                </c:TextBlockComplex>
            </StackPanel>
        </Grid>
    </UserControl>
    

    文本块复合体:
    using System;
    using System.Collections;
    using System.Collections.Specialized;
    using System.ComponentModel;
    using System.Windows;
    using System.Windows.Controls;
    using WpfApplication1.Helpers;
    
    namespace WpfApplication1.CustomControls
    {
        public class TextBlockComplex : TextBlock
        {
            // Content
            public StringFormatContainer Content { get { return (StringFormatContainer)GetValue(ContentProperty); } set { SetValue(ContentProperty, value); } }
            public static readonly DependencyProperty ContentProperty = DependencyProperty.Register("Content", typeof(StringFormatContainer), typeof(TextBlockComplex), new PropertyMetadata(null));
    
            private static readonly DependencyPropertyDescriptor _dpdValue = DependencyPropertyDescriptor.FromProperty(StringFormatContainer.ValueProperty, typeof(StringFormatContainer));
            private static readonly DependencyPropertyDescriptor _dpdValues = DependencyPropertyDescriptor.FromProperty(StringFormatContainer.ValuesProperty, typeof(StringFormatContainer));
            private static readonly DependencyPropertyDescriptor _dpdStringFormat = DependencyPropertyDescriptor.FromProperty(StringFormatContainer.StringFormatProperty, typeof(StringFormatContainer));
            private static readonly DependencyPropertyDescriptor _dpdContent = DependencyPropertyDescriptor.FromProperty(TextBlockComplex.ContentProperty, typeof(StringFormatContainer));
    
            private EventHandler _valueChangedHandler;
            private NotifyCollectionChangedEventHandler _valuesChangedHandler;
    
            protected override IEnumerator LogicalChildren { get { yield return Content; } }
    
            static TextBlockComplex()
            {
                // take default style from TextBlock
                DefaultStyleKeyProperty.OverrideMetadata(typeof(TextBlockComplex), new FrameworkPropertyMetadata(typeof(TextBlock)));
            }
    
            public TextBlockComplex()
            {
                _valueChangedHandler = delegate { AddListeners(this.Content); UpdateText(); };
                _valuesChangedHandler = delegate { AddListeners(this.Content); UpdateText(); };
    
                this.Loaded += TextBlockComplex_Loaded;
            }
    
            void TextBlockComplex_Loaded(object sender, RoutedEventArgs e)
            {
                OnContentChanged(this, EventArgs.Empty); // initial call
    
                _dpdContent.AddValueChanged(this, _valueChangedHandler);
                this.Unloaded += delegate { _dpdContent.RemoveValueChanged(this, _valueChangedHandler); };
            }
    
            /// <summary>
            /// Reacts to a new topmost StringFormatContainer
            /// </summary>
            private void OnContentChanged(object sender, EventArgs e)
            {
                this.AddLogicalChild(this.Content); // inherits DataContext
                _valueChangedHandler(this, EventArgs.Empty);
            }
    
            /// <summary>
            /// Updates Text to the Content values
            /// </summary>
            private void UpdateText()
            {
                this.Text = Content.GetValue() as string;
            }
    
            /// <summary>
            /// Attaches listeners for changes in the Content tree
            /// </summary>
            private void AddListeners(StringFormatContainer cont)
            {
                // in case they have been added before
                RemoveListeners(cont);
    
                // listen for changes to values collection
                cont.CollectionChanged += _valuesChangedHandler;
    
                // listen for changes in the bindings of the StringFormatContainer
                _dpdValue.AddValueChanged(cont, _valueChangedHandler);
                _dpdValues.AddValueChanged(cont, _valueChangedHandler);
                _dpdStringFormat.AddValueChanged(cont, _valueChangedHandler);
    
                // prevent memory leaks
                cont.Unloaded += delegate { RemoveListeners(cont); };
    
                foreach (var c in cont.Values) AddListeners(c); // recursive
            }
    
            /// <summary>
            /// Detaches listeners
            /// </summary>
            private void RemoveListeners(StringFormatContainer cont)
            {
                cont.CollectionChanged -= _valuesChangedHandler;
    
                _dpdValue.RemoveValueChanged(cont, _valueChangedHandler);
                _dpdValues.RemoveValueChanged(cont, _valueChangedHandler);
                _dpdStringFormat.RemoveValueChanged(cont, _valueChangedHandler);
            }
        }
    }
    

    字符串格式容器:
    using System.Linq;
    using System.Collections;
    using System.Collections.ObjectModel;
    using System.Windows;
    
    namespace WpfApplication1.Helpers
    {
        public class StringFormatContainer : FrameworkElement
        {
            // Values
            private static readonly DependencyPropertyKey ValuesPropertyKey = DependencyProperty.RegisterReadOnly("Values", typeof(ObservableCollection<StringFormatContainer>), typeof(StringFormatContainer), new FrameworkPropertyMetadata(new ObservableCollection<StringFormatContainer>()));
            public static readonly DependencyProperty ValuesProperty = ValuesPropertyKey.DependencyProperty;
            public ObservableCollection<StringFormatContainer> Values { get { return (ObservableCollection<StringFormatContainer>)GetValue(ValuesProperty); } }
    
            // StringFormat
            public static readonly DependencyProperty StringFormatProperty = DependencyProperty.Register("StringFormat", typeof(string), typeof(StringFormatContainer), new PropertyMetadata(default(string)));
            public string StringFormat { get { return (string)GetValue(StringFormatProperty); } set { SetValue(StringFormatProperty, value); } }
    
            // Value
            public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(object), typeof(StringFormatContainer), new PropertyMetadata(default(object)));
            public object Value { get { return (object)GetValue(ValueProperty); } set { SetValue(ValueProperty, value); } }
    
            public StringFormatContainer()
                : base()
            {
                SetValue(ValuesPropertyKey, new ObservableCollection<StringFormatContainer>());
                this.Values.CollectionChanged += OnValuesChanged;
            }
    
            /// <summary>
            /// The implementation of LogicalChildren allows for DataContext propagation.
            /// This way, the DataContext needs only be set on the outermost instance of StringFormatContainer.
            /// </summary>
            void OnValuesChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
            {
                if (e.NewItems != null)
                {
                    foreach (var value in e.NewItems)
                        AddLogicalChild(value);
                }
                if (e.OldItems != null)
                {
                    foreach (var value in e.OldItems)
                        RemoveLogicalChild(value);
                }
            }
    
            /// <summary>
            /// Recursive function to piece together the value from the StringFormatContainer hierarchy
            /// </summary>
            public object GetValue()
            {
                object value = null;
                if (this.StringFormat != null)
                {
                    // convention: if StringFormat is set, Values take precedence over Value
                    if (this.Values.Any())
                        value = string.Format(this.StringFormat, this.Values.Select(v => (object)v.GetValue()).ToArray());
                    else if (Value != null)
                        value = string.Format(this.StringFormat, Value);
                }
                else
                {
                    // convention: if StringFormat is not set, Value takes precedence over Values
                    if (Value != null)
                        value = Value;
                    else if (this.Values.Any())
                        value = string.Join(string.Empty, this.Values);
                }
                return value;
            }
    
            protected override IEnumerator LogicalChildren
            {
                get
                {
                    if (Values == null) yield break;
                    foreach (var v in Values) yield return v;
                }
            }
        }
    }
    

    到期 View 模型:
    using System;
    using System.ComponentModel;
    
    namespace WpfApplication1.Models
    {
        public class ExpiryViewModel : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
            protected void OnPropertyChanged(string propertyName)
            {
                if (this.PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
    
            private DateTime _expiryDate;
            public DateTime ExpiryDate { get { return _expiryDate; } set { _expiryDate = value; OnPropertyChanged("ExpiryDate"); } }
    
            public int SecondsToExpiry { get { return (int)ExpiryDate.Subtract(DateTime.Now).TotalSeconds; } }
    
            public ExpiryViewModel()
            {
                this.ExpiryDate = DateTime.Today.AddDays(2.67);
    
                var timer = new System.Timers.Timer(1000);
                timer.Elapsed += (s, e) => OnPropertyChanged("SecondsToExpiry");
                timer.Start();
            }
        }
    }
    

    关于c# - 带有标记扩展名的字符串格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25415400/

    有关c# - 带有标记扩展名的字符串格式的更多相关文章

    1. ruby - 如何从 ruby​​ 中的字符串运行任意对象方法? - 2

      总的来说,我对ruby​​还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用

    2. Ruby 解析字符串 - 2

      我有一个字符串input="maybe(thisis|thatwas)some((nice|ugly)(day|night)|(strange(weather|time)))"Ruby中解析该字符串的最佳方法是什么?我的意思是脚本应该能够像这样构建句子:maybethisissomeuglynightmaybethatwassomenicenightmaybethiswassomestrangetime等等,你明白了......我应该一个字符一个字符地读取字符串并构建一个带有堆栈的状态机来存储括号值以供以后计算,还是有更好的方法?也许为此目的准备了一个开箱即用的库?

    3. ruby-on-rails - 在 Rails 中将文件大小字符串转换为等效千字节 - 2

      我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,

    4. ruby-on-rails - unicode 字符串的长度 - 2

      在我的Rails(2.3,Ruby1.8.7)应用程序中,我需要将字符串截断到一定长度。该字符串是unicode,在控制台中运行测试时,例如'א'.length,我意识到返回了双倍长度。我想要一个与编码无关的长度,以便对unicode字符串或latin1编码字符串进行相同的截断。我已经了解了Ruby的大部分unicode资料,但仍然有些一头雾水。应该如何解决这个问题? 最佳答案 Rails有一个返回多字节字符的mb_chars方法。试试unicode_string.mb_chars.slice(0,50)

    5. ruby - 将差异补丁应用于字符串/文件 - 2

      对于具有离线功能的智能手机应用程序,我正在为Xml文件创建单向文本同步。我希望我的服务器将增量/差异(例如GNU差异补丁)发送到目标设备。这是计划:Time=0Server:hasversion_1ofXmlfile(~800kiB)Client:hasversion_1ofXmlfile(~800kiB)Time=1Server:hasversion_1andversion_2ofXmlfile(each~800kiB)computesdeltaoftheseversions(=patch)(~10kiB)sendspatchtoClient(~10kiBtransferred)Cl

    6. ruby - 使用 ruby​​ 将 HTML 转换为纯文本并维护结构/格式 - 2

      我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h

    7. ruby-on-rails - Rails 常用字符串(用于通知和错误信息等) - 2

      大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje

    8. ruby - 如何以所有可能的方式将字符串拆分为长度最多为 3 的连续子字符串? - 2

      我试图获取一个长度在1到10之间的字符串,并输出将字符串分解为大小为1、2或3的连续子字符串的所有可能方式。例如:输入:123456将整数分割成单个字符,然后继续查找组合。该代码将返回以下所有数组。[1,2,3,4,5,6][12,3,4,5,6][1,23,4,5,6][1,2,34,5,6][1,2,3,45,6][1,2,3,4,56][12,34,5,6][12,3,45,6][12,3,4,56][1,23,45,6][1,2,34,56][1,23,4,56][12,34,56][123,4,5,6][1,234,5,6][1,2,345,6][1,2,3,456][123

    9. ruby - 什么是填充的 Base64 编码字符串以及如何在 ruby​​ 中生成它们? - 2

      我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%

    10. ruby - 如何使用文字标量样式在 YAML 中转储字符串? - 2

      我有一大串格式化数据(例如JSON),我想使用Psychinruby​​同时保留格式转储到YAML。基本上,我希望JSON使用literalstyle出现在YAML中:---json:|{"page":1,"results":["item","another"],"total_pages":0}但是,当我使用YAML.dump时,它不使用文字样式。我得到这样的东西:---json:!"{\n\"page\":1,\n\"results\":[\n\"item\",\"another\"\n],\n\"total_pages\":0\n}\n"我如何告诉Psych以想要的样式转储标量?解

    随机推荐