I have the following class:
public class Sticky : INotifyPropertyChanged {
// ... some members
private BitmapImage _frontPic;
[DataMember]
public BitmapImage FrontPic {
get {
return _frontPic;
}
set {
_frontPic = value;
Changed("FrontPic");
Changed("FrontBrush");
}
}
}
I'm trying to databind it to this XAML:
<Image Width="173" Height="173" Source="{Binding FrontPic}" />
after launching a PhotoChooserTask with this code in my PhoneApplicationPage:
public Sticky Sticky { get; set; }
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) {
Sticky = new Sticky();
DataContext = Sticky;
}
private void ShowFrontPicPicker(object sender, RoutedEventArgs e) {
var t = new PhotoChooserTask();
t.PixelHeight = 173;
t.PixelWidth = 173;
t.ShowCamera = true;
t.Completed += (s, ev) => {
if (ev.TaskResult == TaskResult.OK) {
var img = new BitmapImage();
img.SetSource(ev.ChosenPhoto);
Sticky.FrontPic = img;
}
};
t.Show();
}
However, my image remains blank. If I assign the Image.Source property directly to the Image without databinding, everything works. Databinding other properties works, it's just the image that seems to be the problem. How can I make the DataBinding on the image work?
Found the problem! The completed callback for PhotoChooserTask does not excecute in the UI thread, so a call to Dispatcher.BeginInvoke must be added:
t.Completed += (s, ev) => Dispatcher.BeginInvoke(() => {
// do stuff...
});
Related
questions:
1、i wanna get template elements when calling the constructor, but return null, any way?
2、i found it, get not null obj after loaded event, but i don't want this way.
snippet code:(to see my comments)
using System;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using System.Windows;
using System.Windows.Input;
namespace WpfPropertyGrid_Demo
{
public class MyButton : Control
{
public static readonly DependencyProperty IsMouseDownProperty;
static MyButton()
{
IsMouseDownProperty = DependencyProperty.Register(
"IsMouseDown",
typeof(bool),
typeof(MyButton),
new FrameworkPropertyMetadata(false)
);
}
public bool IsMouseDown
{
get { return (bool)GetValue(IsMouseDownProperty); }
set { SetValue(IsMouseDownProperty, value); }
}
private BitmapImage _normalImg;
private BitmapImage _overImg;
private BitmapImage _clickImg;
private BitmapImage _disabledImg;
public MyButton()
{
_normalImg = new BitmapImage(new Uri("../../Images/ScrollerBtnBg.png", UriKind.RelativeOrAbsolute));
_overImg = new BitmapImage(new Uri("../../Images/ScrollerThumbnailBtnBg.png", UriKind.RelativeOrAbsolute));
_clickImg = new BitmapImage(new Uri("../../Images/ScrollerThumbnailBg.png", UriKind.RelativeOrAbsolute));
_disabledImg = _clickImg;
var style = new Style(typeof(MyButton));
var controlTemplate = new ControlTemplate();
var gridFactory = new FrameworkElementFactory(typeof(Grid));
var imgFacotry = new FrameworkElementFactory(typeof(Image));
imgFacotry.Name = "Image";
imgFacotry.SetValue(Image.SourceProperty, _normalImg);
gridFactory.AppendChild(imgFacotry);
controlTemplate.VisualTree = gridFactory;
var overTrigger = new Trigger();
overTrigger.Property = UIElement.IsMouseOverProperty;
overTrigger.Value = true;
overTrigger.Setters.Add(new Setter(Image.SourceProperty, _overImg, "Image"));
var disabledTriiger = new Trigger();
disabledTriiger.Property = UIElement.IsEnabledProperty;
disabledTriiger.Value = false;
disabledTriiger.Setters.Add(new Setter(Image.SourceProperty, _disabledImg, "Image"));
var downTrigger = new Trigger();
downTrigger.Property = MyButton.IsMouseDownProperty;
downTrigger.Value = true;
downTrigger.Setters.Add(new Setter(Image.SourceProperty, _clickImg, "Image"));
controlTemplate.Triggers.Add(overTrigger);
controlTemplate.Triggers.Add(disabledTriiger);
controlTemplate.Triggers.Add(downTrigger);
style.Setters.Add(new Setter(Control.TemplateProperty, controlTemplate));
this.PreviewMouseDown += new System.Windows.Input.MouseButtonEventHandler(MyButton_PreviewMouseDown);
this.PreviewMouseUp += new System.Windows.Input.MouseButtonEventHandler(MyButton_PreviewMouseUp);
this.MouseLeave += new System.Windows.Input.MouseEventHandler(MyButton_MouseLeave);
this.MouseEnter += new MouseEventHandler(MyButton_MouseEnter);
Style = style;
// 1、why cann't find "Image" element, it return null obj, i wanna get it immediately, any way?
// 2、return not null after loaded event
// var image = controlTemplate.FindName("Image", this );
this.Loaded += new RoutedEventHandler(MyButton_Loaded);
}
void MyButton_MouseEnter(object sender, MouseEventArgs e)
{
IsMouseDown = e.LeftButton == MouseButtonState.Pressed;
}
void MyButton_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
IsMouseDown = false;
}
void MyButton_PreviewMouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
IsMouseDown = false;
}
void MyButton_PreviewMouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
IsMouseDown = true;
}
void MyButton_Loaded(object sender, RoutedEventArgs e)
{
// return not null after loaded event
var image = this.Template.FindName("Image", this) as Image;
}
}
}
Try Visual Helper Class here I hope this will help
I am implementing a Webkit Browser control in my windows app.
I need to use a custom context menu (right click) that only has copy/cut/paste as its options regardless of what element is right clicked. I need kind of a step-by-step as to how to implement it
Customizing the context menu for the WebKitBrowser supposes that you get a reference to the WebViewClass and then, setting a IWebUIDelegate for it by calling the setUIDelegate() method.
void MyWebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
var webView = this.GetWebView() as WebKit.Interop.WebViewClass;
webView.setUIDelegate(new MyWebUIDelegate(this));
}
In the IWebUIDelegate implementation you may intercept the contextMenuItemsForElement method and trigger the display of the context menu of the browser.
Here is a working sample:
public partial class Form1 : Form
{
MyWebBrowser webKitBrowser;
public Form1()
{
InitializeComponent();
webKitBrowser = new MyWebBrowser();
webKitBrowser.Dock = DockStyle.Fill;
this.Controls.Add(webKitBrowser);
webKitBrowser.Navigate("http://www.google.com");
}
}
class MyContextMenu : ContextMenu
{
public MyContextMenu()
{
var cutMenuItem = new MenuItem("Cut");
var copyMenuItem = new MenuItem("Copy");
var pasteMenuItem = new MenuItem("Paste");
cutMenuItem.Click += cutMenuItem_Click;
MenuItems.Add(cutMenuItem);
MenuItems.Add(copyMenuItem);
MenuItems.Add(pasteMenuItem);
}
void cutMenuItem_Click(object sender, EventArgs e)
{
//TODO: implement functionality
MessageBox.Show("Cut was selected");
}
}
class MyWebBrowser : WebKitBrowser
{
public event EventHandler ShowContextMenu = new EventHandler(OnFireShowContextMenu);
public MyWebBrowser()
{
DocumentCompleted += MyWebBrowser_DocumentCompleted;
var myContextMenu = new MyContextMenu();
ContextMenu = myContextMenu;
}
void MyWebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
var webView = this.GetWebView() as WebKit.Interop.WebViewClass;
webView.setUIDelegate(new MyWebUIDelegate(this));
}
public static void OnFireShowContextMenu(object sender, EventArgs e)
{
var webBrowser = (Control)sender;
var webView = (WebKit.Interop.WebViewClass)((MyWebBrowser)sender).GetWebView();
var originalPoint = webBrowser.PointToScreen(new Point(0, 0));
var currentPoint = new Point(Cursor.Position.X - originalPoint.X, Cursor.Position.Y - originalPoint.Y);
((WebKitBrowser)sender).ContextMenu.Show((Control)sender, currentPoint);
}
public void FireShowContextMenu()
{
this.ShowContextMenu(this, null);
}
}
class MyWebUIDelegate : IWebUIDelegate
{
private MyWebBrowser owner;
public MyWebUIDelegate(MyWebBrowser browser)
{
this.owner = browser;
}
//trigger the browser's FireShowContextMenu() method
public int contextMenuItemsForElement(WebView sender, CFDictionaryPropertyBag element, int defaultItemsHMenu)
{
owner.FireShowContextMenu();
return defaultItemsHMenu;
}
//return 1, true
public int hasCustomMenuImplementation()
{
return 1;
}
//the rest of the IWebUIDelegate interface implementation
}
For more insight, probably you would want to study some other customizations, such as open-webkit-sharp.
I am using a wpf UserControl to replace text in files in selected drawing files of AutoCAD. The wpf control is to display a status (ProgressBar) indicating the number of files processed at any given time. So I put up the following code, but the ProgressBar simply does not show any progress. Here is the part of relevant code.
XAML:
<ProgressBar HorizontalAlignment="Stretch" Name="pgrSearch" Minimum="0" Maximum="{Binding Path=ProgressBarMaximum}"
Value="{Binding Path=ProgressBarCurrent}" Height="20" Margin="10" />
CodeBehind:
public partial class ReplaceUserControl : UserControl, INotifyPropertyChanged {
public ReplaceUserControl() {
InitializeComponent();
this.DataContext = this;
}
....
private int _progressBarMaximum;
public int ProgressBarMaximum {
get { return _progressBarMaximum; }
set { _progressBarMaximum = value; RaisePropertyChanged("ProgressBarMaximum"); }
}
private int _progressBarCurrent;
private int ProgressBarCurrent {
get { return _progressBarCurrent; }
set { _progressBarCurrent = value; RaisePropertyChanged("ProgressBarCurrent"); }
}
private void ReplaceTextInFiles() { //Called from Button_Click Handler
....
ProgressBarMaximum = filesList.Count - 1;
SearchReplaceWorker replaceWorker = new SearchReplaceWorker(); //The Work Horse
replaceWorker.FileProcessed += new FileProcessedEventHandler(worker_FileProcessed); //Raised by Work Horse when each file is processed
BackgroundWorker workerThread = new BackgroundWorker(); //The Background Worker Thread
workerThread.DoWork += (o, e) => {
replaceWorker.ReplaceTextInFiles(SearchText, ReplaceText, filesList, ReportFolderPath, MatchCase, MatchSubstring);
};
workerThread.RunWorkerAsync(); //Start the Background Thread Async
}
void worker_FileProcessed(object sender, EventArgs e) {
ProgressBarCurrent = ProgressBarCurrent + 1; //Update the ProgressBar status
}
Why doesn't the ProgressBar update itself when the ProgressBarCurrent is incremented as indicated above in code.
Edit:
In Order to process the ProgressBar update code on UI thread, I changed my code to use BackgroundWorker.ReportProgress() as given under.
CodeBehind for UserControl:
private void ReplaceTextInFiles() { //Called from Button_Click()
if (!Directory.Exists(SearchFolderPath)) {
MessageBox.Show("Invalid Directory Selected for Search");
return;
}
if (!Directory.Exists(ReportFolderPath)) {
MessageBox.Show("Invalid Directory Selected for Report File");
return;
}
List<string> filesList = null;
try {
if (LookInSubFolders) {
filesList = Directory.GetFiles(#SearchFolderPath, "*.dwg", SearchOption.AllDirectories).ToList();
}
else {
filesList = Directory.GetFiles(#SearchFolderPath, "*.dwg", SearchOption.TopDirectoryOnly).ToList();
}
}
catch (Exception ex) {
MessageBox.Show("Error Occurred getting the files list. Contact Admin");
}
pgrSearch.Visibility = Visibility.Visible;
ProgressBarMaximum = filesList.Count - 1;
SearchReplaceWorker replaceWorker = new SearchReplaceWorker();
BackgroundWorker workerThread = new BackgroundWorker();
workerThread.WorkerReportsProgress = true;
workerThread.ProgressChanged += (o, e) => { //This event handler gets called correctly.
ProgressBarCurrent++;
};
workerThread.RunWorkerCompleted += new RunWorkerCompletedEventHandler(workerThread_RunWorkerCompleted);
workerThread.DoWork += (o, e) => {
replaceWorker.ReplaceTextInFiles(workerThread, SearchText, ReplaceText, filesList, ReportFolderPath, MatchCase, MatchSubstring);
};
workerThread.RunWorkerAsync();
}
The BackgroundWorker:
public void ReplaceTextInFiles(BackgroundWorker workerThread, string searchText, string replaceText, List<string> filesList, string reportPath,
bool MatchCase, bool MatchSubstring) {
...
workerThread.ReportProgress(50);
}
Still the ProgressBar doesn't update itself.
I created a test project with your initial code. After a while I found that you have declared the ProgressBarCurrent property as private. After changing to public it worked for me. So it doesn't seem necessary to update the property on the UI thread. It looks like a Dispatcher.Invoke call is made internally when reading back the updated property value.
I need to show Splash Screen with Image & progress bar.
In my application start up i have the code as below to show the main window.
SplashScreenWindowViewModel vm = new SplashScreenWindowViewModel();
AutoResetEvent ev = new AutoResetEvent(false);
Thread uiThread = new Thread(() =>
{
vm.Dispatcher = Dispatcher.CurrentDispatcher;
ev.Set();
Dispatcher.CurrentDispatcher.BeginInvoke((Action)delegate()
{
SplashScreenWindow splashScreenWindow = new SplashScreenWindow();
splashScreenWindow = new SplashScreenWindow();
splashScreenWindow.Show();
splashScreenWindow.DataContext = vm;
vm.InstigateWorkCommand.Execute(null);
});
Dispatcher.Run();
});
uiThread.SetApartmentState(ApartmentState.STA);
uiThread.IsBackground = true;
uiThread.Start();
ev.WaitOne();
In my main viewmodel i have code as below
class MainviewModel : viewmodelbase
{
rivate string _message;
private object content;
private readonly BackgroundWorker worker;
private readonly ICommand instigateWorkCommand;
public SplashScreenWindowViewModel()
{
this.instigateWorkCommand = new
RelayCommand(() => this.worker.RunWorkerAsync(), () => !this.worker.IsBusy);
this.worker = new BackgroundWorker { WorkerReportsProgress = true };
this.worker.DoWork += this.DoWork;
this.worker.ProgressChanged += this.ProgressChanged;
_message = "0 % completed";
}
public ICommand InstigateWorkCommand
{
get { return this.instigateWorkCommand; }
}
private double _currentProgress;
public double CurrentProgress
{
get { return this._currentProgress; }
set
{
if (this._currentProgress != value)
{
this._currentProgress = value;
RaisePropertyChanged("CurrentProgress");
}
}
}
private int _progressMax;
public int ProgressMax
{
get { return this._progressMax; }
set
{
if(this._progressMax != value)
{
this._progressMax = value;
RaisePropertyChanged("ProgressMax");
}
}
}
private void ProgressChanged(object sender, ProgressChangedEventArgs e)
{
this.CurrentProgress = e.ProgressPercentage;
}
private void DoWork(object sender, DoWorkEventArgs e)
{
// calling my long running operation
DAL.dotimeconsumingcode();
worker.ReportProgress((int)e.argument);
}
public string Message
{
get
{
return _message;
}
set
{
if (Message == value) return;
_message = value;
RaisePropertyChanged("Message");
}
}
public object Content
{
get
{
return content;
}
set
{
if (Content == value) return;
content = value;
RaisePropertyChanged("Content");
}
}
public Dispatcher Dispatcher
{
get;
set;
}
}
MY UI has one user control with progress bar and one splash main window.
when my long running operation is completed , my Main window(main application) is opened.
//User Control
<ProgressBar Height="27" Value="{Binding CurrentProgress, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" Margin="53,162,57,0" Name="progressBar" Grid.Row="1"
Maximum="{Binding Path=ProgressMax, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" Visibility="{Binding ProgressVisibility}" />
//SplashWindow
<localView:usercontrol/>
My Problem is
ProgressChangedevent is not firing and % completion is not showing up in the text block either. Please help
You have not registered a complete handler and you are not calling progress properly.
This sample from MSDN covers it all.
BackGroundWorker
How can I create System.Windows.Forms.WebBrowser in background STA thread? I try use some code like this:
var tr = new Thread(wbThread);
tr.SetApartmentState(ApartmentState.STA);
tr.Start();
private void wbThread()
{
CWebBrowser browser = new CWebBrowser();
var text = browser.Navigate("http://site.com", CWebBrowser.EventType.loadCompleted).Body.InnerHtml;
}
CWebBrowser - custom class, wich delegate System.Windows.Forms.WebBrowser object Navigate method and wait until page completed loads. The problem is LoadCompleted event on System.Windows.Forms.WebBrowser object never raises. I found some solution here, but it does not work (can't find method Application.Run() on my WPF app).
public class CWebBrowser : ContentControl
{
public readonly System.Windows.Forms.WebBrowser innerWebBrowser;
private readonly AutoResetEvent loadCompletedEvent;
private readonly AutoResetEvent navigatedEvent;
public enum EventType
{
navigated, loadCompleted
}
public CWebBrowser()
{
innerWebBrowser = new System.Windows.Forms.WebBrowser();
loadCompletedEvent = new AutoResetEvent(false);
navigatedEvent = new AutoResetEvent(false);
System.Windows.Forms.Integration.WindowsFormsHost host = new System.Windows.Forms.Integration.WindowsFormsHost();
host.Child = innerWebBrowser;
Content = host;
innerWebBrowser.DocumentCompleted +=new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(innerWebBrowser_DocumentCompleted);
innerWebBrowser.Navigated += new System.Windows.Forms.WebBrowserNavigatedEventHandler(innerWebBrowser_Navigated);
}
void innerWebBrowser_Navigated(object sender, System.Windows.Forms.WebBrowserNavigatedEventArgs e)
{
navigatedEvent.Set();
}
void innerWebBrowser_DocumentCompleted(object sender, System.Windows.Forms.WebBrowserDocumentCompletedEventArgs e)
{
if (((sender as System.Windows.Forms.WebBrowser).ReadyState != System.Windows.Forms.WebBrowserReadyState.Complete) || innerWebBrowser.IsBusy)
return;
var doc = innerWebBrowser.Document;
loadCompletedEvent.Set();
}
public System.Windows.Forms.HtmlDocument Navigate(string url, EventType etype)
{
if (etype == EventType.loadCompleted)
loadCompletedEvent.Reset();
else if (etype == EventType.navigated)
navigatedEvent.Reset();
innerWebBrowser.Navigate(url);
if (etype == EventType.loadCompleted)
loadCompletedEvent.WaitOne();
else if (etype == EventType.navigated)
navigatedEvent.WaitOne();
System.Windows.Forms.HtmlDocument doc = null;
Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background, new Action(
delegate
{
doc = innerWebBrowser.Document;
}));
return doc;
}
}
Thansk for all advices and sorry for my bad english :o(
Why don't you use the default WebBrowser control like this?
public MainPage()
{
InitializeComponent();
System.Windows.Deployment.Current.Dispatcher.BeginInvoke(startNavigate);
}
void startNavigate()
{
WebBrowser wb = new WebBrowser();
wb.LoadCompleted += new LoadCompletedEventHandler(wb_LoadCompleted);
wb.Navigated += new EventHandler<System.Windows.Navigation.NavigationEventArgs>(wb_Navigated);
wb.Navigate(new Uri("http://www.google.com"));
}
void wb_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
// e.Content
}
void wb_LoadCompleted(object sender, NavigationEventArgs e)
{
// e.Content when the document finished loading.
}
Edit: You are using old System.Windows.Forms.WebBrowser control, instead System.Windows.Controls.WebBrowser which is part of WPF.