I am trying something as simple as connecting to a webpage with the Webbrowser property and printing the html body tag to the console. Though apparently a so called DocumentCompleted Event handler is needed.
I have been trying for hours now to convert one of the thousands C# examples out there into F#.
[<STAThread>]
do
let webbrowser1 = new WebBrowser()
webbrowser1.Navigate(new Uri("https://www.google.dk/"))
webbrowser1.DocumentCompleted <- new
WebBrowserDocumentCompletedEventHandler(printfn "%A"
webbrowser1.Document.Body.InnerText)
Now I have to call SOS since im not getting anywhere.
The following snippet ran successfully in LINQPad. The System.Windows.Forms assembly had to be referenced.
let webbrowser1 = new System.Windows.Forms.WebBrowser()
webbrowser1.Navigate(new Uri("https://www.google.dk/"))
webbrowser1.DocumentCompleted.Add (fun _ ->
printfn "%A" webbrowser1.Document.Body.InnerText)
Related
I have a database that has a color in it. I want to take that color and apply it to dynamically generated buttons. It seems the way to do this is using SolidBrush. However - that's not defined. I tried including system.drawing, but it's still not available....
Dim newbutton As New Button
newbutton.Content = catArray(0, t)
newbutton.Height = 50
newbutton.Width = 175
newbutton.HorizontalAlignment = HorizontalAlignment.Left
newbutton.VerticalAlignment = VerticalAlignment.Top
Dim Brsh As New System.Drawing.SolidBrush(Color.FromArgb(readColor))
newbutton.Margin = New Thickness(y, l, 0, 0)
newbutton.Name = "cat" & catArray(2, t)
AddHandler newbutton.Click, AddressOf catHandler
grdCat.Children.Add(newbutton)
I tried adding System.Drawing as a reference to the project, but I get this error:
"A reference to "System.Drawing.dll could not be added.
The ActiveX type library c:\Windows\Microsoft.net\Framework\v4.0.30319\System.Drawing.tlb was exported from a .net assembly and cannot be added as a reference.
Add a reference to the .net assembly instead."
I tried to "Add a reference to the .net assembly" and got the same error...
I'm sure this is something stupid - all i want to do is change a button color, which was super easy in winforms...
Thanks for your help!!!
I am trying to print a WPF FlowDocument to a particular printer, without prompting the user. The printer is a PDF converter.
This works great except that it prints to the default printer:
PrintDialog pd = new PrintDialog();
var doc = ((IDocumentPaginatorSource) RTB.Document).DocumentPaginator;
// I would like to explicitly set the printer to print to here.
pd.PrintDocument(doc, "Print Document");
In WinForms there is a System.Drawing.Printing.PrinterSettings object on document which has a PrinterName property which can be set to the printer I want, but I don't see that in WPF.
You first need a reference in your project to System.Printing. Then you can use the following code right after you declare your PrintDialog object.
pd.PrintQueue = new PrintQueue(new PrintServer(), "The exact name of my printer");
The PrintQueue is an object that represents the printer and everything else about that print queue.
This worked for me, when I used a shared network printer:
xPrintDialog.PrintQueue = New PrintQueue(New PrintServer("\\computer name"), "printer name")
I have a Silverlight application in which I display some phone numbers. I want that numbers to be displayed as links and when the user clicks on the links to be redirected to skype. (The same as on html).
This is the equivalent in html (just to understand what i need):
+11 11 111 11 11
In silverlight I tried with:
<HyperlinkButton Content="{Binding}" NavigateUri="{Binding StringFormat=callto:\{0\}}" />
but I get System.InvalidOperationException: Failed to navigate to callto:+11 11 111 11 11.
Does somebody knows a solution for this?
Can you try using Javascript to invoke that sort of anchor? If you're able to do this via Javascript, try using the Eval function to invoke the Javascript from Silverlight:
HtmlPage.Window.Eval();
This may be a little late, but if you still want to keep it in Silverlight code, then this will work:
Public Class MyHyperLink : Inherits HyperlinkButton
Sub New(ByVal uri As String)
MyBase.NavigateUri = New Uri(uri)
End Sub
Public Sub Execute()
Application.Current.Host.Content.IsFullScreen = False
MyBase.TargetName = "_blank"
MyBase.OnClick()
End Sub
End Class
And to call, add the following code::
Dim nav As New MyHyperLink(URL)
nav.Execute()
update 5: brians solution worked:
namespace Module1
type Page1() as this =
inherit UserControl()
let uriStr = "/FSSilverlightApp;component/Page1.xaml"
let uri = new System.Uri(uriStr, System.UriKind.Relative)
do
Application.LoadComponent(this, uri)
member public this.Uri with get () = uri
type MyApp() as this =
inherit Application()
do Application.LoadComponent(this, new System.Uri("/FSSilverlightApp;component/App.xaml", System.UriKind.Relative))
let nav : Frame = siteTemplate ? contentFrame
let p1 = new Module1.Page1() ;
member this.navigate ea =
nav.Navigate(p1.Uri)
<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="Module1.MyApp">
<Application.Resources>
</Application.Resources>
</Application>
<UserControl x:Class="Module1.Page1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid x:Name="LayoutRoot" Background="White">
<TextBlock Text="This is page 1 Lets see if we can ever get here!!!" FontSize="24" />
</Grid>
</UserControl>
update 4:
the template Brian has mentioned is mostly doing the trick. I still have a more complex page that is giving me troubles - yet it is most likely my code. Once my code is complete I will post what I can diagnose but the parts include:
Setting App.XAML correctly (referencing your application object correctly)
in post construction of your application object use Application.Load to load the App.xaml
In your application object create instances of your page xaml
in post construction of your page objects use Application.Load to load the individual page.xaml
each of your page objects should extend UserControl; I suspect this is not truly the case - once I get more more complex page running I will see if removing this restriction will have an effect.
update 3:
I implemented my own controller logic in the Application object, which seems to do part of the trick (and solve my needs for a prototype anyhow).
type Page1() as this =
inherit Page()
do
this.Content <- loadXaml("Page1.xaml")
type MyApp() as this =
inherit Application()
let cc = new ContentControl()
let mainGrid : Grid = loadXaml("MainWindow.xaml")
let siteTemplate : Grid = if mainGrid.Name = "siteTemplate" then mainGrid else mainGrid ? siteTemplate
let nav : Frame = siteTemplate ? contentFrame
let page1 = new Module1.Page1() :> Page ;
let page2 = new Module1.Page2() :> Page ;
let page3 = new Module1.Page3() :> Page ;
do
this.Startup.Add(this.startup)
// to be able to get focus
cc.IsTabStop <- true
cc.IsEnabled <- true
System.Windows.Browser.HtmlPage.Plugin.Focus()
cc.Content <- mainGrid
this.RootVisual <- cc
member this.startup ea =
menu.MenuItemClicked.Add(this.navigate)
resolutionSlider.SizeChanged.Add(this.resizeTemplate)
member this.navigate ea =
if ea.Index = 1 then nav.Content <- page1
elif ea.Index = 2 then nav.Content <- page2
elif ea.Index = 3 then nav.Content <- page3
It works... I don't know the implication on memory / performance. I wonder if the navigation fw handles the construction / destruction of page objects more efficiently than what I did. I think the navigation FW works nicely with the browsers back and forward buttons - which my solution doesn't.
update 2: it looks as though teh C# applciation implments
public void InitializeComponent()
which loads and the XAML. Though I am no IL expert; I will make the similar changes on the F# side... I wonder if it is the partial class concept. One theory I am working on is:
page.xaml.cs is definitely a partial class - you can read it in the source.
page.xaml has an attribute that refers back to the c# class. I wonder if the special build commands treat this as a partial class - by parsing it and creating 1) any member component references 2) intialComponent() method which registers the page wherever it needs to be registered?
Update 1: After a nights sleep the problem can be stated more accurately as I have a 100% f# / silverlight implementation and am looking to use the built in Navigation components. C# creates page.xaml and page.xaml.cs um - ok; but what is the relationship at a fundamental level? How would I go about doing this in f#?
The applcuation is loaded in the default module, and I pull the XAML in and reference it from the application object. Do I need to create instances / references to the pages from within the application object? Or set up some other page management object with the proper name value pairs?
When all the Help of VS is stripped away - what are we left with?
original post (for those who may be reading replies)
I have a 100% silverlight 3.0 / f# 2.0 application I am wrapping my brain around. I have the base application loading correctly - and now I want to add the naigation controls to it.
My page is stored as an embedded resource - but the Frame.Navigate takes a URI. I know what I have is wrong but here it is:
let nav : Frame = mainGrid ? mainFrame
let url = "/page1.xaml"
let uri = new System.Uri(url, System.UriKind.Relative) ;
nav.Navigate uri
Any thoughts?
Have you tried making the Xaml a file in the project with a BuildAction of Content rather than an EmbeddedResource? Honestly, I've no clue if that works, but it might get packaged into the .xap that way, and then the relative uri might work. How would it work in a C# project? Try that.
EDIT
Aha, Dmitry's template appears to have this figured out. He has Xaml files with BuildAction of Resource, and then code like
type MainPage() as this =
inherit UserControl()
do
Application.LoadComponent(this,
new System.Uri("/SilverlightApplication3;component/Page.xaml",
System.UriKind.Relative))
let layoutRoot : Grid = downcast this.FindName("LayoutRoot")
do
()
to load it.
I have a background thread updating an array. At timed intervals I call myDataGrid.Items.Refresh(). However nothing changes on the screen. But when I for instance click on the column heading of the data grid on the screen the information is actualized immediately.
I like to see the changes on the screen at timed intervals, for instance every 2 seconds. What am I missing?
Here is the code fragment in F# that shows the situation:
...
let win = new Window()
let grid = DataGrid()
grid.HeadersVisibility <- DataGridHeadersVisibility.All
grid.ItemsSource <- myArray
win.Content <- new ScrollViewer(Content=grid)
win.Show()
...
// Background thread A
// updating myArray
...
// Background thread B
let updateDataGrid =
grid.Items.Refresh()
Thread.Sleep(5000)
updateDataGrid
...
[<STAThread>]
do
let app = new Application()
app.Run() |> ignore
Have you tried a DispatcherTimer? (code below is in C#)
timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(1);
timer.Tick += timer1_Tick;
Prevent the usage of Thread.Sleep.
Can't use formatting in comments so here my response.
The timer actually works, if I write:
[<STAThread>]
do
let app = new Application()
let timer = new DispatcherTimer()
timer.Interval <- TimeSpan.FromSeconds(2.)
timer.Tick.Add (fun _ -> grid.Items.Refresh())
timer.Start()
app.Run() |> ignore
However now the problem is almost reversed, it automatically updates until I click on any of the column headings to sort. After that there are no more refreshes.
However if I perform this trick:
timer.Tick.Add (fun _ ->
grid.ItemsSource <- null
grid.ItemsSource <- myArray
grid.Items.Refresh())
it refreshes fine, but loses the sort ordering.
How to maintain the sort order? I can easily sort the arrray but I do like the user to sort by himself as well.
Since you're already using WPF, is it possible to turn that array into an ObservableCollection? Last I heard, the DataGrids support it. If the objects in the collection are already DependencyObjects, then their DependancyProperties should automatically update in the grid. If not, you can reinsert them into the collection manually.