2016-04-15 25 views
2

我從另一個線程那裏得到了一些幫助,這點我沒有完全理解。c#在RelayCommand中丟失了TapHandler

這是一個UserControl,它區分幾種類型的文本,並使它們看起來像一個單獨的文本框。某些類型(即超鏈接)是可點擊的。

點擊它們我已經得到了這段代碼。

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); 
    } 
} 

現在的問題是,我想在點擊鏈接後停止點擊/點擊事件。通常爲了防止事件冒起來,我會這樣做e.Handled = True。但在這種情況下,我沒有鏈接上的TappEvent。

用戶控制背後的代碼 - 是這樣的,瀏覽器打開

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)); 
     } [...] 

之前,我必須停止此方法從UI元素圍繞這個用戶控件稱爲TapEvent。

我希望這是足夠的信息。否則讓我知道。

乾杯,

Ulpin

回答

0

有一個bool canTapBrowser = FALSE; 更改此

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

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

在webbrower導航完成事件執行以下操作:

canTapBrowser = true; 
OnHyperlinkCommand.RefreshCommand(); 
+0

THX的答案,但我沒有得到這一點。爲什麼這個布爾防止進入下一頁?問題是UserControl的完整表面有一個TappEvent,它會在將它點擊到我的應用程序的下一頁之後引導它。現在,通過該用戶控件中的新鏈接,如果點擊新鏈接,我不希望執行此TappEvent。由於我無法將此TappEvent設置爲「e.Handled = true」,因此應用程序會在webbrower導航完成之前運行到下一頁。 讓我知道如果你需要更多的代碼或更多的信息.​​.. – Ulpin

+0

關於用例/場景的一點細節解釋會有所幫助。我對你想要命令做什麼感到困惑。是否爲webview導航或點擊事件處理。 –

+0

好的。那麼到目前爲止我做了什麼:這是一個Twitter應用程序。如果您點擊/輕按推文周圍的空白處,應用程序會轉到顯示此推文的新頁面。這就是我用UserControl和TappEvent的意思。接下來我做的是實現一個新的UserControl,它代表了包含twitter內容的文本框。這是有問題的,因爲沒有超鏈接的默認文本框等。在上面的代碼示例中,我只發佈了帶有超鏈接的部分,但您可以想象有必須鏈接的Hashtags和@ -TwitterHandles。 – Ulpin