簡體   English   中英

卡特爾,為什么將我的模型的屬性更新為null?

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

學科。 這是在我關閉應用程序時發生的,並且在我的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));

    ...
}

如果我刪除[ViewModelToModel("Server")] ServerIpAddress屬性上的[ViewModelToModel("Server")]屬性,則該值可用。 這是可預測的-不再因模型具有屬性。

關閉應用程序時,如何獲取未將其屬性設置為null的模型? 為什么會這樣呢?

我沒有很多代碼要經過,但是我認為這是由於視圖被卸載引起的。 這意味着您的虛擬機將被取消(因為沒有顯式保存,因此不保存),並且您的模型將被重置為重置為原始狀態。

您可以通過在Model屬性上設置一個屬性來更改此行為:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM