Catel,为什么你将我的模型的属性更新为null?

时间:2016-10-03 05:50:14

标签: c# mvvm catel

主题。这是在我关闭应用程序时,以及我的ViewModel的CloseAsync()试图保存附加模型的部分,这是继承自SavableModelBase

我的ViewModel:

public ServerTabViewModel(Server server)
{
    Argument.IsNotNull(() => server);
    Server = server;
}

#region Properties
[Model]
public Server Server
{
    get { return GetValue<Server>(ServerProperty); }
    set { SetValue(ServerProperty, value); }
}

public static readonly PropertyData ServerProperty = RegisterProperty("Server", typeof(Server));

[ViewModelToModel("Server")]
public string ServerIpAddress
{
    get { return GetValue<string>(ServerIpAddressProperty); }
    set { SetValue(ServerIpAddressProperty, value); }
}

public static readonly PropertyData ServerIpAddressProperty = RegisterProperty("ServerIpAddress", typeof(string));

...
#endregion

protected override async Task CloseAsync()
{
    var server = new Server
    {
        ServerIpAddress = ServerIpAddress, // ServerIpAddress is null now and model property (Server.ServerIpAddress) too.
        ...
    };
    SettingsService.SaveServer(server);
}

我的模特:

public class Server : SavableModelBase<Server>
{
    public string ServerIpAddress
    {
        get { return GetValue<string>(ServerIpAddressProperty); }
        set { SetValue(ServerIpAddressProperty, value); }
    }

    public static readonly PropertyData ServerIpAddressProperty = RegisterProperty("ServerIpAddress", typeof(string));

    ...
}

如果我删除了ViewModel的[ViewModelToModel("Server")]属性上的属性ServerIpAddress,则值可用。这是可预测的 - 不再是带有模型的财产。

在关闭应用程序时,如何让模型不将其属性设置为null?为什么会这样呢?

1 个答案:

答案 0 :(得分:0)

我没有很多代码可供使用,但我认为这是由卸载的视图引起的。这意味着您的虚拟机将被取消(由于没有明确的保存而未保存),您的模型将被重置为重置为原始状态。

您可以通过在Model属性上设置属性来更改此行为:

[Model(SupportIEditableObject = false)]
public Server Server
{
    ...
}
相关问题