我正在尝试在文本框中读取插入的字符,但是我发现它很难,因为如果我的最后一个字符是空格(字符串必须“按原样”读取,这是一个要求),文本框Text属性返回类似“\ r \ n”的东西(是的,有8个空格)。
有关如何避免这种情况的任何建议?我需要阅读最后一个空格,而不是删除它,但我不想要那么奇怪的事情。
感谢您的回答。
修改1:
XAML
<Window x:Class="GSdk.SkypePlugin.ConfigureWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="GSdk.SkypePlugin configuration" Height="308" Width="486" WindowStartupLocation="CenterScreen" WindowStyle="ToolWindow" Topmost="True">
<Grid>
<TextBlock Height="60" HorizontalAlignment="Left" Margin="39,54,0,0" Name="textBlock1" Text="How much time should be displayed the plugin as a foreground applet for each word (seconds):" VerticalAlignment="Top" Width="375" TextWrapping="Wrap" />
<TextBlock Height="23" HorizontalAlignment="Left" Margin="12,12,0,0" Name="textBlock2" Text="Should the applet be displayed over everything when a message is received:" VerticalAlignment="Top" />
<TextBlock Height="23" HorizontalAlignment="Left" Margin="12,32,0,0" Name="textBlock3" Text="(care with this option, if SkypePlugin has focus, it will lose it)" VerticalAlignment="Top" />
<CheckBox Height="16" HorizontalAlignment="Left" Margin="401,32,0,0" Name="showWhenMessageIncomingCheckbox" VerticalAlignment="Top" />
<Slider Height="23" HorizontalAlignment="Left" Margin="39,91,0,0" Name="SecondsPerWordSlider" VerticalAlignment="Top" Width="375" Minimum="1" Value="1" AutoToolTipPlacement="BottomRight" AutoToolTipPrecision="1" LargeChange="0.1" SmallChange="0.01" />
<Button Content="Save" Height="23" HorizontalAlignment="Left" Margin="389,246,0,0" Name="saveButton" VerticalAlignment="Top" Width="75" Click="saveButton_Click" />
<Button Content="Cancel" Height="23" HorizontalAlignment="Left" Margin="305,246,0,0" Name="cancelButton" VerticalAlignment="Top" Width="75" Click="cancelButton_Click" />
<Label Height="28" HorizontalAlignment="Left" Margin="420,86,0,0" Name="label1" VerticalAlignment="Top" Width="32" Content="{Binding ElementName=SecondsPerWordSlider, Path=Value}" />
<TextBox xml:space="preserve" Height="23" HorizontalAlignment="Right" Margin="0,129,170,0" Name="m_MessageSeparatorTextbox" VerticalAlignment="Top" Width="120" IsEnabled="{Binding ElementName=m_UseMessageSeparator, Path=IsChecked}" Text=" " />
<CheckBox Content="Use message separator:" Height="16" HorizontalAlignment="Left" Margin="12,132,0,0" Name="m_UseMessageSeparator" VerticalAlignment="Top" />
</Grid>
</Window>
cs文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Drawing;
using System.Windows.Interop;
namespace GSdk.SkypePlugin
{
/// <summary>
/// Logica di interazione per ConfigureWindowxaml.xaml
/// </summary>
public partial class ConfigureWindow : Window
{
public ConfigureWindow(double secondsPerWord = 1.0, bool showWhenMessageIncoming = true, bool useMessageSeparator = true, string messageSeparator = " ")
{
InitializeComponent();
Icon = Imaging.CreateBitmapSourceFromHBitmap(Properties.Resources.LCDMedia_SkypePluginAppIcon.ToBitmap().GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
SecondsPerWordSlider.Value = secondsPerWord;
showWhenMessageIncomingCheckbox.IsChecked = showWhenMessageIncoming;
m_UseMessageSeparator.IsChecked = useMessageSeparator;
m_MessageSeparatorTextbox.Text = messageSeparator;
}
private void cancelButton_Click(object sender, RoutedEventArgs e)
{
Close();
}
private void saveButton_Click(object sender, RoutedEventArgs e)
{
DialogResult = true;
}
public bool ShowWhenMessageIncoming { get { return (showWhenMessageIncomingCheckbox.IsChecked.HasValue ? showWhenMessageIncomingCheckbox.IsChecked.Value : false); } }
public double SecondsPerWord { get { return SecondsPerWordSlider.Value; } }
public string MessageSeparator { get { return m_MessageSeparatorTextbox.IsEnabled ? m_MessageSeparatorTextbox.Text : " "; } }
public bool UseMessageSeparator { get { return m_UseMessageSeparator.IsChecked.HasValue ? m_UseMessageSeparator.IsChecked.Value : true; } }
}
}
调用代码
private void AppletConfigureHandler(object sender, EventArgs e)
{
App.Current.Dispatcher.BeginInvoke(new Action<SkypeApplet>(a =>
{
bool restart = false;
lock (a.SyncRoot)
{
var w = new ConfigureWindow(Properties.Settings.Default.SecondsPerWord, Properties.Settings.Default.ShowWhenMessageIncoming, Properties.Settings.Default.UseMessageSeparator, Properties.Settings.Default.MessageSeparator);
var result = w.ShowDialog();
if (result.HasValue && result.Value == true)
{
Properties.Settings.Default.SecondsPerWord = w.SecondsPerWord;
Properties.Settings.Default.ShowWhenMessageIncoming = w.ShowWhenMessageIncoming;
Properties.Settings.Default.MessageSeparator = w.MessageSeparator;
Properties.Settings.Default.UseMessageSeparator = w.UseMessageSeparator;
Properties.Settings.Default.Save();
restart = true;
}
}
if (restart)
App.Restart();
}), this);
}
注意:我在这一刻用xml:space测试过,通常不存在。
答案 0 :(得分:0)
对不起,好像我自己发现了这个问题并且非常愚蠢。
事实上,我出于某种原因在“设置”字符串中存储了“\ r \ n”。在我的配置窗口重新加载后,我似乎无法从文本框中的文本中删除该字符串,因为我的文本框是单行,而\ r \ n是在第二行。
我仍然不确定为什么我不能删除所有这些空格,但我认为这就是答案。如果有人有更好的,我会很高兴他/她。
感谢。
编辑1: 似乎在app.config和.settings文件之间出现了问题,我通过手动编辑它们来修复它。
编辑2: 我发现了问题:如果我在保存后立即重新启动应用程序,我不知道为什么我的字符串会被这些字符填充。我不明白为什么会这样。