Get access to parent WPF window across AppDomain - wpf

I am developing an AddIn for SCVMM (System center for Hyper-V), It is a WPF application that allows users to develop Add-Ins and host their WPF UI (user controls) inside of SCVMM. It uses the Microsoft Add-ins and Extensibility framework.
I am trying to access the parent window instance, the main SCVMM window that hosts my add-in. I have searched on this topic and everybody says it is not possible to do this across App domains.
I am able to get the handle to the window but when i try to convert it into a window I get null.
HwndSource source = (HwndSource)HwndSource.FromVisual(this.btn);
IntPtr handle2 = source.Handle;
Window w = source.RootVisual as Window;

Related

Can a WPF Application be hosted on a thread separate from the UI thread?

I'm working on a project to integrate WPF Windows into a large application written in Visual FoxPro. The project has been progressing for a couple of years with success.
One downside to this project is the limitation of the UI thread. FoxPro is an STA app, so there is only one thread that both UI and application services must share. While the application is retrieving data, the UI is locked up. It is my hope that the method we're using to integrate WPF into this app gives us an opportunity to introduce a separate UI thread for the WPF Windows we're using.
How do we integrate WPF into a STA app?
We build a .NET COM server, not an ActiveX control. Instead we're using a simple COM control that exposes events. Our FoxPro client app subscribes to the events and handles UI requests through these events.
We start with a couple of .NET classes that the COM server will use.
// This class allows us to initialize an Application within a COM server
// since there is no App.xaml to do that for us.
[ComVisible(false)]
public class MyApp : System.Windows.Application
{ }
// This class allows us to assign the FoxPro app's main window as the owner
[ComVisible(false)]
public abstract class MyWindow : System.Windows.Window
{
private System.Windows.Interop.WindowInteropHelper _helper;
public void AssignOwner(int hWnd)
{
_helper = new WindowInteropHelper(this);
_helper.Owner = new IntPtr(hWnd);
}
}
The FoxPro app instantiates an instance of the COM server, subscribes to COM events, then calls a method to show the WPF window. The COM server's Show method first causes this bit of code to execute once per VFP app by delegating it to a static class.
if (Application.Current == null)
_ = new MyApp();
if (Application.Current != null)
{
Application.Current.ShutdownMode = ShutdownMode.OnExplicitShutdown;
Application.Current.DispatcherUnhandledException += Application_DispatcherUnhandledException;
}
The Show method then builds a Window and displays it. There's nothing special here.
// Build a business model to access data and services through COM events
var model = new MyModel(this);
// ShellView is an implementation of MyWindow
var window = new ShellView { DataContext = new ShellViewModel(model) };
if (hWnd > 0)
window.AssignOwner(hWnd); && hWnd is a handle to the FoxPro window
window.Show();
The Question
Does the static code that builds the WPF Application instance give us the opportunity to construct a separate UI thread for the Dispatcher used by the WPF components?

Calling commands across the app domain boundary WPF

I'm working on an MVVM and AddIns-based application. I want to build the menu (ribbon control) dynamically through the AddIn module which will contain MVVM based assembly. On the host side, I'm unable to fire the command which is bound through ViewModel, which is hosted in separate appdomain from host application. How should I approach this issue of dynamic ribbon tabs generation with all bindings to commands/icons from the respective AddIn module.
Edit:
I've developed an MVVM-based application. All application state and commands are in ViewModel and that is bound to the View, which works as expected.
Afterwards, we write code using Managed Addin Framework (MAF) for .net and incorporated that application through it. The issue is coming that we are unable to generate a menu based on the commands in the Addin at the host side. I wrote a property to return menus from the ViewModel Addin to the host, but it just returns the plain objects and commands do not get propagated through AddIn Framework. Here is a code sample:
public IEnumerable<TabViewModel> MenuTabs
{
get
{
var tab = new TabViewModel{ Header = "Tab 1"};
var group = new GroupViewModel {Header = "Group 1"};
var button = new ButtonViewModel{Content = "Say Hello", Command = HelloCommand};
group.Buttons.Add(button);
tab.Groups.Add(group);
return new[] {tab};
}
}
This code is written in say TestViewModel, and HelloCommand is an implementation of ICommand interface from WPF input library. The HelloCommand does not reach at the host side everything else is available and I'm getting Ribbon control's tabs and groups properly, but the commands are not being transmitted to the Addin Host.
Edit2
The issue is that, we want to show the Ribbon in the AddIn-Host window those buttons refer to the commands in AddIn's ViewModel, the final thing (i can) is to move the menu control to the AddIn too. Has someone encountered same problem and want to share the experience?

Panning web map not working in WPF WebBrowers. Works in Internet Explorer

We have an intranet web GIS application which I am trying to embed in a WPF application using a WebBrowser control. The application is a proprietary solution from a third party vendor over which I have no control - it works very similar to Google Maps. On my desktop I have Internet Explorer 11 installed, and in Internet Explorer the web GIS works fine. When embedded in a WebBrowser control on a WPF form (.NET 4.5, VS2012) the map loads and will zoom with a roll of the mouse wheel, but will not pan when I try to drag it.
The WPF view is as simple as can be:
<Grid>
<WebBrowser x:Name="TestWebBrowser" />
</Grid>
The code-behind is equally simple:
public MainWindow()
{
InitializeComponent();
this.TestWebBrowser.Navigate("http://myserver/map.aspx");
}
I've tried setting the registry key HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION\myapp.exe (and the .vshost.exe variant) to various values without success.
If I change the URL to http://maps.google.com, I can pan the Google map, so I know the WebBrowser isn't fundamentally incapable of allowing drag events in my current configuration, but I can't change the source code of the web application itself.
Any ideas?
If you're running a 32bit app you need to set the registry key in Wow6432Node node.
So your key will look like:
HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION\myapp.exe

How to cast from Windows.UI.Xaml.UIElement to System.Windows.Controls.UIElement

I'm trying to create a new UI for an app I developed for the Windows Store using the Windows Runtime API. This new client is going to be a WPF Desktop App. I'm trying to reuse as much code as I can but I'm facing several issues.
Fortunately there's a way to use WinRT in a WPF Desktop app (see this and this)
Now I'm blocked by the following issue:
The MediaCapture class has a method to start the preview of a device, after I've started the preview I can create a CaptureElement to display the preview.
CaptureElement is part of Windows.UI.Xaml.Controls and cannot be used in containers from System.Windows.Controls in which the WPF UI is built.
Am I going anywhere with this approach? what would you recommend? How can I display the preview of a device in the WPF app?
Thanks in advance
CaptureElement can't be used in desktop applications - the documentation says "[Windows Store apps only]". In general - the UI controls from WPF and WinRT/XAML can't be used outside of their respective domains (desktop/immersive UI). In a WPF app you need to use WPF controls - e.g. DirectShow for camera capture,

Windows Phone 7 - Using the Application bar and Prism (or MVVM)

I am playing around with the WP7 SDK and the Prism for WP7 beta and have come across a problem I can't figure out (or even a workaround for).
First of all, I'm new into WPF/Silverlight/WP7 and Prism, so I could be overlooking something very obvious.
So I have a Shell page that has my region that is used to hold my content pages, and all of this is working great! Now my problem is that I have a settings control that will allow the users to edit the settings of the application (names, locations, etc). Now I can get this page to work with no problems by having a button on one of my controls that will transition the region manager to the control.
However, I would like to use the application bar on the phone to have the button, but I cannot for the life of me figure out how to get access to my model object from within the page that is opened by the Application bar click. I can only do a NavigationService.Navigate() to a settings page, but the PhoneApplicationPage objects in WP7 do not allow injection on the constructors (the constructors must be parameterless) so I cannot pass in the object instance in that way.
So my question is, how can I access (or pass) objects between pages or controls?
Thanks!
In the examples they use this technique to set the data context of a form after it is navigated to from another form:
NavigationService.Navigate(new Uri("/Page2.xaml", UriKind.Relative));
FrameworkElement root = Application.Current.RootVisual as FrameworkElement;
root.DataContext = some_object;

Resources