开发者

Why does a Silverlight TextBox use \r for a newline instead of Environment.Newline (\r\n)?

开发者 https://www.devze.com 2023-01-14 01:47 出处:网络
In silverlight, if a T开发者_开发技巧extBox AcceptsReturn, all newlines are \\r, even though Environment.Newline is \\r\\n.Why is this?(WPF has \\r\\n as newline for textbox)I agree with ertan\'s answ

In silverlight, if a T开发者_开发技巧extBox AcceptsReturn, all newlines are \r, even though Environment.Newline is \r\n. Why is this? (WPF has \r\n as newline for textbox)


I agree with ertan's answer. I have ran into a scenario in which this is inconveniencing.

We have an application that collects string data from a user through a Silverlight Textbox and stores that data in a SQL Server database, which is a very common. A problem arises when other components of the application use that stored string data expecting line breaks to be represented by "\r\n". One example of such a component is Telerik's reporting solution: See Line break issue in multi line text boxes.

I overcame this problem by using this value converter:

public class LineBreakCorrectionConverter : IValueConverter
{
    private const string CR = "\r";
    private const string CRLF = "\r\n";

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        string text = value as string;

        if (text == null)
            return null;

        return text.Replace(CRLF, CR);
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        string text = value as string;

        if (text == null)
            return null;

        return text.Replace(CR, CRLF);
    }
}


I think because of compatibility with other operating systems.

Silverlight is available on linux and mac operating systems. Both (and most) of these OS's is unix based and unix uses '\r' for new lines. (as far i know only MS using the '\r\n')

While looking framework source code seems like MS developers mostly used to type '\r\n' instead of using Environment.NewLine.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号