How do you make a blink at c#?
-
I have the task of changing the introductory text.
♪
textbox
Introducing, for example, the text of such a type:-)
♪In the introduction of each symbol, the corresponding properties shall be updated. If the combination contains
:-)
but these symbols are instantaneously converted intoand return to the reference point. In doing so, I need to use the observer to make the same transformation.
vm:
public class CurrentDialogViewModel:ViewModelBase { private string _sendingMessageText; public string SendingMessageText { get { return _sendingMessageText; }
set { if (value != null) { WriteingMessage.NotifyObservers(value); } _sendingMessageText = Observer.outtext; OnPropertyChanged("SendingMessageText"); }
}
public Observer Observer = new Observer();
public Observerable WriteingMessage = new Observerable();
public CurrentDialogViewModel(){ WriteingMessage.AddObserver(Observer);}
}
Observer:
public class Observerable
{
public List<Observer> observers;
public Observerable()
{
observers = new List<Observer>();
}
public void AddObserver(Observer o) { observers.Add(o); }
public void RemoveObserver(int index) { observers.RemoveAt(index); }
public void NotifyObservers(string text)
{
foreach (Observer o in observers)
{
o.Notify(text);
}
}}
public class Observer
{ IDictionary<String, String> Smiles { get; set; }
public string outtext{get;set;}
public Observer()
{Smiles = new Dictionary<String, String>(); Smiles.Add(":-)", ""); Smiles.Add(":-(", ""); Smiles.Add(":-D", ""); } public void Notify(string text) { foreach(var i in Smiles) { if (text.Contains(i.Key)) { text.Replace(i.Key, i.Value); } } outtext = text; }
}
♪
View:
<TextBoxText="{Binding Path=SendingMessageText, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" />
-
If textbox introduces the text, e.g.:-) and when each symbol is inserted, the corresponding properties shall be updated, and if the combination contains :-, the symbols shall be instantaneously altered
Minimum and sufficient solution for WPF is such.
<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <TextBox Text="{Binding Text, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" /> </Window>
using System; using System.Collections.Generic; using System.ComponentModel; using System.Windows; namespace WpfApplication1 { class Model : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged = delegate { }; private void Changed(string name) { this.PropertyChanged(this, new PropertyChangedEventArgs(name)); } Dictionary<string, string> smiles; public Model() { this.smiles = new Dictionary<string, string>(); this.smiles.Add(":)", ""); } public string Text { get { return _Text; } set { if (_Text != value) { _Text = value; if(_Text != null) foreach (var kv in smiles) if (_Text.Contains(kv.Key)) _Text = _Text.Replace(kv.Key, kv.Value); Changed("Text"); } } } string _Text; } public partial class MainWindow : Window { public MainWindow() { this.DataContext = new Model() { Text = "123" }; } } }
class Model without change can be transferred to WinForms.