Awesomium 1.7.5.1, WinForms, WebControl obj.ExecuteJavascript - winforms

I want to insert html code into existing html code.
But I do not see the result. Here is the code C #:
1) Program.cs
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
wUI.DocumentReady += wUI_DocumentReady;
}
private void Form1_Load(object sender, EventArgs e)
{
// code here ?
}
void wUI_DocumentReady(object sender, DocumentReadyEventArgs e)
{
wUI.LoadHTML("<html><body>sadasdsad</body></html>");
HtmlManager html = HtmlManager.Instance;
string[] placeholders = { "asset://customdatastore/path/to/any", "type-button", "no-action", "Example link" };
html.Add("{3}", placeholders);
html.InnerCode(html.Code, wUI, "body");
wUI.Refresh();
}
}
2) HtmlManager.cs
public sealed class HtmlManager
{
private static readonly Lazy<HtmlManager> InstanceField = new Lazy<HtmlManager>(() => new HtmlManager());
private StringBuilder _stringBuilder = null;
public string Code { get { return _stringBuilder.ToString(); } }
private HtmlManager()
{
if (_stringBuilder != null)
_stringBuilder.Clear();
_stringBuilder = new StringBuilder();
}
public static HtmlManager Instance { get { return InstanceField.Value; } }
public void Add(string row, string[] placeholders = null)
{
if (placeholders != null)
_stringBuilder.AppendLine(string.Format(row, placeholders));
_stringBuilder.AppendLine(row);
}
public void InnerCode(string code, object sender, string afterTag = "html")
{
Awesomium.Windows.Forms.WebControl ui = (Awesomium.Windows.Forms.WebControl)sender;
ui.ExecuteJavascript(string.Format("document.getElementsByTagName({0})[0].innerHTML({1})", afterTag, code));
}
public void Clear()
{
_stringBuilder.Clear();
}
}
The event (DocumentReady) does not happen, I do not believe, maybe I'm wrong somewhere?
UP: I try do it:
private void Form1_Load(object sender, EventArgs e)
{
wUI.LoadHTML("<html><body>sadasdsad</body></html>");
}
void wUI_DocumentReady(object sender, DocumentReadyEventArgs e)
{
HtmlManager html = HtmlManager.Instance;
string[] placeholders = { "asset://customdatastore/path/to/any", "type-button", "no-action", "Example link" };
html.Add("{3}", placeholders);
wUI.ExecuteJavascript("document.getElementsByTagName('body').innerHTML(\"sometext\")");
//html.InnerCode(html.Code, wUI, "body");
//wUI.Refresh();
}
No result
UP 2:
public void Add(string row, string[] placeholders = null)
{
if (placeholders != null)
_stringBuilder.AppendLine(string.Format(row, placeholders));
if (placeholders == null)
_stringBuilder.AppendLine(row);
}
UP 3:
Work with:
wUI.Source = new Uri(#"http://google.com");
in Form1_Load

You can use LoadHtml method, but only after document is fully loaded (don't confuse with DocumentReadyState.Ready) It works for me at least:
private void WebControl_DocumentReady(object sender, DocumentReadyEventArgs e)
{
if (e.ReadyState != DocumentReadyState.Loaded) return;
}
But as an initialisation, you should use Source property, like you wrote in your third update

Related

Xamarin binding does not work in Behavior<>

I made Behavior for transfer image to the ViewModel property.
When the user сlicks on the element, the gallery will open. When the user chooses some image from gallery, my ImageBytes will have bytes of this image. But after I assign a new value to the property, it is not passed to my VM.
My view model does not respond to changes in the Behavior.
public class FolderDialogBehavior : Behavior<View>
{
public byte[] ImageBytes
{
get { return (byte[])GetValue(ImageBytesProperty); }
private set
{
SetValue(ImageBytesProperty, value);
}
}
public readonly static BindableProperty ImageBytesProperty = BindableProperty.Create(nameof(ImageBytes), typeof(byte[]),
typeof(FolderDialogBehavior), null, BindingMode.TwoWay);
private TapGestureRecognizer tapGestureRecognizer = new TapGestureRecognizer()
{
NumberOfTapsRequired = 1
};
protected override void OnAttachedTo(View view)
{
base.OnAttachedTo(view);
tapGestureRecognizer.Tapped += OnTapGestureRecognizerTapped;
view.GestureRecognizers.Add(tapGestureRecognizer);
}
protected override void OnDetachingFrom(View view)
{
base.OnDetachingFrom(view);
tapGestureRecognizer.Tapped -= OnTapGestureRecognizerTapped;
view.GestureRecognizers.Remove(tapGestureRecognizer);
}
private void OnTapGestureRecognizerTapped(object sender, EventArgs e)
{
GetPhotoAsync();
}
private async void GetPhotoAsync()
{
try
{
var photo = await MediaPicker.PickPhotoAsync();
byte[] bytes;
using (Stream sourceStream = await photo.OpenReadAsync())
{
bytes = new byte[sourceStream.Length];
await sourceStream.ReadAsync(bytes, 0, (int)sourceStream.Length);
}
ImageBytes = bytes;
}
catch (Exception ex)
{
//await DisplayAlert("Сообщение об ошибке", ex.Message, "OK");
}
}
}
<Frame>
<Frame.Behaviors>
<local:FolderDialogBehavior ImageBytes="{Binding AddEmployee.UserImage, Mode=TwoWay}"/>
</Frame.Behaviors>
</Frame>
public class EmployeeViewModel : OnPropertyChangedClass
{
private byte[] _userImage;
public byte[] UserImage
{
get => _userImage;
// *** I don't get here with debugging.***
set => SetProperty(ref _userImage, value);
}
}
public abstract class OnPropertyChangedClass : INotifyPropertyChanged
{
/// <inheritdoc cref="INotifyPropertyChanged"/>
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
protected void SetProperty<T>(ref T propertyFiled, T newValue, [CallerMemberName] string propertyName = null)
{
if (!object.Equals(propertyFiled, newValue))
{
T oldValue = propertyFiled;
propertyFiled = newValue;
RaisePropertyChanged(propertyName);
OnPropertyChanged(propertyName, oldValue, newValue);
}
}
protected virtual void OnPropertyChanged(string propertyName, object oldValue, object newValue) { }
}
protected override void OnAttachedTo(View bindable)
{
BindingContext = bindable.BindingContext;
bindable.BindingContextChanged += Bindable_BindingContextChanged;
/* Some Code */
base.OnAttachedTo(bindable);
}
protected override void OnDetachingFrom(View bindable)
{
bindable.BindingContextChanged -= Bindable_BindingContextChanged;
/* Some Code */
base.OnDetachingFrom(bindable);
}
private void Bindable_BindingContextChanged(object sender, EventArgs e)
{
if (sender is View view)
{
BindingContext = (sender as View).BindingContext;
}
}

How to automatically refresh listbox when add or remove the item in WPF?

I have WPF application that uses web service (asmx). The web service uses EF to get the data from MS SQL Server.
The code looks as following:
1) WPF:
public partial class MainWindow : Window
{
LetterWebServiceSoapClient _client = new LetterWebServiceSoapClient();
private ObservableCollection<Letter> _letters;
public MainWindow()
{
InitializeComponent();
}
private void cmdGetLetters_Click(object sender, RoutedEventArgs e)
{
lstLetters.ItemsSource = null;
_letters = _client.GetAllLetters();
lstLetters.ItemsSource = _letters;
}
private void cmdDeleteLetter_Click(object sender, RoutedEventArgs e)
{
_client.DeleteLetter((Letter)lstLetters.SelectedItem);
}
private void cmdAddLetter_Click(object sender, RoutedEventArgs e)
{
var newLetter = new Letter
{
Name = "Letter3",
Date = DateTime.Now,
Recipient = "John",
Sender = "David",
Content = "cccc"
};
_client.AddNewLetter(newLetter);
}
}
2) The web service:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class LetterWebService : System.Web.Services.WebService
{
LetterDbEntities _letterDbEntities = new LetterDbEntities();
[WebMethod]
public ObservableCollection<Letter> GetAllLetters()
{
return new ObservableCollection<Letter>(_letterDbEntities.Letters.ToList());
}
[WebMethod]
public void AddNewLetter(Letter newLetter)
{
_letterDbEntities.Letters.Add(newLetter);
_letterDbEntities.SaveChanges();
}
[WebMethod]
public void DeleteLetter(Letter letter)
{
var letterToBeDeleted = _letterDbEntities.Letters.First(l => l.Id == letter.Id);
_letterDbEntities.Letters.Remove(letterToBeDeleted);
_letterDbEntities.SaveChanges();
}
}
When I add new letter or remove existing one they are added or removed on database level, but it doesn't reflect in UI that is in list box. What I'm missing?
You need to change the _letters collection. Try this:
private void cmdDeleteLetter_Click(object sender, RoutedEventArgs e)
{
var selectedItem = (Letter)lstLetters.SelectedItem;
_client.DeleteLetter(selectedItem);
_letters.Remove(selectedItem);
}
private void cmdAddLetter_Click(object sender, RoutedEventArgs e)
{
var newLetter = new Letter
{
Name = "Letter3",
Date = DateTime.Now,
Recipient = "John",
Sender = "David",
Content = "cccc"
};
_client.AddNewLetter(newLetter);
_letters.Add(newLetter);
}

Subscribing to PropertyChangedEventHandler in Winforms

In the following example the temp variable in RaisePropertyChanged() is always null. How do I subscribe to the event?
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations.Schema;
using System.Runtime.CompilerServices;
using System.Text;
namespace TestProject.Module.BusinessObjects
{
public class ContactPerson : INotifyPropertyChanged
{
public string FirstName { get; set; }
public string LastName { get; set; }
[NotMapped]
public string PersonFullName
{
set
{
if (PersonFullName != value)
{
var stringArray = value.Split();
var firstIndex = 0;
var lastIndex = stringArray.Length - 1;
if (lastIndex >= firstIndex)
{
FirstName = stringArray[firstIndex];
}
if (lastIndex > firstIndex)
{
LastName = stringArray[lastIndex];
}
RaisePropertyChanged();
}
}
get
{
var sb = new StringBuilder();
sb.Append(FirstName);
sb.Append(" ");
sb.Append(LastName);
var stringArray = sb.ToString().Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
var s = string.Join(" ", stringArray);
return s;
}
}
public event PropertyChangedEventHandler PropertyChanged;
public ContactPerson Clone()
{
var obj = new ContactPerson { FirstName = FirstName, LastName = LastName };
return obj;
}
public override string ToString()
{
return PersonFullName;
}
protected void RaisePropertyChanged([CallerMemberName] string propertName = "")
{
var temp = PropertyChanged;
if (temp != null)
{
temp(this, new PropertyChangedEventArgs(propertName));
}
}
}
}
from reading This question it seems that PropertyChanged has not been subscribed to. How do I do this subscription?
Like this:
private void Form1_Load(object sender, EventArgs e)
{
ContactPerson p = new ContactPerson();
p.PropertyChanged += P_PropertyChanged;
}
private void P_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
throw new NotImplementedException();
}
EDIT: expanding the sample code:
public partial class Form1 : Form
{
ContactPerson p;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
p = new ContactPerson();
p.PersonFullName = "Mary Jane";
p.PropertyChanged += P_PropertyChanged;
label1.Text = p.PersonFullName;
// If you use databinding instead, you get the same result in this case.
//label1.DataBindings.Add(new Binding("Text", p, "PersonFullName"));
}
private void P_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
label1.Text = p.PersonFullName;
}
private void button1_Click(object sender, EventArgs e)
{
p.PersonFullName = "John Doe";
}
}

Silverlight 4 RIA service issue

Hi I have created a sample database application in Silverlight by following a tutorial. I am trying to insert a record in DB but it throws an error Submit operation failed validation. Please inspect Entity.ValidationErrors for each entity in EntitiesInError for more information. I am using Entity framework for DAL. My code is given below. Exception occure when I insert a record. But Iam not sure at which stage exception occurs.
RecordInsertPage.cs file
public partial class BookRegistaeration : ChildWindow
{
public Book newBook { get; set; }
public BookRegistaeration()
{
InitializeComponent();
newBook = new Book();
AddBookForm.CurrentItem =AddBookForm;
AddBookForm.BeginEdit();
}
private void OKButton_Click(object sender, RoutedEventArgs e)
{
AddBookForm.CommitEdit();
this.DialogResult = true;
}
private void CancelButton_Click(object sender, RoutedEventArgs e)
{
newBook = null;
AddBookForm.CancelEdit();
this.DialogResult = false;
}
}
Service.cs Page
public class OrganizationService : LinqToEntitiesDomainService<LibraryEntities1>
{
public IQueryable<Book> GetBooks()
{
return this.ObjectContext.Books.OrderBy(e => e.BookId);
}
public void InsertBook(Book book)
{
//book.Title = "Hello book";
//book.Author = "Ali";
//book.Category = "Humanity";
if ((book.EntityState != EntityState.Detached))
{
this.ObjectContext.ObjectStateManager.ChangeObjectState(book, EntityState.Added);
}
else
{
this.ObjectContext.Books.AddObject(book);
}
}
public void UpdateBook(Book currentBook)
{
this.ObjectContext.Books.AttachAsModified(currentBook, this.ChangeSet.GetOriginal(currentBook));
}
public void DeleteBook(Book book)
{
if ((book.EntityState != EntityState.Detached))
{
this.ObjectContext.ObjectStateManager.ChangeObjectState(book, EntityState.Deleted);
}
else
{
this.ObjectContext.Books.Attach(book);
this.ObjectContext.Books.DeleteObject(book);
}
}
}
private void LibraryDataSource_SubmittedChanges(object sender, SubmittedChangesEventArgs e)
{
if (e.HasError)
{
MessageBox.Show(string.Format("Changes were not saved: {0}", e.Error.Message));
e.MarkErrorAsHandled();
}
submitButton.IsEnabled = true;
}
void addBook_Closed(object sender, EventArgs e)
{
BookRegistaeration book = (BookRegistaeration)sender;
if (book.newBook != null)
{
OrganizationContext _OrganizationContext = (OrganizationContext)(LibraryDataSource.DomainContext);
_OrganizationContext.Books.Add(book.newBook);
LibraryDataSource.SubmitChanges();
}
}
Try doing as the exception message suggests, put a breakpoint on your SubmittedChanges event to inspect that exception object for which you should be able to see Entity.ValidationErrors for each entity in EntitiesInError.
This should tell you which of the fields in the object you are trying to add has failed the validation check, you may have null data in fields which cannot be null. You may find string properties are not allowed to be empty.
Try also to ensure your form is properly populating the object you will be adding, you could place a breakpoint before CommitEdit is called and inspect the object state.
i do not understand this line !
AddBookForm.CurrentItem =AddBookForm;
why did you write this?
it should be something like this
AddBookForm.CurrentItem = your class object !

How do I capture key down in WPF?

How do I capture a key down event in WPF even if my application is not focused?
For me, the best way is this:
public MainWindow()
{
InitializeComponent();
CompositionTarget.Rendering += new EventHandler(CompositionTarget_Rendering);
}
void CompositionTarget_Rendering(object sender, EventArgs e)
{
if ((Keyboard.GetKeyStates(Key.W) & KeyStates.Down) > 0)
{
player1.walk();
}
}
The rendering event runs every time.
Global keyboard hook can slow down your debugging.
I prefer to use this approach:
Create KeyboardListener class
public class KeyboardListener : IDisposable
{
private readonly Thread keyboardThread;
//Here you can put those keys that you want to capture
private readonly List<KeyState> numericKeys = new List<KeyState>
{
new KeyState(Key.D0),
new KeyState(Key.D1),
new KeyState(Key.D2),
new KeyState(Key.D3),
new KeyState(Key.D4),
new KeyState(Key.D5),
new KeyState(Key.D6),
new KeyState(Key.D7),
new KeyState(Key.D8),
new KeyState(Key.D9),
new KeyState(Key.NumPad0),
new KeyState(Key.NumPad1),
new KeyState(Key.NumPad2),
new KeyState(Key.NumPad3),
new KeyState(Key.NumPad4),
new KeyState(Key.NumPad5),
new KeyState(Key.NumPad6),
new KeyState(Key.NumPad7),
new KeyState(Key.NumPad8),
new KeyState(Key.NumPad9),
new KeyState(Key.Enter)
};
private bool isRunning = true;
public KeyboardListener()
{
keyboardThread = new Thread(StartKeyboardListener) { IsBackground = true };
keyboardThread.Start();
}
private void StartKeyboardListener()
{
while (isRunning)
{
Thread.Sleep(15);
if (Application.Current != null)
{
Application.Current.Dispatcher.Invoke(() =>
{
if (Application.Current.Windows.Count > 0)
{
foreach (var keyState in numericKeys)
{
if (Keyboard.IsKeyDown(keyState.Key) && !keyState.IsPressed) //
{
keyState.IsPressed = true;
KeyboardDownEvent?.Invoke(null, new KeyEventArgs(Keyboard.PrimaryDevice, PresentationSource.FromDependencyObject(Application.Current.Windows[0]), 0, keyState.Key));
}
if (Keyboard.IsKeyUp(keyState.Key))
{
keyState.IsPressed = false;
}
}
}
});
}
}
}
public event KeyEventHandler KeyboardDownEvent;
/// <summary>
/// Состояние клавиши
/// </summary>
private class KeyState
{
public KeyState(Key key)
{
this.Key = key;
}
public Key Key { get; }
public bool IsPressed { get; set; }
}
public void Dispose()
{
isRunning = false;
Task.Run(() =>
{
if (keyboardThread != null && !keyboardThread.Join(1000))
{
keyboardThread.Abort();
}
});
}
}
Subscribe to KeyboardDownEvent in code-behind (or where you need it).
public partial class MainWindow : Window
{
private KeyboardListener listener;
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
listener = new KeyboardListener();
listener.KeyboardDownEvent += ListenerOnKeyPressed;
}
private void ListenerOnKeyPressed(object sender, KeyEventArgs e)
{
// TYPE YOUR CODE HERE
}
private void Window_OnUnloaded(object sender, RoutedEventArgs e)
{
listener.KeyboardDownEvent -= ListenerOnKeyPressed;
}
}
Done
See this questions for hooking the keyboard Using global keyboard hook (WH_KEYBOARD_LL) in WPF / C#

Resources