Create WPF BrowserView defined in XAML with custom BrowserContext - wpf

I've got a DotNetBrowser instance defined in a XAML file
<Grid>
<wpf:WPFBrowserView x:Name="BrowserView"></wpf:WPFBrowserView>
</Grid>
The application is used by multiple people, which is causing issues due to the issue discussed here:
Chromium profile directory is already used/locked by another browser
Is it possible to use XAML to define the browser control and still assign a custom context to the browser instance?

Is it possible to use XAML to define the browser control and still assign a custom context to the browser instance?
No, I am afraid it's not.
The Browser property of the WPFBrowserView class doesn't have a public setter so you must create the custom Browser and the BrowserContext programmatically:
BrowserContextParams params1 = new BrowserContextParams("C:\\my-data1");
BrowserContext context1 = new BrowserContext(params1);
Browser browser1 = BrowserFactory.Create(context1);
XAML doesn't support anything like calling BrowserFactory.Create(context1).

Unfortunately, the custom BrowserContext can be configured only if the Browser and WPFBrowserView were created from the source code.
The possible approach is to wrap WPFBrowserView and its non-default initialization into a custom control that manages instantiating and disposal of the WPFBrowserView, make this control expose all the necessary properties and then insert it into your XAML.

Related

Limitations of using WebBrowser control to create WPF apps [duplicate]

I've recently stumbled across WebBrowser WPF control. I am interested if there are any limitations when building user interfaces with HTML+CSS+JS and embedding them in WebBrowser.
So far I've successfully
intercepted HTML click events and handled them in C# using
[ComVisible(true)] class attribute
called JS script from C# using InvokeScript method.
InvokeScript also supports sending objects to the HTML/JS via parameters which faciliates two way communication.
Is there any reason for not using using WebBrowser to create simple apps? I am intersted in parts that absolutely need to be handled with some WPF code and cannot be bridged using WebBrowser.

Should Prism RequestNavigate work with no UI?

I have a Prism 4.0 enabled WPF application that uses RequestNavigate extensively, and it is working well. I have a scenario where I would like to render part of my UI to an image and store it for later use from a Windows service. I already know how to use RenderTargetBitmap to generate the image, but whenever my code tries to call RequestNavigate, nothing happens. I am calling the bootstrapper, so I would expect that all of the types are loaded, but it just is not working. Can anyone tell me if this should even be possible? Is there anything inherent to RequestNavigate that prevents it from working when there is no UI present?
Individual steps:
First, I call Run on my MefBootstrapper. This loads up all of the assemblies into the AggregateCatalog.
Then, I use MEF CompositionContainer.GetExportedValue to create a WPF UserControl that has a single ContentControl that is assigned a RegionManager.RegionName. This always creates the initial UserControl just fine.
Finally, I call MefRegionManager.RequestNavigate with the region name on my UserControl and the path to another UserControl that I want it to load. This fails to load the UserControl that I am attempting to navigate to.
If these are the only steps that I follow, then the final UserControl fails to load whether I am running from a Windows Service or from within my WPF application. However, if I call SetRegionManager to explicitely add the region from my host UserControl before calling RequestNavigate, then the last UserControl will load properly, as long as the code is run from within the WPF application. If this same code is run from my Windows service, then it still does not load.
RequestNavigate is exactly that, a request to navigate to a loaded (but inactive) region. It won't do any loading itself. You need to separately manage the loading of views (including views within views).
If your nested user control is only over loaded within the parent, and you don't need to manage it at runtime, then you can use ViewDiscovery. You register all the views in advance, and then when the region is created, it looks for (and loads) all the nested views. If you need to manage the views at runtime, switch them in and out, etc, then you can use ViewInjection.
For ViewDiscovery, in the Initialize method of the ModuleInit class in your module, insert the following line:
_regionManager.RegisterViewWithRegion("RegionNameOfYourNestedControl",
() => this.container.Resolve<NestedUserControl>());
Then when your region is loaded, the NestedUserControl will automatically be loaded into your ContentControl (region).
For more detail on ViewDiscovery, ViewInjection and UI composition, have a read of the prism documentation

how to set startup uri property to another window in folder in wpf project

In my wpf project i have created a folder called practice, in that folder i added a window, now i want to run that window, so in app.xaml file i set the startup uri to foldername.window.xaml but it is saying build action property is not set to resource.
for that i setted build action property to resource. Now this time that it is showing error message initialized componenet doesn't exist in the current context.
Can you tell me what properties we need to set when we create separate folders in wpf project and that folders contains windows or pages. and How to access those pages in other pages or in App.Xaml file startupUri Property.
When you have folders in your project structure, you should use a "/" not a ".", so it's foldername/window.xaml.
(I hope it's not actually called window.xaml by the way. That's a confusing name for a type in a WPF project, because there's a built in type called Window.)
Setting the build action to Resource will make matters worse: not only were you using the wrong name, you've now changed the build action to the wrong one for XAML. The correct build action for a .xaml file is usually Page. (App.xaml is an exception to that rule.) The Page build action causes the page to be compiled into a binary representation (known as BAML), and that binary format can then be loaded either by the call to InitializeComponent in the codebehind, or through Application.LoadComponent.
Setting the build action to Resource will just embed a copy of the XAML source directly in the project, which won't help you - you can't work with XAML in that form if you want to have a codebehind file. (Not in WPF, anyway. It's different in other XAML-based frameworks such as WinRT.)
Since Page is the default build action for a newly-added window, you don't actually need to set any properties at all. You just need to use / for folder boundaries.
If the XAML is inside any Folder the startup url will be defined as below.
This is how it will defined.

How can I instantiate the window/view that implements Caliburn Micro's IShell interface inside an Outlook Task Pane

I have a desktop application that is based on the Caliburn Micro framework. Everything works great. Now I am trying to port the same app into Outlook as a plugin.
In the desktop app, based on an entry inside app.xaml, Caliburn knows where to find the bootstrapper and instantiates it.
In case of the Outlook plugin, I've created an overridden bootstrapper that I instantiate explicitly inside ThisAddIn.ThisAddIn_Startup(). This one of course does not use the Application object.
I can even invoke a particular view using code similar to this
var windowManager = IoC.Get<IWindowManager>();
windowManager.ShowDialog(new MyViewModel());
And that will cause the view associated with the view model to be shown in a modal window on top of Outlook (hence validating that Caliburn Micro is able to find a view from a view model inside my Outlook plugin)
What I haven't figured out how to do is instantiate the Shell so that I can start using its functionality.
My expectation was that since my bootstrapper derives from BootStrapper, and I have registered my shell view model implementation with the MEF container as exporting IShell, Caliburn will automatically instantiate the shell view model and start using it. That is not happening.
My goal is to get the shell loaded inside my plugin's task pane as the container for other views that I will be loading based on user actions.
Any ideas or tips on how I can get this to work? In general has anyone got a shell implementation loading inside an Outlook or Office plugin's task pane?
Thanks!
Do you mean instantiate via Bootstrapper<Shell>. This uses the Window Manager underneath but I don't think that extends to outlook. There is nothing stopping you using the same code above to initialize your shell manually, composition will handle the rest of the application.
IoC.Get, by default calls Activator.CreateInstance so it is possible your problem is with MEF. The method that drives opening the Shell DisplayRootViewFor() calls this line.
windowManager.ShowWindow(IoC.GetInstance(viewModelType, null), null, settings);
If MEF is not hooked up properly it will fail causing your shell not to load.

WPF client for class library : how to notify error to UI?

I'm new to WPF and I'm working on a project that will have the following components : a WCF server class library, a WCF client class library, a WPF client UI and a WPF server UI.
I have a method in WCF client that add a user to a collection in the client and then register this user in the server. The method checks that user doesn't already exist locally then register in on the server that throws a FaultException if the user already exist on it.
How can I notify the client that operation doesn't succeed and that he must choose an other name? Throwing an exception? Adding code in ViewModel to check if user exists before calling add method?
Thank you.
IMHO, the best option is to check from the ViewModel on the server if the user already exists, if so, show a message on the UI.
Exceptions must be used for unexpected situations, and this is not the case.
A couple of ways. Take a moment to learn ValidationRules. This should help with that.
How to disable a Button on TextBox ValidationErrors in WPF
Howevever, you could do the same thing without a ValidationRule. Just have some error text that has Visibility bound to a bool in the ViewModel. Of course the error text would only be Visibile if user already exists.
Once you have looked at those two options, you should know which on is best for your project.

Resources