【问题标题】:Navigate to Views in order on one tab using prism使用 prism 在一个选项卡上按顺序导航到视图
【发布时间】:2017-04-28 06:29:10
【问题描述】:

我承认,我一直在学习棱镜,他们的一些概念对我来说并不清楚。我遇到了一些问题,我不知道如何解决它。 我必须构建我在几个视图中预测的配置的创建者:配置文件,完成后,使用信用卡查看,然后查看地址,然后添加一些配置。 其中一些是必需的,例如个人资料和地址。如果我们没有个人资料,我们不能去地址。其中一些不像信用卡那样重要。

我的公司希望使用一个通用的 TabControl 和一个通用的 tabItem 来实现。在这个 tabItem 内部,我必须显示每个视图。 点击“下一步”或“错过”按钮后,我必须更改视图。我也有上一步。 我的问题是如何以某种顺序存储多个视图。

到目前为止: 我有课

GeneralBaseViewModel : BindableBase, INavigationAware

它是抽象类,每个类都通用。

我有只有

的 GeneralHost.xaml
mvvm:RegionManager.RegionName="MainTabControlRegion"

作为telerik:RadTabControl(所以它是正常的TabControl)。它是每个视图都有 tabControl 通用的类。

在应该以第一个视图打开这些选项卡的菜单中,我在添加的地方实现了 NavigateOn

if (viewType == typeof(IConfiguration))
                {
                    this.RegionManager.RequestNavigate(RegionNames.MainRegion, typeof(GeneralHost).FullName);
                    this.RegionManager.RequestNavigate(RegionNames.MainTabControlRegion, viewType.FullName);
                    return;
                }

在 MainTabControlRegion 内部我想添加 View -> 所以我创建了 MyProfileViewModel(它实现了 IConfiguration)和 MyProfile,它们看起来像:

<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
              xmlns:lang="clr-namespace:LanguageResources.Properties;assembly=LanguageResources"
              xmlns:mvvm="http://prismlibrary.com/"
              xmlns:mainRegionItems="clr-namespace:App.MainRegionItems"
            xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
            xmlns:converters="clr-namespace:Shared.Converters;assembly=Shared"
            xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
            xmlns:enumerators="clr-namespace:AppManager.Models.ComplexTypes.Enumerators;assembly=AppManager.Models"
            Style="{DynamicResource ControlDefaultStyle}"
      d:DataContext="{d:DesignInstance mainRegionItems:MyProfileViewModel, IsDesignTimeCreatable=True}"
      x:Class="App.MainRegionItems.ConfigurationOfProfile" mc:Ignorable="d" mvvm:ViewModelLocator.AutoWireViewModel="True" d:DesignHeight="800" d:DesignWidth="800" >
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Loaded">
            <i:InvokeCommandAction Command="{Binding LoadDataCommand}"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
    <telerik:RadBusyIndicator BusyContent="{x:Static lang:Resources.PleadeWait}" IsBusy="{Binding IsBusy}">
        <Grid Background="White" >
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"></ColumnDefinition>
                <ColumnDefinition Width="*"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"></RowDefinition>
                <RowDefinition Height="*"></RowDefinition>
            </Grid.RowDefinitions>

            <GroupBox Grid.Column="0" Grid.Row="0" Header="{x:Static lang:Resources.AddressSender}"
                        HorizontalAlignment="Left" VerticalAlignment="Top" Height="307" Width="400">
                <Grid>
                    <Grid.Resources>
                        <Style TargetType="{x:Type TextBox}">
                            <Setter Property="Margin" Value="5" />
                        </Style>
                    </Grid.Resources>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"></ColumnDefinition>
                        <ColumnDefinition Width="*"></ColumnDefinition>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition></RowDefinition>
                        <RowDefinition></RowDefinition>
                        <RowDefinition></RowDefinition>
                        <RowDefinition></RowDefinition>
                        <RowDefinition></RowDefinition>
                        <RowDefinition></RowDefinition>
                        <RowDefinition></RowDefinition>
                        <RowDefinition></RowDefinition>
                    </Grid.RowDefinitions>
                    <Label Grid.Row="0" Grid.Column="0" Content="{x:Static lang:Resources.Name}" />
                    <TextBox Grid.Row="0" Grid.Column="1" Text="{Binding ModifiedInstance.DefaultSendingAddress.CompanyName,ValidatesOnDataErrors=True, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"  />
                    <Label Grid.Row="1" Grid.Column="0" 
                                   Content="{x:Static lang:Resources.AdditionalName}" />
                    <TextBox Grid.Row="1" Grid.Column="1"
                                     Text="{Binding ModifiedInstance.DefaultSendingAddress.AdditionalName,ValidatesOnDataErrors=True,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                </Grid>
            </GroupBox>
            <Menu Grid.Column="1"  Grid.Row="1" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="10" Height="23" >
                <MenuItem Style="{DynamicResource MenuItemStyle}" Command="{Binding SaveDataCommand}"
                              Header="{x:Static lang:Resources.SaveData}">
                    <MenuItem.Icon>
                        <Image Source="/App;component/Icons/Zapisz.ico" Height="25" />
                    </MenuItem.Icon>
                </MenuItem>
                <MenuItem Style="{DynamicResource MenuItemStyle}" Command="{Binding NextStepCommand}"
                              Header="{x:Static lang:Resources.NextStep}">
                    <MenuItem.Icon>
                        <Image Source="/App;component/Icons/Zapisz.ico" Height="25" />
                    </MenuItem.Icon>
                </MenuItem>
                <MenuItem Style="{DynamicResource MenuItemStyle}" Command="{Binding PreviousStepCommand}"
                              Header="{x:Static lang:Resources.PreviousStep}">
                    <MenuItem.Icon>
                        <Image Source="/App;component/Icons/Zapisz.ico" Height="25" />
                    </MenuItem.Icon>
                </MenuItem>
            </Menu>
        </Grid>
    </telerik:RadBusyIndicator>
</UserControl>

我的虚拟机:

public class ConfigurationOfProfileViewModel : GeneralBaseViewModel, IConfiguration
    {
        /// <summary>
        /// The report title.
        /// </summary>
        protected override string GeneralDetailTitle => "Configuration";

        public override void OnNavigatedTo(NavigationContext navigationContext)
        {
            this.GetConfigurationData();
        }

        /// <summary>
        /// The get report data.
        /// </summary>
        public void GetConfigurationData()
        {
            this.LoadingOn();
            this.SetHeaderTitle();
            this.LoadData();
            this.LoadingOff();
        }

        public int indexOrder = 0;

        private ICommand saveDataCommand;
        public ICommand SaveDataCommand => saveDataCommand ?? (saveDataCommand = new DelegateCommand(SaveData));

        private ICommand loadDataCommand;
        public ICommand LoadDataCommand => loadDataCommand ?? (loadDataCommand = new DelegateCommand(LoadData));

        private ICommand nextStepCommand;
        public ICommand NextStepCommand => nextStepCommand ?? (nextStepCommand = new DelegateCommand(NextStep));

        private ICommand previousStepCommand;
        public ICommand PreviousStepCommand => previousStepCommand ?? (previousStepCommand = new DelegateCommand(LoadData)); 

        private Profile modifiedInstance;
        public Profile ModifiedInstance
        {
            get
            {
                return this.modifiedInstance;
            }
            private set
            {
                this.SetProperty(ref this.modifiedInstance, value);
                this.OnPropertyChanged(() => this.ModifiedInstance);
            }
        }

        private void LoadData()
        {
            var repProfile = new ProfileRepository();
            Task<Profile> taskProfile = repProfile.GetPodmiot();
            this.ModifiedInstance = taskProfile.Result;

            if (this.ModifiedInstance == null)
            {
                this.ModifiedInstance = new Profile();
            }
            if (this.ModifiedInstance.ProfileData == null)
            {
                this.ModifiedInstance.ProfileData = new Address();
            }
            this.OnPropertyChanged(() => this.ModifiedInstance);
        }

        public async void SaveData()
        {
            this.LoadingOn();

            try
            {
                var repProfile = new ProfileRepository();
                var taskProfiles = repProfile.GetAll();
                var profiles = taskProfiles.Result;
                if (profiles.Any())
                {
                    await repProfile.Modify(this.ModifiedInstance);
                }
                else
                {
                    await repProfile.Add(this.ModifiedInstance, this.currentlyLoggedUser);
                }
                UserMessageBox.ShowError(Resources.Save, Resources.SaveSucceed, MessageBoxType.Information);
                this.LoadingOff();

            }
            catch (Exception e)
            {
                throw e;
            }

        }

        public async void NextStep()
        {
            try
            {
                this.SaveData();

            }
            catch (Exception ex)
            {
                UserMessageBox.ShowError(Resources.SaveFailed, ex.Message);
            }
        }
    }

我还创建了其他视图,例如 CreditCardConfiguration 和 VM(实现 IConfiguration)、SenderAddress、ReceiverAddress 等。每个 VM 都有属性 indexOrder (int)。

我的问题是如何准备 NextStepCommand 应该更改 MainTabControlRegion 的内容。视图必须按特定顺序提供。 如果我完成了 indexOrder=0 的配置文件,我可以使用 indexOrder=1 转到下一个视图。现在执行 NextStepCommand 后,我只保存了配置文件。 如何重新加载 RegionNames.MainTabControlRegion 的内容?以及如何实现这些视图的排序?

感谢您提供的所有帮助。

【问题讨论】:

    标签: c# wpf prism


    【解决方案1】:

    只需在 NextStep 方法中使用 IRegionManager.RequestNavigate 导航到下一个视图。如果您这样做,您将自动获得以前的功能。您应该在文档http://prismlibrary.readthedocs.io/en/latest/WPF/08-Navigation/#view-based-navigation

    中阅读 Prism 的导航功能

    【讨论】:

      猜你喜欢
      • 2015-11-24
      • 1970-01-01
      • 2017-01-15
      • 1970-01-01
      • 2012-08-29
      • 1970-01-01
      • 2020-12-13
      • 2023-04-07
      • 1970-01-01
      相关资源
      最近更新 更多