简体   繁体   English

WPF设计时解析不同

[英]WPF designtime parsing is different

I'm having an issue where my MarkupExtension is behaving differently at designtime, or rather the xaml parser is providing different values. 我遇到一个问题,即我的MarkupExtension在设计时的行为不同,或者说xaml解析器提供了不同的值。

my class: 我的课:

class FormattableConverter : MarkupExtension, IValueConverter
{
    public FormattableConverter(string format)
    {
        Format = format;
    }

    public string Format { get; set; }

    public override void ProvideValue(IServiceProvider serviceProvider)
    {
        return this;
    }

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var formattable = value as IFormattable;
        return formattable?.ToString(Format, culture);
    }

    /* ConvertBack returns NotImplementedException */
}

I'm using it to show a DateTime value.: 我用它来显示DateTime值。

<HeaderedContentControl Content="{Binding DateTimeValue, Converter={c:FormattableConverter 'hh\\:mm\\:ss'}}"
                        /* Other parameters */ />

When breaking during runtime, IntelliSense shows the format is hh\\\\:mm\\\\:ss , and formatting works. 在运行时中断时,IntelliSense会显示格式为hh \\\\:mm \\\\:ss ,并且格式化有效。

But in the designer a FormatException is thrown: Input string was not in a correct format. 但是在设计器中会引发FormatException: Input string was not in a correct format.

If i debug the designer (Attaching to XDesProc.exe) IntelliSence show me that the format is hh:mm:ss ; 如果我调试设计器(附加到XDesProc.exe),IntelliSence会告诉我该格式为hh:mm:ss The backslashes are gone. 反斜杠不见了。

I've tried: 我试过了:

  • 'hh\\\\:mm\\\\:ss'
  • 'hh&#92;&#92;:mm&#92;&#92;:ss'
  • {}hh\\\\:mm\\\\:ss
  • Format='hh\\\\\\\\:mm\\\\\\\\:ss' (this works in designer, but not runtime) Format='hh\\\\\\\\:mm\\\\\\\\:ss' (这在设计器中有效,但在运行时不适用)
  • Format=hh\\\\\\\\:mm\\\\\\\\:ss (this works in designer, but not runtime) Format=hh\\\\\\\\:mm\\\\\\\\:ss (这在设计器中有效,但在运行时无效)
  • Format=hh\\\\:mm\\\\:ss
  • other combinations of above 以上的其他组合

Any idea why the parser provides different string values? 知道为什么解析器提供不同的字符串值吗?

Try this: 尝试这个:

On the top of your UserControl/Window: 在您的UserControl /窗口的顶部:

<Window.Resources>
    <c:FormattableConverter x:Key="formattableConverter" />
    <sys:String x:Key="myParameter"> hh\\:mm\\:ss </sys:String>
</Window.Resources>

and then in the control: 然后在控件中:

<HeaderedContentControl Content="{Binding DateTimeVale, 
                                          Converter={StaticResource formattableConverter} 
                                          ConverterParameter={StaticResource myParameter}}">

</HeaderedContentControl>

Still if you have any problems, then please post a sample project. 如果仍然有任何问题,请发布一个示例项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM