2

I've got a little help from another Thread here, which I not fully understood.

It's a UserControl which differentiates between several types of texts and makes them look like as one single textbox. Some of the types (i.e. Hyperlinks) are clickable.

To click on them I've got this code.

    public class RelayCommand<T> : ICommand
{
    readonly Action<T> _execute;
    readonly Func<T, bool> _canExecute;

    public event EventHandler CanExecuteChanged;

    public RelayCommand(Action<T> execute, Func<T, bool> canExecute = null)
    {
        _execute = execute;
        _canExecute = canExecute;
    }

    public void RefreshCommand()
    {
        var cec = CanExecuteChanged;
        if (cec != null)
            cec(this, EventArgs.Empty);
    }

    public bool CanExecute(object parameter)
    {
        if (_canExecute == null) return true;
        return _canExecute((T)parameter);
    }

    public void Execute(object parameter)
    {
        _execute((T)parameter);
    }
}

The problem is now, that I want to stop the Tap/Click event after clicking on the links. Usually to prevent the event from bubbling up I would do this e.Handled = True. But in this case I've got no TappEvent on the links.

The code- behind of the user control look like this:

 private void Init()
    {   
        //ComplexTextPresenterElement.Input = "This is where the Content string has to be....";
        ComplexTextPresenterElement.OnHyperlinkCommand = new RelayCommand<object>(Execute);
    }

    private async void Execute(object o)
    {
        List<object> passingParameters = new List<object>();

        //put here the code that can open browser 
        if (o is HyperLinkPart)
        {
            var obj = (HyperLinkPart)o;
            await Windows.System.Launcher.LaunchUriAsync(new Uri(obj.RealUrl, UriKind.Absolute));
        } [...]

Before the webbrowser opens I have to stop the TapEvent which gets called from the UI Element surrounding this UserControl.

I hope this is enough information. Let me know otherwise.

Cheers,

Ulpin

Ulpin
  • 179
  • 1
  • 14

1 Answers1

0

Have a bool canTapBrowser = false; Change this

ComplexTextPresenterElement.OnHyperlinkCommand = new RelayCommand<object>(Execute);

To

ComplexTextPresenterElement.OnHyperlinkCommand = new RelayCommand<object>(Execute,()=> {return canTapBrowser});

In the webbrower navigation completed event do the following:

canTapBrowser = true;
OnHyperlinkCommand.RefreshCommand();
Chirag Shah
  • 981
  • 1
  • 6
  • 12
  • Thx for the answer, But I'm not getting this. Why should this Bool prevent going to the next page? The problem is that the complete surface of the UserControl has a TappEvent which leads after tapping it to the next page of my App. Now, with the new links in that user control, I dont want this TappEvent to be executed if you click onto the new links. Because I'm not capable to set this TappEvent to `e.Handled = true`, the app runs to the next page far before the webbrower navigation is completed. Let me know if you need some more code or further information... – Ulpin Apr 18 '16 at 08:28
  • A little detailed explanation about the usecase/scenario would help. I'm confused about what exactly you want the command to do. Is it for the webview to navigate or tapped event handling. – Chirag Shah Apr 18 '16 at 08:43
  • Ok. So what have I've done so far: It's a Twitter application. If you click/tap the surrounding white space of a tweet the app goes to a new page displaying this tweet. This is what I've meant with the UserControl and TappEvent. The next thing I've done is implementing a new UserControl which represent the textbox with the twitter content. This was problematic because there is no default textbox with hyperlinks etc. In the code example above, I only posted the part with a hyperlink but as you can imagine there are Hashtags and @-TwitterHandles which has to be linked. – Ulpin Apr 18 '16 at 09:42
  • So now when you click on the links, hashtags, twitter handles inside the UserControl representing the tweet context a ClickEvent will also be generated for entire white space surrounding the tweet. So I want to prevent this event from beeing called if you click on links/hashtags/twitterhandles. As i said I usually would do this with a e.Handled = true but in this relayCommand code which I not fully understand there is no such thing. – Ulpin Apr 18 '16 at 09:46
  • http://stackoverflow.com/questions/27734084/create-hyperlink-in-textblock-via-binding had a look at this? – Chirag Shah Apr 19 '16 at 12:53
  • Nope, not yet but it looks much more convenient then my solution. This will be a lot of work changing. Could you try to explain to me what my the problem is with this RelayCommand so I cant use this execute function like a EventHandler. – Ulpin Apr 19 '16 at 13:08