[QueryProperty("Filename", "Filename_")]
public partial class DetailsViewModel : ObservableObject
{
[ObservableProperty]
string filename;
partial void OnFilenameChanged(string value)
{
ReadFile();
}
[Relay Command]
async Task Edit(string s)
{
// filename passed as s
await Shell.Current.GoToAsync($"{"Edit"}?Filename_{s}");
}
我使用此代码来确保设置了属性,而不是从构造函数调用 ReadFile() (其中 Filename 为 null),这在我的 DetailsViewModel 中运行良好,但在我的 EditViewModel 中使用相同的想法时,OnFilenameChanged 方法根本不运行。
[QueryProperty("Filename", "Filename_")]
public partial class EditViewModel : ObservableObject
{
[ObservableProperty]
string filename;
partial void OnFilenameChanged(string value)
{
ReadFile();
}
起初我以为问题出在 ReadFile() 方法上,但我在它上面设置了一个断点,但它从未被调用,这意味着 OnFilenameChanged 也从未被调用。我尝试过使用变量,我单步执行它,s 是我希望它传递给编辑命令时的文件名。
[QueryProperty("Filename", "Filename_")]
public partial class EditViewModel : ObservableObject
{
[ObservableProperty]
string filename;
partial void OnFilenameChanged(string value)
{
ReadFile();
} i think that it is the right way to solve this. there is no any issu