WPF实战学习笔记26-首页导航

修改UI,添加单击行为,并绑定导航命令

修改文件:Mytodo.Views.IndexView.xaml ,在导航梯形添加内容

<DataTemplate>
    <Border
            Margin="10"
            Background="{Binding Color}"
            CornerRadius="5"
            Opacity="0.9">
        <Border.InputBindings>
            <MouseBinding MouseAction="LeftClick"
                          Command="{Binding DataContext.NavigateCommand, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ItemsControl}}"
                          CommandParameter="{Binding}"/>
        </Border.InputBindings>

添加导航命令,并初始化

修改文件:indexviewmodel.cs

/// <summary>
/// 导航命令
/// </summary>
public DelegateCommand<TaskBar> NavigateCommand { get;private set; }
NavigateCommand = new DelegateCommand<TaskBar>(Navigate);

添加导航区域变量,并初始化

修改文件:indexviewmodel.cs

this.regionManager = provider.Resolve<IRegionManager>();
private readonly IRegionManager regionManager;

添加导航方法

private void Navigate(TaskBar obj)
{
    if (string.IsNullOrWhiteSpace(obj.Target)) return;

    NavigationParameters param = new NavigationParameters();

    if (obj.Title == "已完成")
    {
        param.Add("Value", 1);
    }
    regionManager.Regions[PrismManager.MainViewRegionName].RequestNavigate(obj.Target, param);
}

TaskBars添加对应的导航区域

void CreatBars()
{
    Title = "您好,2022";
    TaskBars = new ObservableCollection<TaskBar>();
    TaskBars.Add(new TaskBar { Icon = "CalendarBlankOutline", Title = "汇总", Color = "#FF00FF00", Content = "27", Target = "TodoView" });
    TaskBars.Add(new TaskBar { Icon = "CalendarMultipleCheck", Title = "已完成", Color = "#6B238E", Content = "24", Target = "TodoView" });
    TaskBars.Add(new TaskBar { Icon = "ChartLine", Title = "完成比例", Color = "#32CD99", Content = "100%", Target = "" });
    TaskBars.Add(new TaskBar { Icon = "CheckboxMarked", Title = "备忘录", Color = "#5959AB", Content = "13", Target = "MemoView" });
}

修改OnNavigate方法

当为“已完成”栏时,使得显示的待办都为已完成。

public override void OnNavigatedTo(NavigationContext navigationContext)
{
    base.OnNavigatedTo(navigationContext);
    if (navigationContext.Parameters.ContainsKey("Value"))
        SelectIndex = navigationContext.Parameters.GetValue<int>("Value");
    else
        SelectIndex = 0;
    GetDataAsync();
}