使用Unity的通用应用程序棱镜

本文关键字:应用程序 棱镜 Unity 使用 | 更新日期: 2023-09-27 18:15:15

我正在与Prism和Unity一起学习全新的通用应用程序创建,但我有几个问题我不确定:

我有以下简单的数据对象:

public class Customer : IEditableObject, IEquatable<Customer>
{
    private Customer backup;
    public string Name { get; set; }
    public string Surname { get; set; }
    public DateTime DateOfBirth { get; set; }
    public void BeginEdit()
    {
        this.backup = this.MemberwiseClone() as Customer;
    }
    public void CancelEdit()
    {
        this.Name = this.backup.Name;
        this.Surname = this.backup.Surname;
        this.DateOfBirth = this.backup.DateOfBirth;
    }
    public void EndEdit()
    {
        this.backup = this.MemberwiseClone() as Customer;
    }
    public bool WasChangeMade()
    {
        if (this.Equals(backup))
            return false;
        else
            return true;
    }
    public bool Equals(Customer other)
    {
        return this.Name == other.Name &&
            this.Surname == other.Surname &&
            this.DateOfBirth == other.DateOfBirth;
    }
}

在我的主页下,我有一个简单的ListBox,其中我显示了这些客户的集合。到目前为止一切顺利。

之后,在我的ListBox下,用户选择了这些Customer中的任何一个,然后他可以点击编辑设置按钮并编辑所选客户的属性。这是一个简单的命令:

        cmd_EditCustomer = new DelegateCommand(() =>
        {
            _navigationService.Navigate(App.Experiences.Detail.ToString(), SelectedCustomer);
        });

它只是导航到一个新页面(详细页面,用户可以在其中进行更改),我在这里传递的参数是Selected Customer

我的DetailPage View模型如下所示:

public class DetailPageViewModel : ViewModel, Interfaces.IDetailPageViewModel
{
    public DelegateCommand cmd_SaveChanges { get; set; }
    public Customer SelectedCustomer { get; set; }
    private readonly INavigationService _navigationService;
    private readonly IDialogService _dialogService;
    public DetailPageViewModel(INavigationService navigationService,
        IDialogService dialogService)
    {
        _navigationService = navigationService;
        _dialogService = dialogService;
        InitializeCommands();
    }
    public override void OnNavigatedTo(object navigationParameter, NavigationMode navigationMode, Dictionary<string, object> viewModelState)
    {
        this.SelectedCustomer = navigationParameter as Customer;
        this.SelectedCustomer?.BeginEdit();
    }
    private void InitializeCommands()
    {
        cmd_SaveChanges = new DelegateCommand(() =>
        {
            SelectedCustomer?.EndEdit();
            _dialogService.Show("Changes Saved!");
            _navigationService.Navigate(App.Experiences.Main.ToString(), null);
        });
    }
}
如您所见,这是一个非常简单的应用程序,我仅将其用于学习目的。以下是我的问题:

1)以这样的方式传递Selected Customer是好的吗?(在INavigationService的参数中),还是我应该实现其他逻辑?

2)当用户对选定的客户进行更改并单击Save Changes(您可以在那里看到的唯一命令)时,它不会更新原始客户(来自我的原始集合)。这怎么可能呢?如何实现,我的客户将被更新?我应该为此创建PubSubEvent吗?

编辑:

我已经设法找到了错误-当用户导航回MainPage时,我的MainPageViewModel被重新初始化,重新填充项目的集合。现在的问题是——如何在整个应用程序生命周期中保持MainWindowViewModel的活动?

使用Unity的通用应用程序棱镜

重新填充来自的项集合

你只需要保存一个新的值,例如,如果你从DB填充你的客户,你必须调用DB和保存更改之前导航回来等,所以在那之后,当MainPageViewModel将被重新初始化,你会得到你的更改和更改由另一个用户执行。

最后,我发现这不是在应用程序中保存数据的好方法。

根据我所读到的,我应该实现存储库策略,它只在ViewModel中引用,例如:

public MainPageViewModel(IDataRepository dataRepository, INavigationService navService, ...){etc.}

简化界面示例:

public interface IDataRepository
{
    List<string> GetListOfStrings();
    string GetUserEnteredData();
    void SetUserEnteredData(string data);
}

UnityContainer:

_container.RegisterType<IDataRepository, DataRepository>();

你可以从Patterns &实践团队在这里:https://prismwindowsruntime.codeplex.com/