I have some problem with my code. I have silverlight button after I press button I want to load document library and get information about how many items are in this library co my code is:
var web = context.Web;
List sharedDocumentsList = context.Web.Lists.GetByTitle("dokumenty");
int i = sharedDocumentsList.ItemCount;
context.Load(sharedDocumentsList);
context.ExecuteQueryAsync(OnFileWriteSucceeded, OnFileWriteFailed);
But I got still same problem.
The collection has not been initialized. It has not been requested or the request has not been executed. It may need to be explixitly requested
How Can I get ItemCount. This is only a simple example I need to work with this list in cycle and so on. But I need to solve this main problem. How to work with this documentlist directly in button click method.
Thank You.
This works for me:
private List docList;
private void GetList()
{
var context = new ClientContext(ApplicationContext.Current.Url);
context.Load(context.Web);
docList = context.Web.Lists.GetByTitle("dokumenty");
context.Load(docList);
context.ExecuteQueryAsync(GetListSucceded, GetListFailed);
}
private void GetListFailed(object sender, ClientRequestFailedEventArgs e)
{
Dispatcher.BeginInvoke(() => MyErrorFunction();
}
private void GetListSucceded(object sender, ClientRequestSucceededEventArgs e)
{
Dispatcher.BeginInvoke(() => GetItemsCount());
}
private void GetItemsCount()
{
MessageBox.Show(docList.ItemCount.ToString());
}
Related
In my C# winforms application I am loading the data from the database using Task in the background, below is my code. But after uploading the data I am updating my UI control(s) which is BindingSource. This BindingSource component is connected to the DataGrid which will be updated as well.
what I need to know is that what I am doing is the right way or is there another better way to achieve the same goal.
private async void Form_Load(object sender, EventArgs e)
{
Task loadDataTask = Task.Factory.StartNew(() =>
{
// LoadData(); // loading the data from the database
});
await loadDataTask.ConfigureAwait(true);
FormBindingSource.DataSource = _businessObjecsCollection;
}
if you can afford it, make method which loads data asynchronous. then you will be able to change Form method as:
private async void Form_Load(object sender, EventArgs e)
{
await LoadDataAsync();
FormBindingSource.DataSource = _businessObjecsCollection;
}
maybe even
private async void Form_Load(object sender, EventArgs e)
{
FormBindingSource.DataSource = await LoadDataAsync();
}
otherwise current method seems fine
I have problem with awesomium web control 1.7.4 in WPF , when the user click links in page , awesomium navigate to targetURL , but i want to open that links in system default browser.
also I want to determine mailto:jondue#example.com to open this links in default Email client.
Please help me.
Thanks
Update :
I've been doing some more searching to solve my problem, after few days I founded that when the link has a target=_blank the event ShowCreatedWebView is fired. The main problem was about links without target=_blank. After that I'm able to find links without that cause firing event RequestBringIntoView.
private void Browser_ShowCreatedWebView(object sender, Awesomium.Core.ShowCreatedWebViewEventArgs e)
{
System.Diagnostics.Process.Start(Browser.TargetURL.AbsoluteUri);
}
and
private void Browser_RequestBringIntoView(object sender, RequestBringIntoViewEventArgs e)
{
if (Browser.TargetURL != new Uri("about:blank"))
{
System.Diagnostics.Process.Start(Browser.TargetURL.AbsoluteUri);
e.Handled = true;
}
}
I've been doing some more searching to solve my problem, after few days I founded that when the link has a target=_blank the event ShowCreatedWebView is fired. The main problem was about links without target=_blank. After that I'm able to find links without that cause firing event RequestBringIntoView.
private void Browser_ShowCreatedWebView(object sender, Awesomium.Core.ShowCreatedWebViewEventArgs e)
{
System.Diagnostics.Process.Start(Browser.TargetURL.AbsoluteUri);
}
and
private void Browser_RequestBringIntoView(object sender, RequestBringIntoViewEventArgs e)
{
if (Browser.TargetURL != new Uri("about:blank"))
{
System.Diagnostics.Process.Start(Browser.TargetURL.AbsoluteUri);
e.Handled = true;
}
}
You could try using IResourceInterceptor to decide what you want to do when Awesomium loads a page.
public partial class MainWindow : Window
{
public MainWindow()
{
WebCore.Initialize(new WebConfig());
WebCore.Initialized += ((object sender, CoreStartEventArgs e) =>
{
WebCore.ResourceInterceptor = new ResourceInterceptor("http://google.com/");
});
InitializeComponent();
}
}
public class ResourceInterceptor : IResourceInterceptor
{
//Url of the first page to be loaded inside webcontrol without redirection.
protected string m_startupURL;
public ResourceInterceptor(string startupURL)
{
m_startupURL = startupURL;
}
public virtual bool OnFilterNavigation(NavigationRequest request)
{
if (request.Url.ToString() != m_startupURL)
{
System.Diagnostics.Process.Start(request.Url.ToString());
return true;
}
return false;
}
public ResourceResponse OnRequest(ResourceRequest request)
{
return ResourceResponse.Create(request.Url.OriginalString);
}
}
This is a very basic implementation. You should add some additional tests on the Url. Actually Process.Start(request.Url.ToString()) can do anything (launching an application or formatting your disk). So don't forget to test whether it is a valid Url or a mailto: link.
I have a dynamically generated hyperlink which when clicked should open a lotus notes document. I do it using the code below.
HyperlinkButton hlb = new HyperlinkButton();
hlb.SetBinding(HyperlinkButton.ContentProperty, new Binding("Properties[" + col.DisplayField + "]"));
hlb.SetBinding(HyperlinkButton.NavigateUriProperty, new Binding("Properties[" + col.LinkField + "]"));
hlb.Click += new RoutedEventHandler(hlb_Click);
RootGrid.Children.Add(hlb);
this is the code that fires when the link is clicked.
static void hlb_Click(object sender, RoutedEventArgs e)
{
HyperlinkButton hlb = (HyperlinkButton)sender;
var hostingWindow = HtmlPage.Window;
hostingWindow.Navigate(hlb.NavigateUri);
}
the lotus notes document opens correctly but I get a System.InvalidOperationException, the details of which are given below
Description: Failed to navigate to notes://<path to the document>
Stacktrace:
at MS.Internal.NavigationHelper.Navigate(Boolean checkUserInitiatedAction)
at System.Windows.Controls.HyperlinkButton.OnClick()
at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
at System.Windows.Controls.Control.OnMouseLeftButtonUp(Control ctrl, EventArgs e)
at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)
Another interesting thing to note is that it is raised on another thread and hence is not caught when the hostingWindow.Navigate method is fired.
Any ideas ?
Using Silverlight 5, I wrapped the call to open the Lotus Notes doc link within a task and was able to open the link without generating an error.
private void TryOpenDocLink()
{
TaskScheduler ts = TaskScheduler.Default;
Task<bool> task = OpenDocLink();
task.ContinueWith(t =>
{
if (t.Exception != null)
{
this.SetError(t.Exception.Message, enMessageLevel.Error);
}
});
}
private Task<bool> OpenDocLink()
{
TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
try
{
var hostWindow = HtmlPage.Window;
hostWindow.Navigate(new Uri(DocLinkPath));
tcs.SetResult(true);
}
catch (Exception)
{
tcs.SetResult(false);
}
return tcs.Task;
}
Try marking the click event as handled:
static void hlb_Click(object sender, RoutedEventArgs e)
{
e.Handled = true;
HyperlinkButton hlb = (HyperlinkButton)sender;
var hostingWindow = HtmlPage.Window;
hostingWindow.Navigate(hlb.NavigateUri);
}
I'm not sure that this will fix the issue. The error is coming from the click event code inside the hyperlink button. You can tell because that code uses the NavigationHelper class while the Window.Navigate method does not.
Is there a reason you're not just letting the hyperlink button do the navigation?
So I have a datagrid in Silverlight that is bound to a WCF that populates a list of class. I basically a pass a parameter to a Linq query. When I do a second query I get double the results, a third triple and so forth. What can I do to make it so when I send a call out to the service that I only get one set of results. I have attached my code in case it helps anyone.
private void button1_Click(object sender, RoutedEventArgs e)
{
dgOrder.ItemsSource = null;
Uri address = new Uri(Application.Current.Host.Source, "../Services/Service1.svc");
//var client = new Services.dataserviceClient("CustomBinding_dataservice", address.AbsoluteUri);
var client = new ServiceReference2.Service1Client("CustomBinding_Service1", address.AbsolutePath);
client.GetOrderCompleted += (s, ea) =>
{
dgOrder.AutoGenerateColumns = false;
//dgOrder.ColumnWidth.Value = 100;
dgOrder.Columns.Add(CreateTextColumn("SKU", "SKU"));
dgOrder.Columns.Add(CreateTextColumn("productname", "Product Name"));
dgOrder.Columns.Add(CreateTextColumn("itemnumber", "Item Number"));
dgOrder.Columns.Add(CreateTextColumn("cost", "Cost"));
dgOrder.Columns.Add(CreateTextColumn("asin", "ASIN"));
dgOrder.Columns.Add(CreateTextColumn("pendingorder", "Rank"));
dgOrder.Columns.Add(CreateTextColumn("rank", "Node"));
//dgOrder.Columns.Add(CreateTextColumn("w4", "AMZN"));
dgOrder.Columns.Add(CreateTextColumn("amazon", "AMZN"));
dgOrder.Columns.Add(CreateTextColumn("ourprice", "OurPrice"));
dgOrder.Columns.Add(CreateTextColumn("bbprice", "BuyBox"));
dgOrder.Columns.Add(CreateTextColumn("afner", "AFN"));
dgOrder.Columns.Add(CreateTextColumn("quantity", "INV"));
dgOrder.Columns.Add(CreateTextColumn("w4", "W4"));
dgOrder.Columns.Add(CreateTextColumn("w3", "W3"));
dgOrder.Columns.Add(CreateTextColumn("w2", "W2"));
dgOrder.Columns.Add(CreateTextColumn("w1", "W1"));
dgOrder.Columns.Add(CreateTextColumn("order", "Order"));
dgOrder.Columns.Add(CreateTextColumn("total", "Total"));
dgOrder.Columns.Add(CreateTextColumn("profit", "Profit"));
dgOrder.Columns.Add(CreateTextColumn("percent", "Percent"));
dgOrder.Columns.Add(CreateHyperlink("asin"));
dgOrder.ItemsSource = ea.Result;
Original = ea.Result;
};
client.GetOrderAsync(txtCompany.Text);
}
The problem is , you are creating a new(duplicate) event handler every time you press the Button. Due to having an extra event for each button pres you do, you get extra sets of data. You need to create your Event.Completed method outside the Button.Cliked event.
To clarify:
public partial class NewPage : Page
{
Uri address = new Uri(Application.Current.Host.Source, "../Services/Service1.svc");
ServiceReference2.Service1Client client = new ServiceReference2.Service1Client("CustomBinding_Service1", address.AbsolutePath);
public NewPage()
{
client.GetOrderCompleted += (s, ea) =>
{
//YOUR CODE
};
}
private void button1_Click(object sender, RoutedEventArgs e)
{
dgOrder.ItemsSource = null;
client.GetOrderAsync(txtCompany.Text);
}
}
I've figured out how to make everything red as soon as the page is finished loading:
private void webBrowser1_LoadCompleted(object sender, NavigationEventArgs e)
{
var doc = (IHTMLDocument2)webBrowser1.Document;
foreach (IHTMLElement elem in doc.all)
{
elem.style.backgroundColor = "#ff0000";
}
}
Now what if I want to make the element only change color when it's clicked? I see that elem has an onclick property, but it's type is dynamic so I don't know what to do with it. The documentation is pretty useless.
You could do it by using the HTMLDocumentClass instead of the IHTMLDocument2 interface:
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void webBrowser1_LoadCompleted(object sender, NavigationEventArgs e)
{
mshtml.HTMLDocumentClass doc = (mshtml.HTMLDocumentClass)webBrowser1.Document;
doc.HTMLDocumentEvents_Event_onclick += new mshtml.HTMLDocumentEvents_onclickEventHandler(OnClickHandler);
}
bool OnClickHandler()
{
mshtml.HTMLDocumentClass doc = (mshtml.HTMLDocumentClass)webBrowser1.Document;
mshtml.IHTMLWindow2 win = doc.parentWindow;
win.#event.srcElement.style.backgroundColor = "#ff0000";
return false;
}
}
The above solution, has one drawback: the onclick event does not bubble, even though false is returned (i.e. clicking at hyperlinks does not navigate to other pages).