gpt4 book ai didi

.net - MVVM 中的等待光标

转载 作者:行者123 更新时间:2023-12-03 10:22:31 26 4
gpt4 key购买 nike

长时间操作需要等待光标。
Mouse.OverrideCursor 在 ViewModel 中工作,但不是真正的 MVVM 模式。
我有一个 IsBusy 属性 - 如何在 View 中获取等待光标?

这类作品
但我只让按钮上的等待光标
我想要整个窗口上的等待光标
我在转换器中破解了 Mouse.OverrideCursor = Cursor.Wait 但感觉很hacky
有一个更好的方法吗?

<Window x:Class="WaitCursor.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WaitCursor"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<BooleanToVisibilityConverter x:Key="BVC"/>
<local:ViewModel x:Key="ViewModel"/>
<local:BusyMouseConverter x:Key="BusyToCursorConverter"/>
</Window.Resources>
<Window.Cursor>
<Binding Path="IsBusy" Converter="{StaticResource BusyToCursorConverter}"/>
</Window.Cursor>
<Grid DataContext="{StaticResource ViewModel}">
<Button Content="Search" VerticalAlignment="Top" Command="{Binding SearchCommand}" Margin="2,0,2,2"
Cursor="{Binding Path=IsBusy, Converter={StaticResource BusyToCursorConverter}}"/>
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Markup;

namespace WaitCursor
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
[ValueConversion(typeof(bool), typeof(Cursors))]
public class BusyMouseConverter : MarkupExtension, IValueConverter
{
public BusyMouseConverter()
{
}

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value is bool)
{
if ((bool)value)
{
Mouse.OverrideCursor = Cursors.Wait; // this works but it feels hacky
return Cursors.Wait;
}
else
{
Mouse.OverrideCursor = null;
return null;
}
}

return null;
}

public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value is Cursors)
{
if (value == Cursors.Wait)
return true;
else
return false;
}

return null;
}

public override object ProvideValue(IServiceProvider serviceProvider)
{
return instance;
}

private static BusyMouseConverter instance = new BusyMouseConverter();

}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using System.ComponentModel;
using System.Diagnostics;


namespace WaitCursor
{
public class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}

class ViewModel : ViewModelBase
{
private ICommand _SearchCommand;
public ICommand SearchCommand
{
get
{
if (_SearchCommand == null)
{
_SearchCommand = new RelayCommand(param => this.Search(), null);
}
return _SearchCommand;
}
}
private bool isBusy = false;
public bool IsBusy
{
get { return isBusy; }
set
{
if (isBusy == value)
return;
isBusy = value;
NotifyPropertyChanged("IsBusy");
NotifyPropertyChanged("Cursor");
}
}
private async void Search()
{
IsBusy = true;
//UIServices.SetBusyState();
//Mouse.OverrideCursor = Cursors.Wait;
await Task.Delay(5000);
Debug.WriteLine("Search");
//Mouse.OverrideCursor = null;
IsBusy = false;
}
}
public class RelayCommand : ICommand
{
public RelayCommand(Action<object> execute) : this(execute, null)
{
}
public RelayCommand(Action<object> execute, Predicate<object> canExecute)
{
if (execute == null)
throw new ArgumentNullException("execute");
_execute = execute;
_canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
return _canExecute == null ? true : _canExecute(parameter);
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public void Execute(object parameter)
{
_execute(parameter);
}
private readonly Action<object> _execute;
private readonly Predicate<object> _canExecute;
}
}

最佳答案

正确的 MVVM 方法是使用 IValueConverter .您的 ViewModel 可以通过 IsBusy 继续提供指示程序是否繁忙的逻辑。 .你的 ViewModel 永远不应该决定显示什么——这是 View 的范围。

然后在您的 View 中,绑定(bind) Cursor将 UserControl 或 Window 的属性更改为 IsBusy ,使用转换器。

XAML:

<UserControl ...... Cursor="{Binding IsBusy, Converter={StaticResource MyBusyToCursorConverter}}" ....>

转换器:
public class BusyToCursorConverter : System.Windows.Data.IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == null || !(value is bool))
return System.Windows.Input.Cursors.Arrow;

var isBusy = (bool)value;

if (isBusy)
return System.Windows.Input.Cursors.Wait;
else
return System.Windows.Input.Cursors.Arrow;
}

public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}

更新

您的绑定(bind)可能没有触发,这就是您的转换器没有被调用的原因。这可能是因为 DataContext没有正确踢。
<Window.Cursor>
<Binding Path="DataContext.IsBusy" RelativeSource="{RelativeSource Self}" Converter="{StaticResource BusyToCursorConverter}"/>
</Window.Cursor>

关于.net - MVVM 中的等待光标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40010156/

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