.NET Maui MVVM模式下复杂对象跨页面传递失败求助
问题根源:QueryProperty与ViewModel属性名不匹配
你的shiptoo和group列表无法传递,核心原因是ProductsViewModel中QueryProperty指定的属性名与实际生成的ObservableProperty属性名不匹配,同时LogInModel中的字段定义可优化为符合.NET规范的属性(虽非直接诱因,但推荐调整)。
具体问题点:
- Shiptoo列表传递失败:
传递的键是"shiptoo",QueryProperty指定的目标属性是"Shiptoo",但ViewModel中[ObservableProperty] List<Shiptoo> shppp;生成的属性名为Shppp,两者不匹配导致数据无法绑定。 - Group列表传递失败:
传递的键是"group",QueryProperty指定的目标属性是"Group",但ViewModel中[ObservableProperty] List<Group> groups;生成的属性名为Groups,名字不匹配导致数据无法接收。
修改方案:
1. 修正ProductsViewModel的属性对应关系
确保QueryProperty的第一个参数(ViewModel属性名)与ObservableProperty生成的属性名完全一致,以下两种方式二选一:
方式一:调整ObservableProperty变量名匹配QueryProperty
namespace Tenga.ViewModel { [QueryProperty("Products", "products")] [QueryProperty("Group", "group")] [QueryProperty("Shiptoo", "shiptoo")] [QueryProperty("Shipto", "shipto")] public partial class ProductsViewModel : BaseViewModel { public ProductsViewModel() { } [ObservableProperty] List<Shiptoo> shiptoo; // 变量名改为shiptoo,生成Shiptoo属性 [ObservableProperty] List<Group> group; // 变量名改为group,生成Group属性 [ObservableProperty] List<Products> products; [ObservableProperty] List<Shipto> shipto; } }
方式二:调整QueryProperty匹配ObservableProperty生成的属性名
namespace Tenga.ViewModel { [QueryProperty("Products", "products")] [QueryProperty("Groups", "group")] // 对应生成的Groups属性 [QueryProperty("Shppp", "shiptoo")] // 对应生成的Shppp属性 [QueryProperty("Shipto", "shipto")] public partial class ProductsViewModel : BaseViewModel { public ProductsViewModel() { } [ObservableProperty] List<Shiptoo> shppp; [ObservableProperty] List<Group> groups; [ObservableProperty] List<Products> products; [ObservableProperty] List<Shipto> shipto; } }
2. 优化LogInModel为属性(可选但推荐)
将公共字段改为自动属性,符合.NET编码规范:
namespace Tenga.Model { public class LogInModel { public string OTP { get; set; } public string CustomerNumber { get; set; } public string CustomerName { get; set; } public string Balance { get; set; } public string OpenToBuy { get; set; } public string CreditLimit { get; set; } public string LoginStatus { get; set; } public string Error { get; set; } public List<Shipto> Shp { get; set; } = new List<Shipto>(); public List<Shiptoo> Cat { get; set; } = new List<Shiptoo>(); public List<Group> Grp { get; set; } = new List<Group>(); public List<Products> Prod { get; set; } = new List<Products>(); } // 其余类保持不变 public class Shipto { public string ShipCode { get; set; } public string ShipDescription { get; set; } } public class Products { public string ItemCode { get; set; } public string ItemDescription { get; set; } public string UOM { get; set; } public string ConversionFactor { get; set; } public string Category { get; set; } public string Group { get; set; } public byte[] Image { get; set; } } public class Shiptoo { public string ShipCode { get; set; } public byte[] Image { get; set; } } public class Group { public string Category { get; set; } public string Code { get; set; } public byte[] Image { get; set; } } }
若调整了LogInModel的属性名,MainPageViewModel的传递代码需同步修改:
await Shell.Current.GoToAsync($"{nameof(ProductsPage)}", true, new Dictionary<string, object> { {"shiptoo",LogInModels[0].Cat }, {"group",LogInModels[0].Grp }, {"products",LogInModels[0].Prod }, {"shipto",LogInModels[0].Shp } });
验证要点:
- 传递字典的键名与
QueryProperty的第二个参数需完全一致(区分大小写)。 QueryProperty的第一个参数需与ViewModel中ObservableProperty生成的属性名完全一致(如[ObservableProperty] List<Group> groups;会生成Groups属性,对应[QueryProperty("Groups", "group")])。
内容的提问来源于stack exchange,提问作者tipaz
相关产品推荐
相关产品推荐

