简体   繁体   中英

Catel, why you update the properties of my model to null?

Subject. This is happens at the moment when i'm closing application, and in section of CloseAsync() of my ViewModel trying to save attached model, that is inherited from SavableModelBase .

My 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);
}

My Model:

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));

    ...
}

In case, if i remove attribute [ViewModelToModel("Server")] on ServerIpAddress property of my ViewModel, value is available. It is predictable - no longer due on the property with a model.

How can I get the model does not set their properties to null at the moment when i'm closing my application? And why this happens?

I don't have a lot of code to go by, but I think this is caused by the views being unloaded. This means that your vm will be canceled (not saved since there is no explicit save) and your models will be reset to reset to the original state.

You can change this behavior by setting a property on the Model attribute:

[Model(SupportIEditableObject = false)]
public Server Server
{
    ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM