gpt4 book ai didi

c# 在 RelayCommand 中缺少 TapHandler

转载 作者:太空宇宙 更新时间:2023-11-03 15:22:40 25 4
gpt4 key购买 nike

我从这里的另一个线程那里得到了一些帮助,我还没有完全理解。

这是一个 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);
}
}

现在的问题是,我想在单击链接后停止 Tap/Click 事件。通常为了防止事件冒泡,我会这样做 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));
} [...]

在打开网络浏览器之前,我必须停止从围绕此 UserControl 的 UI 元素调用的 TapEvent。

我希望这是足够的信息。否则请告诉我。

干杯,

乌尔平

最佳答案

有一个 bool canTapBrowser = false;改变这个

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

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

在 webbrower navigation completed 事件中执行以下操作:

canTapBrowser = true;
OnHyperlinkCommand.RefreshCommand();

关于c# 在 RelayCommand 中缺少 TapHandler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36646181/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com