0
点赞
收藏
分享

微信扫一扫

vue3 通过 axios + jsonp 实现根据公网 ip, 查询天气信息

潇湘落木life 2024-09-25 阅读 21
wpf

在WPF(Windows Presentation Foundation)应用程序中,ViewModel扮演着连接View(用户界面)和Model(数据模型)的重要角色。ViewModel的设计和实现是实现MVVM(Model-View-ViewModel)设计模式的关键部分。以下是一个详细的WPF入门教学,介绍ViewModel的设计与实现。

1. ViewModel的作用

ViewModel的主要作用包括:

  • 数据绑定:提供属性供View绑定,以便显示数据和接收用户输入。
  • 命令处理:实现ICommand接口,处理用户在View上的操作。
  • 业务逻辑:包含与业务相关的逻辑,保持View的简洁。
  • 状态管理:管理应用程序的状态,如启用/禁用某些控件。

2. 创建ViewModel基类

通常,我们会创建一个基类,所有具体的ViewModel都继承自这个基类。这个基类可以实现一些通用的功能,如INotifyPropertyChanged接口。

 
public abstract class BaseViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    protected bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
    {
        if (EqualityComparer<T>.Default.Equals(storage, value))
            return false;

        storage = value;
        OnPropertyChanged(propertyName);
        return true;
    }
}

3. 实现具体的ViewModel

假设我们有一个简单的应用程序,显示一个计数器并提供增加和减少按钮。

 
public class CounterViewModel : BaseViewModel
{
    private int _count;
    public int Count
    {
        get => _count;
        set => SetProperty(ref _count, value);
    }

    public ICommand IncrementCommand { get; }
    public ICommand DecrementCommand { get; }

    public CounterViewModel()
    {
        IncrementCommand = new RelayCommand(Increment);
        DecrementCommand = new RelayCommand(Decrement);
    }

    private void Increment()
    {
        Count++;
    }

    private void Decrement()
    {
        Count--;
    }
}

4. 在XAML中绑定ViewModel

在XAML中,你需要将ViewModel设置为窗体或控件的DataContext。

 
<Window x:Class="YourNamespace.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Counter Example" Height="200" Width="300">
    <Window.DataContext>
        <local:CounterViewModel />
    </Window.DataContext>
    <Grid>
        <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center">
            <TextBlock Text="{Binding Count, StringFormat='Count: {0}'}" FontSize="24" Margin="10"/>
            <Button Content="Increment" Command="{Binding IncrementCommand}" Margin="5"/>
            <Button Content="Decrement" Command="{Binding DecrementCommand}" Margin="5"/>
        </StackPanel>
    </Grid>
</Window>

5. 使用RelayCommand

在上例中,我们使用了RelayCommand来处理按钮点击事件。RelayCommand是一个实现了ICommand接口的类,通常用于ViewModel中。

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

    public RelayCommand(Action<object> execute, Func<object, bool> canExecute = null)
    {
        _execute = execute ?? throw new ArgumentNullException(nameof(execute));
        _canExecute = canExecute;
    }

    public bool CanExecute(object parameter)
    {
        return _canExecute == null || _canExecute(parameter);
    }

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

    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }
}

6. 处理复杂逻辑

如果你的ViewModel需要处理更复杂的逻辑,可以将其分解为多个小的服务或帮助类,并在ViewModel中使用这些服务。

 
public class CounterService
{
    public int Increment(int currentCount)
    {
        return currentCount + 1;
    }

    public int Decrement(int currentCount)
    {
        return currentCount - 1;
    }
}

public class CounterViewModel : BaseViewModel
{
    private readonly CounterService _counterService = new CounterService();
    private int _count;
    public int Count
    {
        get => _count;
        set => SetProperty(ref _count, value);
    }

    // ... 其他代码 ...
}

总结

通过上述步骤,你可以设计和实现一个基本的ViewModel,并在WPF应用程序中使用它。ViewModel的设计应该尽量保持简洁和专注,每个ViewModel最好只负责一个特定的功能或视图。这样不仅有利于代码的维护和测试,还能提高应用程序的可扩展性。

举报

相关推荐

vue3 - axios

vue3 axios

axios vue3

[vue3] Axios 使用

Vue3 Ajax(axios)

vue3 axios封装

0 条评论