【发布时间】:2016-12-12 15:38:18
【问题描述】:
有一个导航命令需要在点击时显示一个工具提示,同时被禁用,以便用户知道它为什么被禁用。我遇到的问题是我不知道如何将 TouchDown 事件从我的 xaml 文件传递到我的视图模型。有什么方法可以绑定而不是在 command.xaml.cs 中创建事件?
命令的结构如下。我有一个 CommandButton.xaml 和 CommandButton.xaml.cs,而设置按钮的所有内容都由 VM(文本、图像、执行的命令等)代码处理,如下面的示例。
<Button Focusable="True" Name="Btn1" Command="{Binding CommandToExecute}" Tag="{Binding Text}" Foreground="{x:Null}" Style="{DynamicResource ButtonStyle}" ToolTipService.ShowOnDisabled="true" TouchDown="Btn1_OnTouchDown" >
<Button.ToolTip>
<StackPanel>
<TextBlock>Test</TextBlock>
<TextBlock>Load stencil, or not your choice.</TextBlock>
</StackPanel>
</Button.ToolTip>
<shellModule:AutoGreyableImage Source="{Binding Image}" />
</Button>
至于后面的代码,我将其与基本命令类中的大多数处理程序内容进行了拆分,如下所示。
public abstract class BaseCommand : BindableBase
{
protected IModuleManager ModuleManager { get; set; }
protected IRegionManager RegionManager { get; set; }
protected BaseCommand(IRegionManager regionManager, IModuleManager moduleManager, string pageName = null)
{
RegionManager = regionManager;
ModuleManager = moduleManager;
Text = GetButtonText(pageName + "_BtnTxt");
Image = (ImageSource)Application.Current.FindResource(pageName + "_BtnImg");
}
private string _text;
private ImageSource _image;
public ICommand CommandToExecute => new DelegateCommand<object>(Command, Evaluate);
protected abstract void Command(object obj);
protected virtual bool Evaluate(object obj)
{
return false;
}
public string Text
{
get { return _text; }
set { SetProperty(ref _text, value); }
}
public ImageSource Image
{
get { return _image; }
set { SetProperty(ref _image, value); }
}
protected string GetButtonText(string key)
{
string uiString;
var locExtension = new LocTextExtension
{
Key = "Resources",
ResourceIdentifierKey = key
};
locExtension.ResolveLocalizedValue(out uiString);
return uiString;
}
}
然后是视图模型中的命令特定内容。
public class Page1CommandViewModel : BaseCommand, IPage1CommandViewModel
{
public Page1CommandViewModel(IRegionManager regionManager, IModuleManager moduleManager) : base( regionManager, moduleManager, PageNames.Page1 )
{
}
protected override void Command(object obj)
{
Task.Run(() =>
{
ModuleManager.LoadModule(ModuleNames.Page1Module);
RegionManager.RequestNavigate(RegionNames.ContentRegion, new Uri(PageNames.Page1, UriKind.Relative));
});
}
}
如果有人能指出我正确的方向,将不胜感激。
【问题讨论】:
-
点击工具提示很奇怪。为什么不将按钮包装在显示工具提示的某个面板中?这样即使按钮本身被禁用,它也将是可见的。
-
应用程序将在触摸屏上运行,这就是为什么需要点击而不是悬停的原因。
-
那不是真的“点击”,是吗? Tooltip 在触摸 UI 中不是一个方便的解决方案。如果您确实需要以这种方式显示信息,为什么不完全禁用该按钮,并根据某些条件在用户单击它时显示一些消息?
-
取决于我们获得的触摸屏。我们过去(可能在未来)使用的电阻式不会将手指按压转化为触摸事件,而是转化为点击事件。或者至少看起来是这样。我认为它们只在电容屏上注册为触摸事件(我可能错了)