In my small WPF app (F# only) I would like to remember the window size and location after closing. This C# solution suggest to use the projects settings IDictinary for User.config. This looks like the simple approach that I am after but I did not find the project settings in my F# project. Do they exist for F# projects?
I tried this but it does not work: (The save() call as in the C# example is not available.)
let getSetting k def =
if Application.Current.Properties.Contains k
then Application.Current.Properties.Item k
else def
let window = System.Windows.Window()
// is box() the best wax to make floats into obj ?
window.Top <- getSetting "WindowTop" (box 0.0) |> unbox
window.Left <- getSetting "WindowLeft" (box 0.0) |> unbox
window.Height <- getSetting "WindowHeight" (box 800.0) |> unbox
window.Width <- getSetting "WindowWidth" (box 800.0) |> unbox
window.Closing.Add( fun _ ->
Application.Current.Properties.Add("WindowTop",window.Top)
Application.Current.Properties.Add("WindowHeight",window.Height)
Application.Current.Properties.Add("WindowLeft",window.Left)
Application.Current.Properties.Add("WindowWidth",window.Width)
//Application.Current.Properties.Save() // not available!
)
I know I could use a type provider but I would like to keep it simple and without dependencies if possible. Is there a built in way to persist some user values in an F# WPF app?
As #mm8 points out, the approach you mention depends on the Settings.settings file included in the project template of a C# WPF app.
The F# template does not provide such functionality, which is unfortunate given the XAML support is pretty cool. Instead, you could resort to the App.config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
...
<appSettings>
<add key="WindowTop" value="0" />
<add key="WindowLeft" value="0" />
<add key="WindowHeight" value="350" />
<add key="WindowWidth" value="525" />
</appSettings>
</configuration>
If you don't want to depend on FSharp.Configuration.dll you can use the ConfigurationManager (note you still need to add a reference to System.Configuration.dll).
open System.Configuration
type UserSettings = {
WindowTop : float
WindowLeft : float
WindowHeight : float
WindowWidth : float
} with
static member Load() =
let config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
{ // To do: add validation
WindowTop = float config.AppSettings.Settings.["WindowTop"].Value
WindowLeft = float config.AppSettings.Settings.["WindowLeft"].Value
WindowHeight = float config.AppSettings.Settings.["WindowHeight"].Value
WindowWidth = float config.AppSettings.Settings.["WindowWidth"].Value
}
member this.Save() =
let config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
config.AppSettings.Settings.["WindowTop"].Value <- string this.WindowTop
config.AppSettings.Settings.["WindowLeft"].Value <- string this.WindowLeft
config.AppSettings.Settings.["WindowHeight"].Value <- string this.WindowHeight
config.AppSettings.Settings.["WindowWidth"].Value <- string this.WindowWidth
config.Save()
Now you can:
open System.Windows
let window = new Window()
let settings = UserSettings.Load()
window.Top <- settings.WindowTop
window.Left <- settings.WindowLeft
window.Height <- settings.WindowHeight
window.Width <- settings.WindowWidth
window.Closing.Add( fun _ ->
{
WindowTop = window.Top
WindowLeft = window.Left
WindowHeight = window.Height
WindowWidth = window.Width
}.Save() )
Thanks to the answer of Funk I came up with this as the minimal general solution using System.Configuration.dll:
module Config =
open System.Configuration
let private config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
let save key value =
try config.AppSettings.Settings.[key].Value <- value
with _ -> config.AppSettings.Settings.Add(key,value) // in case key does not exist yet
config.Save()
let load key =
try Some config.AppSettings.Settings.[key].Value
with _ -> None
Related
I am tinkering with using F# scripts and I'm just wanting to draw lines on a blank Windows Form with a simple button click. Hopefully you can see what I'm trying to do here:
open System.Drawing
open System.Windows.Forms
let form = new Form(Width = 400, Height = 400, Text = "draw test")
let panel = new FlowLayoutPanel()
form.Controls.Add(panel)
let paint(e : PaintEventArgs) =
let pen = new Pen(Color.Black);
e.Graphics.DrawLine(pen, new PointF(100.0f, 100.0f), new PointF(200.0f, 200.0f))
let button = new Button()
button.Text <- "Click to draw"
button.AutoSize <- true
button.Click.Add(fun _ -> form.Paint.Add(paint)) // <- does not draw a line on click
panel.Controls.Add(button)
//form.Paint.Add(paint) <- here, if uncommented, it will draw a line when the script is run
form.Show()
If I take the form.Paint.Add(paint) uncomment it above form.Show(), then of course it will draw on the form, but I'm trying to do it with a button click. It's not exactly clear to me how to make this happen in a script like this, and I've been scouring all over for a similar example in F#. Any help would be appreciated.
If you add your Paint event handler before the form is drawn for the first time, then it will draw using that handler.
If you add it after, you need to make sure the form then redraws itself. You could for instance call Refresh or Invalidate on it.
Ex.:
button.Click.Add(fun _ -> form.Paint.Add(paint); form.Invalidate())
Originally an edit, I moved it to the answer section:
Okay, so I was confused about the difference between WPF and Winforms due to the fact that I have seen the terms used together various places... #Asik has added an answer for Winforms, but here I have slapped together a working .fsx script specifically for WPF based on several FSharp Snippets (as well as several Google searches) which can also be compiled if so desired. I'll update this as needed or requested. Also, just to point out, the whole motivation behind this is to be able to quickly test drawing graphics via FSI.
#r #"PresentationCore"
#r #"PresentationFramework"
#r #"WindowsBase"
#r #"System.Xaml"
#r #"UIAutomationTypes"
open System
open System.Windows
open System.Windows.Media
open System.Windows.Shapes
open System.Windows.Controls
let window = Window(Height = 400.0, Width = 400.0)
window.Title <- "Draw test"
let stackPanel = StackPanel()
window.Content <- stackPanel
stackPanel.Orientation <- Orientation.Vertical
let button1 = Button()
button1.Content <- "Click me to draw a blue ellipse"
stackPanel.Children.Add button1
let button2 = Button()
button2.Content <- "Click me to draw a red ellipse"
stackPanel.Children.Add button2
let clearButton = Button()
clearButton.Content <- "Click me to clear the canvas"
stackPanel.Children.Add clearButton
let canvas = Canvas()
canvas.Width <- window.Width
canvas.Height <- window.Height
stackPanel.Children.Add canvas
let buildEllipse height width fill stroke =
let ellipse = Ellipse()
ellipse.Height <- height
ellipse.Width <- width
ellipse.Fill <- fill
ellipse.Stroke <- stroke
ellipse
let ellipse1 = buildEllipse 100.0 200.0 Brushes.Aqua Brushes.Black
Canvas.SetLeft(ellipse1, canvas.Width / 10.0) //messy, will fix at some point!
Canvas.SetTop(ellipse1, canvas.Height / 10.0)
let ellipse2 = buildEllipse 200.0 100.0 Brushes.Red Brushes.DarkViolet
Canvas.SetLeft(ellipse2, canvas.Width / 4.0)
Canvas.SetTop(ellipse2, canvas.Height / 5.0)
let addEllipseToCanvas (canvas:Canvas) (ellipse:Ellipse) =
match canvas.Children with
| c when c.Contains ellipse ->
canvas.Children.Remove ellipse
canvas.Children.Add(ellipse) |> ignore //needs to be removed and readded or the canvas complains
| _ ->
canvas.Children.Add(ellipse) |> ignore
button1.Click.Add(fun _ -> addEllipseToCanvas canvas ellipse1)
button2.Click.Add(fun _ -> addEllipseToCanvas canvas ellipse2)
clearButton.Click.Add(fun _ -> canvas.Children.Clear())
#if INTERACTIVE
window.Show()
#else
[<EntryPoint; STAThread>]
let main argv =
let app = new Application()
app.Run(window)
#endif
I want to combine 2 curves like this:
Then here is my code:
// Create a path to draw a geometry with.
Path myPath = new Path();
myPath.Stroke = Brushes.Black;
myPath.StrokeThickness = 1;
var gmy1 = (StreamGeometry)StreamGeometry.Parse("M100,100C110,118.333333333333 138.333333333333,206.666666666667 160,210 181.666666666667,213.333333333333 205,123.333333333333 230,120 255,116.666666666667 280,186.666666666667 310,190 340,193.333333333333 396.666666666667,156.666666666667 410,140 423.333333333333,123.333333333333 393.333333333333,98.3333333333333 390,90");
var gmy2 = (StreamGeometry)StreamGeometry.Parse("M180,241.25L180,241.25 230,290 300,246.66667175293 330,160");
var gmy = Geometry.Combine(gmy1, gmy2, GeometryCombineMode.Union, null);
myPath.Data = gmy;
// Add path shape to the UI.
this.panel1.Children.Add(myPath);
But the result is this:
How to combine the curves in WPF?
And because of the project limitation, we have to implement this without layout and xaml. That means we need the result type is Geometry.
More general than concatenating path strings:
If you have a set of arbitrary Geometries and want to group them, use a GeometryGroup:
Geometry gmy1 = ...;
Geometry gmy2 = ...;
var gmy = new GeometryGroup();
gmy.Children.Add(gmy1);
gmy.Children.Add(gmy2);
myPath.Data = gmy;
Easy:
Path myPath = new Path();
myPath.Stroke = Brushes.Black;
myPath.StrokeThickness = 1;
var gmy1 = (StreamGeometry)StreamGeometry.Parse("M100,100C110,118.333333333333 138.333333333333,206.666666666667 160,210 181.666666666667,213.333333333333 205,123.333333333333 230,120 255,116.666666666667 280,186.666666666667 310,190 340,193.333333333333 396.666666666667,156.666666666667 410,140 423.333333333333,123.333333333333 393.333333333333,98.3333333333333 390,90");
var gmy2 = (StreamGeometry)StreamGeometry.Parse("M180,241.25L180,241.25 230,290 300,246.66667175293 330,160");
var gmy = (StreamGeometry)StreamGeometry.Parse(gmy1.ToString() + gmy2.ToString());
myPath.Data = gmy;
// Add path shape to the UI.
this.panel1.Children.Add(myPath);
The path definition language is a language. Use it as one. StreamGeometry.ToString() unparses a Geometry back to its Path Definition Language representation, which you can then merge with another one.
Note that this works because each starts with a M for Move command: It starts a new line. I don't think there's any realistic case where you'd run into any trouble with that (and it won't let you start with L for Line), but theory's not exactly my strongest subject.
Just add both of them to a Grid or Canvas, Combine does a intersecting combination, you just seem to want to overlay them. Alternatively add both of them to a GeometryGroup and add that to your panel.
I have this really simple program to get RSS-feeds from a website and populate a listbox with the items. Whenever the user selects an item and presses Enter, it should go to a web-page. this is the KeyUp event handler!
rssList.KeyUp
|> Event.filter (fun e -> rssList.SelectedItems.Count > 0)
|> Event.filter (fun (args:Input.KeyEventArgs) -> args.Key = Key.Enter)
|> Event.add -> let feed = unbox<RSSFeed> rssList.SelectItem)
Process.Start(feed.Link) |> ignore)
What I'm getting is the following:
the first time the event triggers, it works fine, the browser open and a page is loaded
the second time it triggers TWO times, so now i get two browser windows opened and the page is loaded in both of them.
the third time I get Three browser . . . You get the idea!
Anybody any idea why this is happening? My goal is (you guessed it) just to open 1 browser window and 1 page PER trigger
There are several compilation errors in your example including a malformed lambda expression, mismatched parentheses, incorrect identitfiers (SelectItem is not a property, I'm assuming you mean SelectedItem not SelectedItems), and incorrect indentation following the let feed binding.
Below is a simplified example that works as you intended. The selected item in the top ListBox is put into the bottom ListBox when the user hits Enter.
open System
open System.Windows
open System.Windows.Controls
open System.Windows.Input
[<EntryPoint>]
[<STAThread>]
let main argv =
let panel = new DockPanel()
let listBox = new ListBox()
for i in [| 1 .. 10 |] do
listBox.Items.Add i |> ignore
DockPanel.SetDock(listBox, Dock.Top)
let listBox2 = new ListBox(Height = Double.NaN)
panel.Children.Add listBox |> ignore
panel.Children.Add listBox2 |> ignore
listBox.KeyUp
|> Event.filter (fun e -> listBox.SelectedItems.Count > 0)
|> Event.filter (fun e -> e.Key = Key.Enter)
|> Event.add (fun e -> let i = unbox<int> listBox.SelectedItem
listBox2.Items.Add(i) |> ignore)
let win = new Window(Content = panel)
let application = new Application()
application.Run(win) |> ignore
0
I got it working properly by implementing the Handled property of the event args with the following:
let doubleClick = new MouseButtonEventHandler(fun sender (args:MouseButtonEventArgs) ->
let listBox = unbox<ListBox> sender
match listBox.SelectedItems.Count > 0 with
| true ->
let listBox = unbox<ListBox> sender
let feed = unbox<RSSFeed> listBox.SelectedItem
Process.Start(feed.Link) |> ignore
args.Handled <- true; ()
| false ->
args.Handled <- true; ())
thanks to everyone who helped me here!
I cannot get the axis to format as currency, any idea?
What am I doing wrong? I need to be able to change the formatting on the fly and for this test I wanted to set it as currency for the Y axis on the scale of values.
Anyone?
Thanks...
var columnSeries = new ColumnSeries
{ Title = reportProcedureNode.Value,
IndependentValuePath = "PrimaryKey",
DependentValuePath = "Value",
IndependentAxis = new CategoryAxis { Orientation = AxisOrientation.X, ShowGridLines = false, Location = AxisLocation.Bottom},
DependentRangeAxis = new LinearAxis(){Orientation = AxisOrientation.Y, ShowGridLines = false}
};
var labelStyle = new Style(typeof(AxisLabel));
labelStyle.Setters.Add(new Setter(AxisLabel.StringFormatProperty, "{}{0:C0}"));
var axis = (LinearAxis)columnSeries.DependentRangeAxis;
axis.AxisLabelStyle = labelStyle;
In my WPF4 version of the charting toolkit, your code crashes. I needed to change:
labelStyle.Setters.Add(new Setter(AxisLabel.StringFormatProperty, "{}{0:C0}"));
to:
labelStyle.Setters.Add(new Setter(AxisLabel.StringFormatProperty, "{0:C0}"));
That is, remove the {}. The {} comes from markup extension syntax:
{} Escape Sequence / Markup Extension
and is only needed when being parsed by XAML as a markup extension inside "{...}".
Since you are setting the property directly, no markup extension is involved and including it prevents the real currency format from being seen.
Is the WPF event loop in this answer still a good one for FSI (besides rethrow which is now reraise)? The answer is from 2008 and I'm not sure if there are any "better" implementations around. If not what would one be?
My understanding is that the default implementation is for WinForms.
Yes the default is for Winforms, I do use the WpfEventLoop quite a lot, Code is below,
#I "c:/Program Files/Reference Assemblies/Microsoft/Framework/v3.0"
#I "C:/WINDOWS/Microsoft.NET/Framework/v3.0/WPF/"
#r "PresentationCore.dll"
#r "PresentationFramework.dll"
#r "WindowsBase.dll"
module WPFEventLoop =
open System
open System.Windows
open System.Windows.Threading
open Microsoft.FSharp.Compiler.Interactive
open Microsoft.FSharp.Compiler.Interactive.Settings
type RunDelegate<'b> = delegate of unit -> 'b
let Create() =
let app =
try
// Ensure the current application exists. This may fail, if it already does.
let app = new Application() in
// Create a dummy window to act as the main window for the application.
// Because we're in FSI we never want to clean this up.
new Window() |> ignore;
app
with :? InvalidOperationException -> Application.Current
let disp = app.Dispatcher
let restart = ref false
{ new IEventLoop with
member x.Run() =
app.Run() |> ignore
!restart
member x.Invoke(f) =
try
disp.Invoke(DispatcherPriority.Send,new RunDelegate<_>(fun () -> box(f ()))) |> unbox
with e -> eprintf "\n\n ERROR: %O\n" e; reraise()
member x.ScheduleRestart() = ()
//restart := true;
//app.Shutdown()
}
let Install() = fsi.EventLoop <- Create()
WPFEventLoop.Install()
Test code
open System
open System.Windows
open System.Windows.Controls
let window = new Window(Title = "Simple Test", Width = 800., Height = 600.)
window.Show()
let textBox = new TextBox(Text = "F# is fun")
window.Content <- textBox
Let me know if this helps.
-Fahad