Đây là vấn đề về luồng. Chúng ta phải lấy đúng chủ đề và thực hiện thông qua các đại biểu.
Tôi đang cập nhật thuộc tính của mình thông qua bộ hẹn giờ, cứ sau mỗi 500 ms sẽ mất hết.Đây là mã:
public delegate void ClipboarDelegate();
ClipboarDelegate clipboardDelegate = null;
void clipboardTimer_Elapsed(object sender, ElapsedEventArgs e)
{
if (clipboardDelegate == null)
clipboardDelegate = ClipboarDelegateMethod;
//Here we get the right thread, most probably the application thread
Application.Current.Dispatcher.BeginInvoke(clipboardDelegate);
}
public void ClipboarDelegateMethod()
{
try
{
if (Clipboard.ContainsData(DataFormats.Text))
{
//It's important to lock this section
lock (ClipboardString)
{
ClipboardString = Clipboard.GetData(DataFormats.Text) as string;
}
}
}
catch
{ }
}
Hơn nữa tôi đã thực hiện một DependencyProperty thích hợp với ClipboardString:
public static readonly DependencyProperty ClipboardStringDP =
DependencyProperty.Register("ClipboardString",
typeof(string),
typeof(MainWindow),
new UIPropertyMetadata(string.Empty));
public string ClipboardString
{
get { return (string)this.GetValue(ClipboardStringDP); }
set { this.SetValue(ClipboardStringDP, value); }
}
Bằng cách này nó có thể được liên kết với TextBox của tôi trong XAML giả kiểm soát hoặc cửa sổ x:Name="_this"
tôi:
<TextBox Name="ClipBoardTextBox"
DataContext="{Binding ElementName=_this}"
Text="{Binding Path=ClipboardString, Mode=OneWay}"/>
Làm cách nào để đưa dữ liệu vào khay nhớ tạm? –
Dữ liệu nằm trên "văn bản" của bảng tạm và không phải là đồ họa, âm thanh hay khác? – JMD