Retrieve baseUri w/o resorting to Application.Current - silverlight

My custom controls are not loading in VS.NET's designer because of a null reference exception. It's got everything to do with the way I am retrieving the baseUri of the application when it runs in the browser:
_uriPrefix = Application.Current.Host.Source.AbsoluteUri.Substring(0,
Application.Current.Host.Source.AbsoluteUri.IndexOf("/ClientBin")).Trim();
According to the exception details and the help file I'm directed to (here) I'm not correctly designing my app for a runtime that's not the browser (i.e. the new WPF editor in VS.NET or Expression Blend).
So, the question is how do retrieve the baseUri (the http://localhost:#### part of my application) if I can't use Application.Host which apparently is null during design time? is there a safe way to do this so I can load my custom controls in a designer?

To eleborate on the problem and solution (i wasn´t finished writing it when you posted your own answer)
Your problem is the runtime context you are in.
When your page is run as on a ASPX.Net page, your are hosted by the IIS->ASP.Net pipeline. This application will give your the HTTPContext and your Application.Current is refering to the W3WP.EXE process in which the ASP.Net pipeline is serving your page.
When your page is displayed in the MS Visual Studio designer it does not provide this HTTPContext since there is none. The request to render your control did not come through an HTTP request, but the Visual Studio designer running inside Visual Studio.
To have your control be displayed correctly during design time, you´ll have to add ´design time support classes´. There are a number in the .Net framework you can use directly, but for custom serialization (writing the server ASP.Net tags or rendering a HTML preview with styling) you´ll have to put in some more effort and write your own designer classes.
Hope this helps,

Duh! I hate/love when I do this. I'm answering my own question (again). I can use the DesignerProperties.IsInDesignTool boolean to test if I'm in design mode or not:
if (!DesignerProperties.IsInDesignTool)
_uriPrefix = Application.Current.Host.Source.AbsoluteUri.Substring(0,
Application.Current.Host.Source.AbsoluteUri.IndexOf("/ClientBin")).Trim();
Now I can view my custom control in the new VS 2010 editor. Yeah!

Related

CefSharp.Wpf: FileLoadException when trying to load assembly

We have an old legacy application that is running with MS Access. We are in the process of migrating it to WPF. For that we created a WinForms control, that is showing our new WPF stuff in an ElementHost. This WinForms control is then somehow used in a COM ocx which then is placed on a MS Access form. This works so far, and for the time being this is like it is and I can't change it.
Until now we used the built-in WebBrowser control from .NET to show web sites, but since this control is based on IE 11, we are looking for a replacement. WebView2 is not working in an ElementHost, so now we are investigating and testing CefSharp for WPF.
In all of my stand-alone tests it worked so far, but when I tried to integrate it into our software today I stumbled upon a problem and I'm not sure how to fix that.
When I put the ChromiumWebBrowser control from CefSharp onto our WPF window and ran our software I got an IO.FileLoadException for the file CefSharp.Wpf. Checking that with fuslogvw.exe I've seen, that .NET looked for that file in the directory where the MSAccess.exe is situated, probably because this in fact is the executing assembly.
So I registered an AppDomain.CurrentDomain.AssemblyResolve event to manually load the necessary files from where they are situated. This worked for
CefSharp.Wpf
CefSharp
CefSharp.Core
But then it tried to load CefSharp.Core.Runtime and although this assembly is present I get an IO.FileLoadException (this file or one of its dependencies can't be found) when loading this assembly. I assume that it tries to find one of its own dependencies (non .NET) and looks for it in the wrong place (maybe the MS Access directory again?).
Do you have any suggestions how I can tell CefSharp and all of its dependencies (direct or indirect) where they can be found and where they should be loaded from?

Best Starting Point for WPF Revit Add in

I am wanting to rewrite one of my Revit Add ins so that it utilizes WPF with MVVM because I like the look and functionality of WPF better than Windows Forms.
I have used the Revit Template Wizzard from Jeremy Tammik for the Form based add in, but adding a WPF user control seems not to work (a run time error that the xaml resource cannot be found).
I found a WPF MVVM revit add in example (AddMaterials, here is the github link, which will add materials from an Excel spreadsheet) but it does not follow what I am expecting to see at the top level.
Revit Add ins have an app.cs file which tells Revit how to register and access the DLL (ribbon panel buttons etc).
A windows WPF app will have app.xaml as the top level entry point.
The Add Materials project has neither which tells me that it must be
a class library, however the views are not using UserControls
rather they are Windows which I prefer. However Visual Studio does not
let you add a Window for a Class Library type project.
The third issue is easily solved by simply copying windows from a WPF application project into a class library project. But I don't really understand how the class library will instantiate in Revit without following the app.cs code from the template. Is anyone else creating add-ins this way, and if so can you let me in on any tricks or discussions that will help? Has anyone created a WPF Revit addin template for Visual Studio?
When I add a WPF window and try to instantiate it I get an error that it cannot find the xaml resource (System.IO.IOException: Cannot locate resource 'xxxx.xaml'). I have tried to fix this according to advice found when googling for this error, but to no avail. I am thinking it comes from being in a form based project, and that I may have to just start with a new project without the form stuff.
I have now verified that indeed you can start with the Revit AddIn Wizzard and use WPF . . . I started from scratch and copied in a window created in another project and got it to run (after adding the various references, namespaces, etc). So my problem seems to just be with the original project which already had a bunch of form stuff added.
Yes, I'm using WPF to create Revit Addins. It works well. You can easily create your own WPF template from the SDK samples:
Start with one of the Autodesk-provided SDK samples. I used the "DockableDialogs" sample. I know this one works, your mileage may vary with the others. If you're looking for windows rather than docked panes in the UI, another sample (perhaps the AddMaterials sample) is probably simpler.
I used Visual Studio to turn the sample into a template. File - Export Template -> select "DockableDialogs" or other WPF sample project.
Create a new project based on the template you just created. This was the easiest method I could find to get the WPF internal bits wired up correctly.
I'm not specifically familiar with the AddMaterials project, but to clarify your bullet points.
Revit Addins - It's not the file name (app.cs) but rather they must extend IExternalApplication or IExternalCommand. If you are creating a xaml interface (rather than just running a command from a ribbon button) you'll use 'IExternalApplication' as your entry point. Look for something like this in the sample:
public class ThisApplication : IExternalApplication ...
I don't used a top level app.xaml, but instead have page.xaml pages which are called by the Revit app. In my case these are Pages rather than Windows, which extend the IDockablePaneProvider class. These must be registered with the application which can then can be show, hide, etc your Panes. I imagine this is simpler with Windows, but haven't done it myself. For the dockable panes, your xaml.cs should start out something like:
public partial class MainPage : Page, Autodesk.Revit.UI.IDockablePaneProvider ...
Yes, the project is a class library in the sense that it is a collection of classes, at least one of which extends IExternalApplication or IExternalCommand. Remember that you're not creating a standalone application, but adding functionality to an existing Windows application (Revit). Revit will instantiate the ThisApplication class and then call its .OnStartup() method when the Revit application starts. This shouldn't stop you from adding .xaml or .cs files to the project, though. I can do it using VS Community 2015 using Ctrl-Shift-A.
Hopefully this gets you started - I've been able to implement a WPF UI in Revit without any prior WPF experience, and I'm not even a real programmer, so it's definitely possible. Good Luck!
addendum
If you want to add WPF elements to an existing revit addin, you can follow the instructions here: How can I connect xaml and xaml.cs files
Ultimately I found it easier to migrate my addin code into a template made from a working sample, you may want to try this approach as well.

SharePoint embed of Silverlight app breaking ControlTemplates

Strange problem here, I have a page with 4 ControlTemplates declared in the page resources. In the code for the page, I declare a reference to each of these ControlTemplates so that I can use them in binding. This works fine Out-Of-Browser, and In-Browser, except when it is running in the Silverlight Web Part in SharePoint. In SharePoint, the buttons that I want templated are shown as just normal buttons with the class as the text instead of the special layout and content I define in the template. Any idea why it would work everywhere but in SharePoint?
While I am not sure what caused the null reference only while being ran in SharePoint, I refactored my code to make the ItemsControl.ItemsSource reference a property instead of a single assignment of a List<> to it. Now it is working just like it should.
Helpful in figuring this out was the ability to attach Visual Studio to the Internet Explorer (didn't work so well for Chrome) process that was running the Silverlight Web Part. This is found in Visual Studio by going to the Tools menu > Attach to Process. Which IE process it is will be obvious as it will have Silverlight in the Type column.

Sending WPF User Controls over WCF or similar

I am building an application in the MVVM style where the actual views (UserControls) are stored remotely and hopefully sent via a WCF service to my main application window. I am doing this in order to avoid having the user reinstall the application each time a new view is added. However, when I try to return a User Control from my WCF service I get an error stating:
System.Runtime.Serialization.InvalidDataContractException: Type 'System.Windows.Input.TouchDevice' cannot be serialized.
I am wondering if there is any way around this or if people have implemented something similar in other ways?
Thanks,
Ochowie
When you're loading from an assembly you're instantiating the compiled object from an assembly, which is a lot different than a deserialization operation, which is what is happening with a service call. The best you can do with serialization is transfer raw XAML that can be loaded with XamlReader, but this limits what you can do with a control since you can't use any code. If you're really set on hosting controls on your server the most flexible option would be to have your app download an assembly containing the new control and dynamically load the new assembly or use a separate launcher that can download new control assemblies before starting up the main application (make sure you take the time to understand the security implications and secure the assemblies and downloads).
What you're trying to do doesn't really make sense... controls are not DTOs, they're strongly dependent on their runtime environment. In WPF, there is also the issue of the dispatcher : even if you could transfer a control, how would you reattach it to your application dispatcher ? Anyway WPF controls are not serializable with the DataContractSerializer, and there is no way to change that.
However, what you could do is transfer a representation of the views in XAML. This is of course assuming your views are XAML only, without any code-behind. You could then load the views using XamlReader and render them in your UI. I'm not sure of the feasibility, but I think it's your best option.
You could also implement your views in a separate assembly, so that the users don't need to reinstall the whole application. They will only need to upgrade the modified assemblies.

Alternatives to WebBrowser control in WPF?

We are working on a WPF app with the web browser control. When the user drags the window, the web browser lags behind the window as it drags.
Does anyone know how to solve this issue? Failing that, does anyone know an alternative to the web browser control itself?
Thanks!
Yes, as long as you don't require IE specific features you can use Awesomium which basically gives the same browser engine used by Google Chrome except for everything is rendered in pure WPF. For a while it was open source but I believe it is becoming a commercial product.
I haven't heard about any Native WPF WebBrowser Control. The only options I know are based on Win32 controls which are wrapped into WPF controls in order to displayed. (See more details at http://msdn.microsoft.com/en-us/library/ms752055.aspx )
Recently I have to use the WPF wrapper for the WebBrowser control, and it's very limited regarding the options available for the developer. During my research I found that you are able to inject scripts or html content in order to modify the page behavior. Also, you can call .NET code (e.g. CSharp function from Javascript).
However tasks such as accessing the cache, modifying cookies are difficult task or changing the web browser behavior are very difficult. Here is when you will have to use DLL Imports in order to invoke unmanaged routines (see http://msdn.microsoft.com/en-us/library/aa984739(v=vs.71).aspx )
It's possible to look for an alternative WebBrowser control such as:
http://code.google.com/p/csexwb2/
http://wpfchromium.codeplex.com/
I hope this helps
You can try CefSharp.Wpf which uses Chromium as a base browser. You can find it on NuGet

Resources