RTF with Links in a RichTextBox WPF - wpf

I am able to load an rtf document in a RichTextBox, but the links that the document contains to some websites are not working.
Anyone have any idea why? Some solution to make the links work?
Best regards,
Paulo Azevedo

WPF by default doesn't understand where you want the links to be displayed, so what's happening is that the Hyperlink class is firing an event, RequestNavigate, and expecting you, the application designer, to cause the actual navigation to occur.
I assume you just want to launch the system configured web browser, so here's all you need to do:
Hook the Hyperlink::RequestNavigate routed event
Call Process.Start with the URL you receive to have the OS launch the browser.
That might look a little something like this:
public class MyWindow : Window
{
public MyWindow()
{
this.InitializeComponent();
this.myRichTextBox.AddHandler(Hyperlink.RequestNavigate, MyWidow.HandleRequestNavigate);
}
private static void HandleRequestNavigate(object sender, RequestNavigateEventArgs args)
{
Process.Start(args.Uri.ToString());
}
}

Related

JavaFX 8 : Combobox with full screen popup

I currently develop a javafx application designed for windows 8 tablet. I use the JMetro theme (a little customized) for the whole application.
I would like the combobox to behave like on Android, opening a full screen popup list.
I think the better and simpler solution would be to create a new skin and to plug it with -fx-skin css ? After that I "only" have to create my popup, react to click event, and setValue of the combobox. I looked at the source code of ComboBoxListViewSkin but I don't see what to change if I extend it...
I there a build-in solution to display a fullscreen combobox popup ?
Any help appreciated. Thank for reading.
The solution was quite simple, but not evident for a beginner.
public class TouchComboBoxListViewSkin<T> extends ComboBoxListViewSkin<T> {
private ComboBox<T> comboBox;
public TouchComboBoxListViewSkin(ComboBox<T> comboBox) {
super(comboBox);
this.comboBox = comboBox;
}
#Override
public void show() {
//TODO use the accessible protected popup to display the list of items
}
}
and a little of css to use this skin :
.combo-box {
-fx-skin: "path.to.TouchComboBoxListViewSkin";
}
Feel free to comment is this is not the right method.

Webkit.Net DocumentText not updating

I'm currently using a WebkitBrowser for .NET component in a Win form application in order to render HTML.
The application generate html code in a richtextbox and I want it to be display in the the webkit browser in real time.
Here is my code:
public void Write_HTML(string html)
{
HTMLTag.Text = html;
}
This is a method call from another class in order to update the html content in the richtextbox
private void HTMLTag_TextChanged(object sender, EventArgs e)
{
HTMLTag.Refresh();
webKitBrowser.DocumentText = HTMLTag.Text;
}
Each time the text of the rich textbox is change I tried to set the Webkit browser html content. after this affectation the DocumentText is still empty and I cannot understand why.
The funny part is that is I go on the interface and change the contetn of the textbox then the webpart refresh accordingly but is the event is trigger by the mthod Write_HTML it doesn't work, and no idea why.
Any ideas?
To get actual document text, you can use following:
webKitBrowser.StringByEvaluatingJavaScriptFromString("document.documentElement.innerHTML");

How to display a splash screen using Caliburn Micro

I'm using Caliburn Micro v1.3 with WPF. I would like to display a splash screen while my app loads.
I have overriden OnStartup as below but can't see how to close my splash when the base.OnStartup complete
protected override void OnStartup(object sender, System.Windows.StartupEventArgs e)
{
var wm = new WindowManager();
var vm = new StatusReporterViewModel("TEST", "information", null);
try
{
wm.ShowWindow(vm);
base.OnStartup(sender, e);
}
finally
{
vm.TryClose();
}
}
Any ideas?
Cheers
Steve
If you want to use the default WPF splash mechanism then it requires no code.
Add an image file to your WPF .EXE project and then set the properties on the image to "SplashScreen"
If your Splash Screen is an actual customized Window, you can close the SplashScreen in the OnInitialize() Method of your ShellViewModel (or if you dont have a shell, the first view model that gets activated). To get a reference to the SplashScreen in your Shell either inject it or make it a singleton

Windows Phone 7/Silverlight: How to do navigation?

I'm developing a Silverlight wp7 app. I'm not sure exactly how to do navigation.
I have several PhoneApplicationPage classes, which contain several UserControls. It looks like I can use NavigationService to navigate from the PhoneApplicationPage classes, but not the UserControl classes. Is that preferable? Is the general pattern not to navigate directly from a UserControl, but to handle it from a PhoneApplicationPage?
Currently, I have a collection of content separated into sections. Each section has its own PivotItem in a PivotControl. The content for each section is in a ListBox. I wrapped the ListBox in a UserControl to provide a little more functionality/managing the content. However, it looks like I can't navigate directly from this class.
I could remove the wrapper and just put the functionality in the pivot page directly. But what if I want to repeat the content list elsewhere in my app?
Alternatively, I pass NavigationService to the UserControl when it's constructed by the PhoneApplicationPage.
In WPF, it would be simple: You'd call the static method on NavigationService to get your answer: NavigationService.GetNavigationService(this).
Unfortunately, this does not appear to be available in WP7.
Instead, I came up with this hack... It is ugly as sin... hopefully there is something better. Possibly, at least, you can come up with something a bit more pretty. At least do some null checking...
var service = ((Application.Current as App).RootFrame.Content as Page).NavigationService;
In WP7, the RootVisual is always a PhoneApplicationFrame, and since NavigationService and Frame (or PhoneApplicationFrame) share almost all their methods/properties (intentionally), you can do this:
(Application.Current.RootVisual as PhoneApplicationFrame).Navigate(...whatever...);
I made a custom UserControl for this:
public class UserControlWithNavigation :UserControl
{
public event EventHandler NavigateToPageEvent;
public void NavigateToPage(Uri uri)
{
var e = new NavigationEventArgs(null, uri);
if (NavigateToPageEvent != null)
NavigateToPageEvent(this, e);
}
}
XAML use of the custom UserControl class:
<common:UserControlWithNavigation
xmlns:common="clr-namespace:NameSpace;assembly=AssemblyName"
and in my Page
MyUserControl.NavigateToPageEvent += (s, e) =>
{
NavigationService.Navigate(((NavigationEventArgs)e).Uri);
};
As you see, you have to give your UserControl a name (MyUserControl in above example)
I did a sample of navigating using MVVM Light at http://www.geoffhudik.com/tech/2010/10/10/another-wp7-navigation-approach-with-mvvm.html. It could use some refactoring and some prefer to put some of the navigation helper functions in another class other than a base page. That's easy enough to do though and it might give some ideas.

Cannot show up WPF application when setting MainWindow manually and composing application (MEF)

I got my hands om MEF for a week now and I am trying to build up a WPF application that loads imported controls from MEF.
I created a WPF application project and removed the default window and application start up URI. Then I handled the application startup event to compose the application:
public partial class App : Application, IPartImportsSatisfiedNotification
{
{...}
private void App_Startup(object sender, StartupEventArgs e)
{
this.Compose();
}
public void Compose()
{
try
{
globalCatalog.Catalogs.Add(new DirectoryCatalog(extensionsDirectoryPath));
CompositionContainer container = new CompositionContainer(globalCatalog);
container.ComposeParts(this);
}
catch (Exception ex)
{
// Do something
}
}
{...}
}
Actually, when debugging and watching objects after imports are satisfied, everything has hierarchically composed fine like I wanted. But when I try to show up the MainWindow of the application an exception is thrown on MainWindow.Show() call:
"Specified element is already the logical child of another element. Disconnect it first."
Though my code in OnImportsSatisfied method seems fine as it is working when not using MEF mecanism:
public void OnImportsSatisfied()
{
Window mainWindow = new Window();
mainWindow.Content = this.importedControl;
this.MainWindow = mainWindow;
this.MainWindow.Show();
}
I insist on the fact that this works perfectly when not importing controls with MEF. What is surprising is that this code does not work too:
Window mainWindow = new Window();
//mainWindow.Content = this.importedControl;
this.MainWindow = mainWindow;
this.MainWindow.Show();
So I suspect that ComposeParts is doing a bit more than what it says as it is the only member acting on my actual application instance.
Hope someone can help me (Glenn?).
Thanks.
Edit:
I discovered that when I remove the IPartImportsSatisfiedNotification interface from my parts, no exception is thrown and the window shows up. But of course the window is empty as I need this OnImportsSatisfied method to set the DataContext of the window to its associated imported view model.
The sample applications of the WPF Application Framework (WAF) show how to use MEF within a WPF application.
I finally discovered that I was importing my WPF user controls by using the default ImportAttribute constructor, which in fact will make a shared instance of the class if the creation policy is not specified during export. And as many of my controls were implementing the same interface and I was binding them in my views, I was actually trying to add this shared user control instance to different visual elements, which is not permited by WPF (and so the exception).
I marked my imports using the RequiredCreationPolicy set to NonShared and everything got back in order! That was all about learning MEF...

Resources