How to use ModernWPF Programaticially - wpf

ModernWPF is a great project, as it restyles my WPF app in modern UI style. The docs tell what XAML changes to be done to activate the styling. Unfortunately I am working with an application that is not XAML based (Python.NET).
I tried already to add the dictionaries programaticially like:
app = Application()
app.Resources.MergedDictionaries.Add(ModernWpf.ThemeResources())
app.Resources.MergedDictionaries.Add(ModernWpf.Controls.XamlControlsResources())
app.Run(win)
The main window win is created via XAML:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ui="http://schemas.modernwpf.com/2019"
Title="Enterprisey-FireDrop Express" Width="640" Height="480"
ui:WindowHelper.UseModernWindowStyle="True">
...
</Window>
Any hints?

In C# the following seems to work, but I'm not sure if the same can be done in Python since I don't know Python at all:
var app = new Application();
var themeResources = new ModernWpf.ThemeResources();
((ISupportInitialize)themeResources).BeginInit();
app.Resources.MergedDictionaries.Add(themeResources);
((ISupportInitialize)themeResources).EndInit();
app.Resources.MergedDictionaries.Add(new ModernWpf.Controls.XamlControlsResources());
app.Run(new MainWindow());

Related

How can I use WPF menus and dialog boxes in F#?

I've been trying to find an example of using XAML and F# - without C# - to set up traditional menus and dialog boxes. Everything I can find online either uses C# or it is old, before the most recent versions of F# and .NET. Can anyone suggest an example I can look at? Thanks.
When you try to learn WPF, you come across many C# examples based on "good old" code behind rather than MVVM or MVC. The following explains how to quickly create an F# WPF code behind application. Using this, it becomes easier to experiment with all those examples.
Create an F# console application.
Change the Output type of the application to Windows Application.
Add FsXaml from NuGet.
Add these four source files, and arrange them in this order.
MainWindow.xaml
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="First Demo" Height="200" Width="300">
<Canvas>
<Button Name="btnTest" Content="Test" Canvas.Left="10" Canvas.Top="10" Height="28" Width="72"/>
</Canvas>
</Window>
MainWindow.xaml.fs
namespace FirstDemo
type MainWindowXaml = FsXaml.XAML<"MainWindow.xaml">
type MainWindow() =
inherit MainWindowXaml()
let whenLoaded _ =
()
let whenClosing _ =
()
let whenClosed _ =
()
let btnTestClick _ =
this.Title <- "Yup, it works!"
()
do
this.Loaded.Add whenLoaded
this.Closing.Add whenClosing
this.Closed.Add whenClosed
this.btnTest.Click.Add btnTestClick
App.xaml
<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Application.Resources>
</Application.Resources>
</Application>
App.xaml.fs
namespace FirstDemo
open System
open System.Windows
type App = FsXaml.XAML<"App.xaml">
module Main =
[<STAThread; EntryPoint>]
let main _ =
let app = App()
let mainWindow = new MainWindow()
app.Run(mainWindow) // Returns application's exit code.
Delete the Program.fs file.
Change the Build Action to Resource for the two xaml files.
Add a reference to the .NET assembly UIAutomationTypes.
Compile and run.
You can't use the designer to add event handlers. Simply add them manually in the code behind.
StackOverflow is possibly not the best place to post complete demos like this one, especially if this spins off more questions along the same line. If there is another better place, e.g. a public repo for this kind of thing, please let me know.

Left-click doesn't work on web canvas being hosted in WPF WebBrowser

I'm trying to present vis.js web page in my WPF using WebBrowser, the page presented successfully but I can left-click any of the items inside the canvas.
Mouse zoom is working as expected inside the canvas.
I wrote a simple project contains only 2 files:
MainWindow.xaml:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<WebBrowser x:Name="webBrowser"></WebBrowser>
</Grid>
</Window>
MainWindow.xaml.cs:
using System.Windows;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
webBrowser.Navigate("http://visjs.org/examples/network/other/configuration.html");
}
}
}
Note: I added my registry the following key so my WPF process will use IE11:
Key name: HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Internet
Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION\processName.exe
Type: REG_DWORD Value Data: 11000 (decimal)
I won't answer your question directly but I'll give you some advice based on my experience, and that is: never use default wpf WebBrowser for anything even remotely serious. I tried to use it before many times for many different projects and always got some showstopper issue, no exceptions. And of course that is with IE11 (10,9 - I tried it many times as I said) emulation.
Look at your case for example. Not only canvas clicks do not work - whole page is completely destroyed. Sliders are messed up, if you try to choose a color - clicks again do not work, and many more issues on this single page.
So, don't bother with wpf WebBrowser and just use real browser, for example Chromium. I personally prefer CefSharp which has wpf binding to chromium. Just install their nuget package (CefSharp.WPF), then:
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:wpf="clr-namespace:CefSharp.Wpf;assembly=CefSharp.Wpf"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<wpf:ChromiumWebBrowser x:Name="webBrowser"/>
</Grid>
</Window>
And
InitializeComponent();
webBrowser.Address = "http://visjs.org/examples/network/other/configuration.html";
And you page just displays as it should. Canvas clicks work, no styles are destoyed, color pickers work. In addition to all that, cefsharp browser control is much more flexible. You can call to\from javascript easily, intercept almost every event (resource load, redirections), you can disable javascript\images, even intercept requests and feed completely different data. And no need to fix registry on every client machine.
Note that if you would install CefSharp.WPF via package, it will require your project to have x86\x64 platform (does not work with AnyCPU). But you really can make it work with anyCPU with little effort, if you would have such requirement.

Why can't I launch a WPF app written in F#?

I receive the following warning when attempting to build a WPF app written in F#:
Main module of program is empty: nothing will happen when it is run
As a result, I am unable to launch the application.
I have verified that all AssemblyInfo.fs files has a "do()" at the end.
I have played around with the order of files as well.
Any suggestions?
UPDATE
I Added a file to the end of my project.
The file has the following code:
module Bootstrap
open System.Windows
[<EntryPoint>]
let main args =
System.Windows.Application.Current.Run() |> ignore;
// Return 0. This indicates success.
0
When I attempt to run the file though, I hit a null reference exception on Application.Current.
My solution looks like this:
Here's a minimal working example, using FSharp.ViewModule.Core and FsXaml.Wpf packages:
MainWindow.xaml:
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:local="clr-namespace:ViewModel;assembly=fsxamltest">
<Window.DataContext>
<local:MainViewModel/>
</Window.DataContext>
</Window>
ViewModel.fs:
namespace ViewModel
open FSharp.ViewModule
type MainViewModel() as me = inherit ViewModelBase()
App.fs:
open System
type App = FsXaml.XAML<"App.xaml">
[<STAThread;EntryPoint>]
let main _ = App().Root.Run()
App.xaml:
<Application
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml"/>
If that doesn't work, please show us the full code or create MCVE.

Why is my very minimal WPF app hanging?

I have what is as yet a very minimal (just started) WPF app that uses Bing Maps. This is all there is to it so far:
<Window x:Class="DataMapper.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:m="clr-namespace:Microsoft.Maps.MapControl.WPF;assembly=Microsoft.Maps.MapControl.WPF"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Data Mapper" Height="532" Width="798" WindowStartupLocation="CenterScreen" >
<Grid>
<m:Map x:Name="dataMapper" ZoomLevel="10" CredentialsProvider="MyKeyWhichWorksInWindows8Apps" Mode="Aerial" ></m:Map>
</Grid>
</Window>
Yet, when I run it does display a map (very faintly, I might add), but it is unresponsive/hangs. I had to Ctrl+Alt+Delete to get it to shut down.
Try using the updated WPF control which has a bunch of bug fixes: http://www.microsoft.com/en-us/download/details.aspx?id=27165
There is also a Nuget package available now: https://www.nuget.org/packages/Microsoft.Maps.MapControl.WPF/1.0.0.3

Silverlight Won't Load PivotViewer Collection

I'm trying to get a proof-of-concept PivotViewer application up and running, but I can't get the collection to load properly, even in a testing environment. I'm following these instructions:
Building Your First PivotViewer Application
I have a clientaccesspolicy XML file in the root, giving all URIs access. I copied an already existing collection, and have it in the root as well. The collection XML checks out properly.
Whenever I attempt to debug the application, however, it loads but simply displays the URL for the collection, never actually building the PivotViewer.
MainPage.xaml:
<UserControl x:Class="PivotViewer.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:Pivot="clr-namespace:System.Windows.Pivot;assembly=System.Windows.Pivot"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White">
<Pivot:PivotViewer x:Name="Pivot" />
</Grid>
</UserControl>
MainPage.xaml.cs:
namespace PivotViewer
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
Pivot.LoadCollection("http://localhost:55334/Top_Movies.cxml", string.Empty);
}
}
}
OK, here's a number of ways forward for you.
Firstly, wire up the PivotViewer's CollectionLoadingFailed event before calling the LoadCollection method. This will expose a CollectionErrorEventArgs containing any exceptions.
Secondly, install Fiddler2 and use it to watch what the PivotViewer is requesting. If it's failing to reach certain parts of the pivot collection it will be very obvious.
Let me know what those 2 suggestion bring to light.

Resources